1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02: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,7 +13,9 @@ export default class ComposableObservableStore extends ObservableStore {
*/
constructor(initState, config) {
super(initState);
this.updateStructure(config);
if (config) {
this.updateStructure(config);
}
}
/**
@ -24,12 +26,10 @@ 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)) {
config[key].subscribe((state) => {
this.updateState({ [key]: state });
});
}
for (const key of Object.keys(this.config)) {
config[key].subscribe((state) => {
this.updateState({ [key]: state });
});
}
}
@ -40,15 +40,16 @@ 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)) {
const controller = this.config[key];
const state = controller.getState
? controller.getState()
: controller.state;
flatState = { ...flatState, ...state };
}
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';
}