1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Replace uses of hasOwnProperty.call with simpler alternatives (#11009)

Three uses of `hasOwnProperty.call` have been replaced with simpler
alternatives. I did this because I found this convention confusing.

The `ComposableObservableStore` changes required adding a check for
whether the `config` is defined, because the old syntax would allow
iterating over `undefined` as if it was an empty object. Functionally
it should be identical. A new test was added to highlight this
functionality.
This commit is contained in:
Mark Stacey 2021-05-11 14:09:17 -02:30 committed by Dan Miller
parent 8303e866e2
commit 352dbd8831
3 changed files with 22 additions and 18 deletions

View File

@ -13,8 +13,10 @@ export default class ComposableObservableStore extends ObservableStore {
*/
constructor(initState, config) {
super(initState);
if (config) {
this.updateStructure(config);
}
}
/**
* Composes a new internal store subscription structure
@ -24,14 +26,12 @@ export default class ComposableObservableStore extends ObservableStore {
updateStructure(config) {
this.config = config;
this.removeAllListeners();
for (const key in config) {
if (Object.prototype.hasOwnProperty.call(config, key)) {
for (const key of Object.keys(this.config)) {
config[key].subscribe((state) => {
this.updateState({ [key]: state });
});
}
}
}
/**
* Merges all child store state into a single object rather than
@ -40,16 +40,17 @@ export default class ComposableObservableStore extends ObservableStore {
* @returns {Object} Object containing merged child store state
*/
getFlatState() {
if (!this.config) {
return {};
}
let flatState = {};
for (const key in this.config) {
if (Object.prototype.hasOwnProperty.call(this.config, key)) {
for (const key of Object.keys(this.config)) {
const controller = this.config[key];
const state = controller.getState
? controller.getState()
: controller.state;
flatState = { ...flatState, ...state };
}
}
return flatState;
}
}

View File

@ -32,4 +32,9 @@ describe('ComposableObservableStore', function () {
});
assert.deepEqual(store.getFlatState(), { foo: 'foo', bar: 'bar' });
});
it('should return empty flattened state when not configured', function () {
const store = new ComposableObservableStore();
assert.deepEqual(store.getFlatState(), {});
});
});

View File

@ -40,9 +40,7 @@ export default async function getFirstPreferredLangCode() {
const firstPreferredLangCode = userPreferredLocaleCodes
.map((code) => code.toLowerCase().replace('_', '-'))
.find((code) =>
Object.prototype.hasOwnProperty.call(existingLocaleCodes, code),
);
.find((code) => existingLocaleCodes[code] !== undefined);
return existingLocaleCodes[firstPreferredLangCode] || 'en';
}