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:
parent
8303e866e2
commit
352dbd8831
@ -13,7 +13,9 @@ export default class ComposableObservableStore extends ObservableStore {
|
|||||||
*/
|
*/
|
||||||
constructor(initState, config) {
|
constructor(initState, config) {
|
||||||
super(initState);
|
super(initState);
|
||||||
this.updateStructure(config);
|
if (config) {
|
||||||
|
this.updateStructure(config);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -24,12 +26,10 @@ export default class ComposableObservableStore extends ObservableStore {
|
|||||||
updateStructure(config) {
|
updateStructure(config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.removeAllListeners();
|
this.removeAllListeners();
|
||||||
for (const key in config) {
|
for (const key of Object.keys(this.config)) {
|
||||||
if (Object.prototype.hasOwnProperty.call(config, key)) {
|
config[key].subscribe((state) => {
|
||||||
config[key].subscribe((state) => {
|
this.updateState({ [key]: state });
|
||||||
this.updateState({ [key]: state });
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,15 +40,16 @@ export default class ComposableObservableStore extends ObservableStore {
|
|||||||
* @returns {Object} Object containing merged child store state
|
* @returns {Object} Object containing merged child store state
|
||||||
*/
|
*/
|
||||||
getFlatState() {
|
getFlatState() {
|
||||||
|
if (!this.config) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
let flatState = {};
|
let flatState = {};
|
||||||
for (const key in this.config) {
|
for (const key of Object.keys(this.config)) {
|
||||||
if (Object.prototype.hasOwnProperty.call(this.config, key)) {
|
const controller = this.config[key];
|
||||||
const controller = this.config[key];
|
const state = controller.getState
|
||||||
const state = controller.getState
|
? controller.getState()
|
||||||
? controller.getState()
|
: controller.state;
|
||||||
: controller.state;
|
flatState = { ...flatState, ...state };
|
||||||
flatState = { ...flatState, ...state };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return flatState;
|
return flatState;
|
||||||
}
|
}
|
||||||
|
@ -32,4 +32,9 @@ describe('ComposableObservableStore', function () {
|
|||||||
});
|
});
|
||||||
assert.deepEqual(store.getFlatState(), { foo: 'foo', bar: 'bar' });
|
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(), {});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -40,9 +40,7 @@ export default async function getFirstPreferredLangCode() {
|
|||||||
|
|
||||||
const firstPreferredLangCode = userPreferredLocaleCodes
|
const firstPreferredLangCode = userPreferredLocaleCodes
|
||||||
.map((code) => code.toLowerCase().replace('_', '-'))
|
.map((code) => code.toLowerCase().replace('_', '-'))
|
||||||
.find((code) =>
|
.find((code) => existingLocaleCodes[code] !== undefined);
|
||||||
Object.prototype.hasOwnProperty.call(existingLocaleCodes, code),
|
|
||||||
);
|
|
||||||
|
|
||||||
return existingLocaleCodes[firstPreferredLangCode] || 'en';
|
return existingLocaleCodes[firstPreferredLangCode] || 'en';
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user