UNPKG

@zlattice/lattice-js

Version:

Lattice blockchain TypeScript SDK with dual module support (CJS + ESM)

71 lines 2.1 kB
"use strict"; /** * Property helper functions. * * @_subsection api/utils:Properties [about-properties] */ Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveProperties = resolveProperties; exports.defineProperties = defineProperties; function checkType(value, type, name) { const types = type.split("|").map(t => t.trim()); for (let i = 0; i < types.length; i++) { switch (type) { case "any": return; case "bigint": if (typeof value === "bigint") return; break; case "boolean": if (typeof value === "boolean") return; break; case "number": if (typeof value === "number") return; break; case "string": if (typeof value === "string") return; break; } } const error = new Error(`invalid value for type ${type}`); error.code = "INVALID_ARGUMENT"; error.argument = `value.${name}`; error.value = value; throw error; } /** * Resolves to a new object that is a copy of %%value%%, but with all * values resolved. */ 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; }, {}); } /** * Assigns the %%values%% to %%target%% as read-only values. * * It %%types%% is specified, the values are checked. */ function defineProperties(target, values, types) { for (const key in values) { const value = values[key]; const type = types ? types[key] : null; if (type) { checkType(value, type, key); } Object.defineProperty(target, key, { enumerable: true, value, writable: false }); } } //# sourceMappingURL=properties.js.map