UNPKG

1.25 kBJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3
4/**
5 * @name objectProperty
6 * @summary Assign a get property on the input object
7 */
8export function objectProperty(that, key, getter) {
9 // There are 3 approaches here -
10 // - Object.prototype.hasOwnProperty.call(that, key) - this only checks the current class, i.e
11 // will retuirn false if the property is set in the parent class
12 // - isUndefined(...) - this may yield a false positive when the property is there, but not set.
13 // Additionally, on pre-defined getters it may make a call
14 // - key in that - Does not need to be combined with either of the above and checks the full chain
15 if (!(key in that)) {
16 Object.defineProperty(that, key, {
17 enumerable: true,
18 // Unlike in lazy, we always call into the upper function, i.e. this method
19 // does not cache old values (it is expected to be used for dynamic values)
20 get: () => getter(key)
21 });
22 }
23}
24/**
25 * @name objectProperties
26 * @summary Assign get properties on the input object
27 */
28
29export function objectProperties(that, keys, getter) {
30 for (let i = 0; i < keys.length; i++) {
31 objectProperty(that, keys[i], k => getter(k, i));
32 }
33}
\No newline at end of file