2022-11-30 15:44:23 -05:00
|
|
|
/**
|
|
|
|
* Property helper functions.
|
|
|
|
*
|
2022-12-02 21:27:06 -05:00
|
|
|
* @_subsection api/utils:Properties [about-properties]
|
2022-11-30 15:44:23 -05:00
|
|
|
*/
|
|
|
|
function checkType(value, type, name) {
|
2022-09-05 16:57:11 -04:00
|
|
|
const types = type.split("|").map(t => t.trim());
|
|
|
|
for (let i = 0; i < types.length; i++) {
|
|
|
|
switch (type) {
|
|
|
|
case "any":
|
|
|
|
return;
|
2022-11-30 15:44:23 -05:00
|
|
|
case "bigint":
|
2022-09-05 16:57:11 -04:00
|
|
|
case "boolean":
|
|
|
|
case "number":
|
|
|
|
case "string":
|
|
|
|
if (typeof (value) === type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 15:44:23 -05:00
|
|
|
const error = new Error(`invalid value for type ${type}`);
|
|
|
|
error.code = "INVALID_ARGUMENT";
|
|
|
|
error.argument = `value.${name}`;
|
|
|
|
error.value = value;
|
|
|
|
throw error;
|
|
|
|
}
|
2022-12-02 21:27:06 -05:00
|
|
|
/**
|
|
|
|
* Resolves to a new object that is a copy of %%value%%, but with all
|
|
|
|
* values resolved.
|
|
|
|
*/
|
|
|
|
export async function resolveProperties(value) {
|
|
|
|
const keys = Object.keys(value);
|
|
|
|
const results = await Promise.all(keys.map((k) => Promise.resolve(value[k])));
|
|
|
|
return results.reduce((accum, v, index) => {
|
|
|
|
accum[keys[index]] = v;
|
|
|
|
return accum;
|
|
|
|
}, {});
|
|
|
|
}
|
2022-11-30 15:44:23 -05:00
|
|
|
/**
|
|
|
|
* Assigns the %%values%% to %%target%% as read-only values.
|
|
|
|
*
|
|
|
|
* It %%types%% is specified, the values are checked.
|
|
|
|
*/
|
|
|
|
export function defineProperties(target, values, types) {
|
2022-09-05 16:57:11 -04:00
|
|
|
for (let key in values) {
|
|
|
|
let value = values[key];
|
2022-11-30 15:44:23 -05:00
|
|
|
const type = (types ? types[key] : null);
|
|
|
|
if (type) {
|
|
|
|
checkType(value, type, key);
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
|
|
|
Object.defineProperty(target, key, { enumerable: true, value, writable: false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//# sourceMappingURL=properties.js.map
|