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.
feature/default_network_editable
Mark Stacey 4 years ago committed by Dan Miller
parent 8303e866e2
commit 352dbd8831
  1. 13
      app/scripts/lib/ComposableObservableStore.js
  2. 5
      app/scripts/lib/ComposableObservableStore.test.js
  3. 4
      app/scripts/lib/get-first-preferred-lang-code.js

@ -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;
}
}

@ -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(), {});
});
});

@ -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';
}

Loading…
Cancel
Save