@rzl-zone/utils-js
Version:
A modern, lightweight set of JavaScript utility functions with TypeScript support for everyday development, crafted to enhance code readability and maintainability.
120 lines (115 loc) • 4.77 kB
JavaScript
/*!
* ====================================================
* Rzl Utils-JS.
* ----------------------------------------------------
* Version: 3.11.0.
* Author: Rizalvin Dwiky.
* Repository: https://github.com/rzl-zone/utils-js.
* ====================================================
*/
import { isBigInt } from './chunk-QNKGP5DY.js';
import { assertIsPlainObject, isPlainObject, isBoolean, isNaN, hasOwnProp, getPreciseType, isUndefined, isNumberObject, isInfinityNumber, isStringObject, isBooleanObject, isFunction, isSymbol, isObjectOrArray, isSet, isArray, isNull, isNumber, isObject } from './chunk-MSUW5VHZ.js';
function isMap(value) {
return Object.prototype.toString.call(value) === "[object Map]" || value instanceof Map;
}
var isDate = (value, options = {}) => {
assertIsPlainObject(options, {
message: ({ currentType, validType }) => `Second parameter (\`options\`) must be of type \`${validType}\`, but received: \`${currentType}\`.`
});
const skipInvalidDate = isPlainObject(options) && isBoolean(options.skipInvalidDate) ? options.skipInvalidDate : false;
const instanceDate = value instanceof Date;
if (skipInvalidDate) return instanceDate;
return instanceDate && !isNaN(value.getTime());
};
var safeStableStringify = (value, options = {}) => {
assertIsPlainObject(options, {
message: ({ currentType, validType }) => `Second parameter (\`options\`) must be of type \`${validType}\`, but received: \`${currentType}\`.`
});
const pretty = hasOwnProp(options, "pretty") ? options.pretty : false;
const sortKeys = hasOwnProp(options, "sortKeys") ? options.sortKeys : true;
const sortArray = hasOwnProp(options, "sortArray") ? options.sortArray : false;
const keepUndefined = hasOwnProp(options, "keepUndefined") ? options.keepUndefined : false;
if (!isBoolean(sortKeys) || !isBoolean(sortArray) || !isBoolean(pretty) || !isBoolean(keepUndefined)) {
throw new TypeError(
`Parameters \`sortKeys\`, \`sortArray\`, \`keepUndefined\` and \`pretty\` property of the \`options\` (second parameter) must be of type \`boolean\`, but received: "['sortKeys': \`${getPreciseType(
sortKeys
)}\`, 'sortArray': \`${getPreciseType(
sortArray
)}\`, 'keepUndefined': \`${getPreciseType(
keepUndefined
)}\`, 'pretty': \`${getPreciseType(pretty)}\`]".`
);
}
if (isUndefined(value)) {
return keepUndefined ? "undefined" : "null";
}
const seen = /* @__PURE__ */ new WeakSet();
const isPrimitive = (val) => isNull(val) || !isObjectOrArray(val) && !isFunction(val);
const deepProcess = (val) => {
if (isNumberObject(val)) {
const valOf = val.valueOf();
return isNaN(valOf) || isInfinityNumber(valOf) ? null : valOf;
}
if (isStringObject(val)) return val.valueOf();
if (isBooleanObject(val)) return val.valueOf();
if (isFunction(val) || isSymbol(val)) return void 0;
if (isBigInt(val)) return val.toString();
if (isNaN(val) || isInfinityNumber(val)) return null;
if (isUndefined(val)) {
return keepUndefined ? void 0 : null;
}
if (isObjectOrArray(val)) {
if (seen.has(val)) return "[Circular]";
seen.add(val);
if (isDate(val)) return val.toISOString();
if (isMap(val)) {
return {
map: Array.from(val.entries()).map(([k, v]) => [k, deepProcess(v)])
};
}
if (isSet(val)) return { set: Array.from(val.values()).map(deepProcess) };
if (isArray(val)) {
const processedArr = val.map(deepProcess);
if (sortArray) {
const primitives = [];
const nonPrimitives = [];
for (const item of processedArr) {
if (isPrimitive(item)) primitives.push(item);
else nonPrimitives.push(item);
}
primitives.sort((a, b) => {
if (isNumber(a) && isNumber(b)) return a - b;
return String(a).localeCompare(String(b));
});
return [...primitives, ...nonPrimitives];
}
return processedArr;
}
const keys = Object.keys(val);
if (sortKeys) {
keys.sort((a, b) => {
const na = Number(a);
const nb = Number(b);
if (!isNaN(na) && !isNaN(nb)) return na - nb;
return a.localeCompare(b);
});
}
const result = {};
if (isObject(val)) {
for (const k of keys) {
const v = deepProcess(val[k]);
if (!isUndefined(v)) result[k] = v;
}
}
return result;
}
return val;
};
try {
return JSON.stringify(deepProcess(value), null, pretty ? 2 : 0);
} catch (err) {
console.warn("Error in safeStableStringify:", err);
return "{}";
}
};
export { isDate, isMap, safeStableStringify };