Debian Codegraph @

smob/dist/index.cjs

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