Debian Codegraph @

smob/dist/index.mjs

raw
1 //#region src/constants.ts
2 let PriorityName = /* @__PURE__ */ function(PriorityName) {
3 PriorityName["LEFT"] = "left";
4 PriorityName["RIGHT"] = "right";
5 return PriorityName;
6 }({});
7 //#endregion
8 //#region src/utils/check.ts
9 function isObject(item) {
10 return !!item && typeof item === "object" && !Array.isArray(item);
11 }
12 function isSafeKey(key) {
13 return key !== "__proto__" && key !== "prototype" && key !== "constructor";
14 }
15 function isEqual(x, y) {
16 if (Object.is(x, y)) return true;
17 if (x instanceof Date && y instanceof Date) return x.getTime() === y.getTime();
18 if (x instanceof RegExp && y instanceof RegExp) return x.toString() === y.toString();
19 if (isObject(x) && isObject(y)) {
20 const keysX = Reflect.ownKeys(x);
21 const keysY = Reflect.ownKeys(y);
22 if (keysX.length !== keysY.length) return false;
23 for (let i = 0; i < keysX.length; i++) {
24 const key = keysX[i];
25 if (!Reflect.has(y, key) || !isEqual(x[key], y[key])) return false;
26 }
27 return true;
28 }
29 if (Array.isArray(x) && Array.isArray(y)) {
30 if (x.length !== y.length) return false;
31 for (let i = 0; i < x.length; i++) if (!isEqual(x[i], y[i])) return false;
32 return true;
33 }
34 return false;
35 }
36 //#endregion
37 //#region src/utils/array.ts
38 function distinctArray(arr) {
39 for (let i = 0; i < arr.length; i++) for (let j = arr.length - 1; j > i; j--) if (isEqual(arr[i], arr[j])) arr.splice(j, 1);
40 return arr;
41 }
42 //#endregion
43 //#region src/utils/clone.ts
44 /* istanbul ignore next */
45 const gT = (() => {
46 if (typeof globalThis !== "undefined") return globalThis;
47 if (typeof self !== "undefined") return self;
48 if (typeof window !== "undefined") return window;
49 if (typeof global !== "undefined") return global;
50 throw new Error("unable to locate global object");
51 })();
52 function polyfillClone(input) {
53 const map = /* @__PURE__ */ new WeakMap();
54 const fn = (value) => {
55 if (Array.isArray(value)) {
56 if (map.has(value)) return map.get(value);
57 const cloned = [];
58 map.set(value, cloned);
59 value.map((el) => cloned.push(fn(el)));
60 return cloned;
61 }
62 if (isObject(value)) {
63 if (map.has(value)) return map.get(value);
64 const output = {};
65 const keys = Object.keys(value);
66 map.set(value, output);
67 for (let i = 0; i < keys.length; i++) output[keys[i]] = fn(value[keys[i]]);
68 return output;
69 }
70 return value;
71 };
72 return fn(input);
73 }
74 /* istanbul ignore next */
75 function clone(value) {
76 if (gT.structuredClone) return gT.structuredClone(value);
77 /* istanbul ignore next */
78 return polyfillClone(value);
79 }
80 //#endregion
81 //#region src/utils/object.ts
82 function hasOwnProperty(obj, prop) {
83 return Object.prototype.hasOwnProperty.call(obj, prop);
84 }
85 //#endregion
86 //#region src/utils/options.ts
87 function buildOptions(options = {}) {
88 options.array = options.array ?? true;
89 options.arrayDistinct = options.arrayDistinct ?? false;
90 options.clone = options.clone ?? false;
91 options.inPlace = options.inPlace ?? false;
92 options.priority = options.priority || "left";
93 options.arrayPriority = options.arrayPriority || options.priority;
94 return options;
95 }
96 function togglePriority(priority) {
97 return priority === "left" ? `right` : `left`;
98 }
99 //#endregion
100 //#region src/module.ts
101 function baseMerger(context, ...sources) {
102 let target;
103 let source;
104 let { priority } = context.options;
105 if (sources.length >= 2) {
106 if (Array.isArray(sources.at(0)) && Array.isArray(sources.at(-1))) priority = context.options.arrayPriority;
107 }
108 if (priority === "right") {
109 target = sources.pop();
110 source = sources.pop();
111 } else {
112 target = sources.shift();
113 source = sources.shift();
114 }
115 if (!source) {
116 if (Array.isArray(target) && context.options.arrayDistinct) return distinctArray(target);
117 return target;
118 }
119 if (Array.isArray(target) && Array.isArray(source)) {
120 target.push(...source);
121 if (context.options.arrayPriority === "right") return baseMerger(context, ...sources, target);
122 return baseMerger(context, target, ...sources);
123 }
124 context.map.set(source, true);
125 if (isObject(target) && isObject(source)) {
126 const keys = Object.keys(source);
127 for (let i = 0; i < keys.length; i++) {
128 const key = keys[i];
129 if (!isSafeKey(key)) continue;
130 if (hasOwnProperty(target, key)) {
131 if (context.options.strategy) {
132 if (typeof context.options.strategy(target, key, source[key]) !== "undefined") continue;
133 }
134 if (isObject(target[key]) && isObject(source[key])) {
135 if (context.map.has(source[key])) {
136 const sourceKeys = Object.keys(source[key]);
137 for (let j = 0; j < sourceKeys.length; j++) if (isSafeKey(sourceKeys[j]) && !hasOwnProperty(target[key], sourceKeys[j])) target[key][sourceKeys[j]] = source[key][sourceKeys[j]];
138 continue;
139 }
140 if (context.options.priority === "right") target[key] = baseMerger(context, source[key], target[key]);
141 else target[key] = baseMerger(context, target[key], source[key]);
142 continue;
143 }
144 if (context.options.array && Array.isArray(target[key]) && Array.isArray(source[key])) switch (context.options.priority !== context.options.arrayPriority ? togglePriority(context.options.arrayPriority) : context.options.arrayPriority) {
145 case "left":
146 Object.assign(target, { [key]: baseMerger(context, target[key], source[key]) });
147 break;
148 case "right":
149 Object.assign(target, { [key]: baseMerger(context, source[key], target[key]) });
150 break;
151 }
152 } else Object.assign(target, { [key]: source[key] });
153 }
154 }
155 context.map = /* @__PURE__ */ new WeakMap();
156 if (context.options.priority === "right") return baseMerger(context, ...sources, target);
157 return baseMerger(context, target, ...sources);
158 }
159 function createMerger(input) {
160 const options = buildOptions(input);
161 return (...sources) => {
162 if (!sources.length) throw new SyntaxError("At least one input element is required.");
163 const ctx = {
164 options,
165 map: /* @__PURE__ */ new WeakMap()
166 };
167 if (options.clone) return baseMerger(ctx, ...clone(sources));
168 if (!options.inPlace) {
169 if (Array.isArray(sources.at(0)) && options.arrayPriority === "left") {
170 sources.unshift([]);
171 return baseMerger(ctx, ...sources);
172 }
173 if (Array.isArray(sources.at(-1)) && options.arrayPriority === "right") {
174 sources.push([]);
175 return baseMerger(ctx, ...sources);
176 }
177 if (options.priority === "left") sources.unshift({});
178 else sources.push({});
179 }
180 return baseMerger(ctx, ...sources);
181 };
182 }
183 const merge = createMerger();
184 //#endregion
185 //#region src/presets.ts
186 /**
187 * Assign source attributes to a target object.
188 *
189 * @param target
190 * @param sources
191 */
192 function assign(target, ...sources) {
193 return createMerger({
194 inPlace: true,
195 priority: "left",
196 array: false
197 })(target, ...sources);
198 }
199 //#endregion
200 export { PriorityName, assign, buildOptions, clone, createMerger, distinctArray, hasOwnProperty, isEqual, isObject, isSafeKey, merge, polyfillClone, togglePriority };
201
202 //# sourceMappingURL=index.mjs.map