parjs
Version:
Library for building parsers using combinators.
82 lines • 2.34 kB
JavaScript
;
/* eslint-disable @typescript-eslint/no-explicit-any */
Object.defineProperty(exports, "__esModule", { value: true });
exports.range = exports.clone = exports.defaults = exports.defaultsDeep = exports.cloneDeep = void 0;
function cloneDeep(source) {
if (typeof source !== "object" || !source) {
return source;
}
if (Array.isArray(source)) {
return source.map(x => cloneDeep(x));
}
const newObj = Object.create(Object.getPrototypeOf(source));
for (const key of Object.keys(source)) {
newObj[key] = cloneDeep(source[key]);
}
return newObj;
}
exports.cloneDeep = cloneDeep;
// Based on lodash's implementation: https://github.com/lodash/lodash
function _defaultsDeep(target, source) {
target = Object(target);
if (!source)
return target;
source = Object(source);
for (const key of Object.keys(source)) {
const value = source[key];
if (typeof target[key] === "object") {
defaultsDeep(target[key], Object(value));
}
else {
if (target[key] === undefined) {
target[key] = value;
}
}
}
return target;
}
function defaultsDeep(target, ...sources) {
for (const source of sources) {
target = _defaultsDeep(target, source);
}
return target;
}
exports.defaultsDeep = defaultsDeep;
function defaults(arg, ...sources) {
const target = Object(arg);
for (const source of sources) {
if (!source) {
continue;
}
for (const key of Object.keys(source)) {
if (!(key in target) || target[key] === undefined) {
target[key] = source[key];
}
}
}
return target;
}
exports.defaults = defaults;
function clone(source) {
if (typeof source !== "object" || !source) {
return source;
}
if (Array.isArray(source)) {
return source.slice();
}
const newObj = Object.create(Object.getPrototypeOf(source));
for (const key of Object.keys(source)) {
newObj[key] = source[key];
}
return newObj;
}
exports.clone = clone;
function range(start, end) {
const arr = [];
for (let i = start; i < end; i++) {
arr.push(i);
}
return arr;
}
exports.range = range;
//# sourceMappingURL=utils.js.map