UNPKG

1.07 kBJavaScriptView Raw
1/**
2 * Sets a constant default value for the property when it is undefined.
3 * @template T
4 * @template {keyof T} Property
5 * @param {T} object An object.
6 * @param {Property} property A property of the provided object.
7 * @param {T[Property]} [defaultValue] The default value to set for the property.
8 * @returns {T[Property]} The defaulted property value.
9 */
10const d = (object, property, defaultValue) => {
11 if (typeof object[property] === 'undefined' && typeof defaultValue !== 'undefined') {
12 object[property] = defaultValue;
13 }
14 return object[property];
15};
16
17/**
18 * Resolves the value for a nested object option.
19 * @template T
20 * @template {keyof T} Property
21 * @template Result
22 * @param {T} object An object.
23 * @param {Property} property A property of the provided object.
24 * @param {function(T | undefined): Result} fn The handler to resolve the property's value.
25 * @returns {Result} The resolved option value.
26 */
27const n = (object, property, fn) => {
28 object[property] = fn(object[property]);
29 return object[property];
30};
31
32module.exports = { d, n };