UNPKG

1.59 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.objectProperties = exports.objectProperty = void 0;
4/**
5 * @name objectProperty
6 * @summary Assign a get property on the input object
7 */
8function objectProperty(that, key, getter, getName, index = 0) {
9 const name = getName
10 ? getName(key, index)
11 : key;
12 // There are 3 approaches here -
13 // - Object.prototype.hasOwnProperty.call(that, key) - this only checks the current class, i.e
14 // will retuirn false if the property is set in the parent class
15 // - isUndefined(...) - this may yield a false positive when the property is there, but not set.
16 // Additionally, on pre-defined getters it may make a call
17 // - key in that - Does not need to be combined with either of the above and checks the full chain
18 if (!(name in that)) {
19 Object.defineProperty(that, name, {
20 enumerable: true,
21 // Unlike in lazy, we always call into the upper function, i.e. this method
22 // does not cache old values (it is expected to be used for dynamic values)
23 get: function () {
24 return getter(key, index, this);
25 }
26 });
27 }
28}
29exports.objectProperty = objectProperty;
30/**
31 * @name objectProperties
32 * @summary Assign get properties on the input object
33 */
34function objectProperties(that, keys, getter, getName) {
35 for (let i = 0, count = keys.length; i < count; i++) {
36 objectProperty(that, keys[i], getter, getName, i);
37 }
38}
39exports.objectProperties = objectProperties;