UNPKG

xuxi

Version:

Dynamically utility for combining different types of values ​​into a single value.

160 lines (159 loc) 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.string = void 0; exports.recursive = recursiveFn; exports.instanceof = instanceofFn; const variant_1 = require("./variant"); /** * Serializes a given value into a space-separated string. * @param v - The value to be processed. * @returns A space-separated string representation of the value. */ function sv(v) { let k, y, s = ''; if (typeof v === 'string' || typeof v === 'number' || typeof v === 'bigint' || v === null) { s += v; } else if (typeof v === 'object') { if (Array.isArray(v)) { let o = v.length; for (k = 0; k < o; k++) { if (v[k]) { if ((y = sv(v[k]))) { s && (s += ' '); s += y; } } } } else { for (y in v) { if (v[y]) { s && (s += ' '); s += y; } } } } else if (typeof v === 'function') { s += sv(v(s)); } return s; } /** * Recursively serializes objects into a key-value string format. * @param v - The value to be processed. * @returns A string representation of the object. */ function rv(v) { let k, y, s = ''; if (typeof v === 'object' && v !== null) { if (Array.isArray(v)) { let o = v.length; for (k = 0; k < o; k++) { if (v[k]) { if ((y = rv(v[k]))) { s && (s += ' '); s += y; } } } } else { for (y in v) { if (v[y]) { s && (s += ' '); s += `${y}:${typeof v[y] === 'object' ? rv(v[y]) : v[y]}`; } } for (const sym of Object.getOwnPropertySymbols(v)) { if (v[sym]) { s && (s += ' '); s += `${String(sym)}:${v[sym]}`; } } } } return s; } /** * Serializes instances of Date, Map, and Set objects into a string format. * @param v - The value to be processed. * @returns A string representation of the instance. */ function iv(v) { let k, y, s = ''; if (v instanceof Date) { s += v.toISOString(); } else if (v instanceof Map) { for (const [q, u] of v.entries()) { if (u) { s && (s += ' '); // concatenation s += `${q}:${u}`; } } } else if (v instanceof Set) { v.forEach(e => { if (e) { s && (s += ' '); s += e; } }); } else if (typeof v === 'object') { if (Array.isArray(v)) { var o = v.length; for (k = 0; k < o; k++) { if (v[k]) { if ((y = iv(v[k]))) { s && (s += ' '); s += y; } } } } } else if (typeof v === 'function') { s += iv(v(s)); } return s; } /** * Applies recursive transformation to the input values. * @param args - Input values. * @returns Transformed string. */ function recursiveFn(...args) { return rv(args); } /** * Applies instance-based transformation to the input values. * @param args - Input values. * @returns Transformed string. */ function instanceofFn(...args) { return iv(args); } /** * Converts input values into a space-separated string. * @param args - Input values. * @returns The formatted string. */ const string = (...args) => { let i = 0, t, x, s = '', o = args.length; for (; i < o; i++) { if ((t = args[i])) { if ((x = sv(t))) { s && (s += ' '); s += x; } } } return s; }; exports.string = string; string.recursive = recursiveFn; string.instanceof = instanceofFn; string.variant = variant_1.variant;