UNPKG

1.33 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.objectProperties = objectProperties;
7exports.objectProperty = objectProperty;
8
9var _undefined = require("../is/undefined");
10
11// Copyright 2017-2022 @polkadot/util authors & contributors
12// SPDX-License-Identifier: Apache-2.0
13
14/**
15 * @name objectProperty
16 * @summary Assign a get property on the input object
17 */
18function objectProperty(that, key, getter) {
19 // We use both the hasOwnProperty as well as isUndefined checks here, since it may be set
20 // in inherited classes and _Own_ properties refers to the class only, not only parents
21 if (!Object.prototype.hasOwnProperty.call(that, key) && (0, _undefined.isUndefined)(that[key])) {
22 Object.defineProperty(that, key, {
23 enumerable: true,
24 // Since we don't use any additional this internally, we can use arrow (unlike lazy)
25 // Unlike in lazy, we always call into the upper function, i.e. this method
26 // does not cache old values (it is expected to be used for dynamic values)
27 get: () => getter(key)
28 });
29 }
30}
31/**
32 * @name objectProperties
33 * @summary Assign get properties on the input object
34 */
35
36
37function objectProperties(that, keys, getter) {
38 for (let i = 0; i < keys.length; i++) {
39 objectProperty(that, keys[i], k => getter(k, i));
40 }
41}
\No newline at end of file