objwalk 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -20,7 +20,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ clone: () => clone,
23
24
  each: () => each,
25
+ equally: () => equally,
24
26
  extract: () => extract,
25
27
  map: () => map,
26
28
  merge: () => merge,
@@ -269,9 +271,103 @@ var merge = (target, ...sources) => {
269
271
  }
270
272
  return target;
271
273
  };
274
+
275
+ // src/deepEqual.ts
276
+ var deepEqual = (x, y, store) => {
277
+ if (Number.isNaN(x) && Number.isNaN(y)) {
278
+ return true;
279
+ }
280
+ if (x === y) {
281
+ return true;
282
+ }
283
+ if (x == null || y == null) {
284
+ return false;
285
+ }
286
+ if (typeof x !== "object" || typeof y !== "object") {
287
+ return false;
288
+ }
289
+ let seen = store.get(x);
290
+ if (seen !== void 0 && seen.has(y)) {
291
+ return true;
292
+ }
293
+ if (seen === void 0) {
294
+ seen = /* @__PURE__ */ new WeakSet();
295
+ store.set(x, seen);
296
+ }
297
+ seen.add(y);
298
+ if (x instanceof Date && y instanceof Date) {
299
+ return x.getTime() === y.getTime();
300
+ }
301
+ if (x instanceof RegExp && y instanceof RegExp) {
302
+ return x.toString() === y.toString();
303
+ }
304
+ const isArrayX = Array.isArray(x);
305
+ const isArrayY = Array.isArray(y);
306
+ if (isArrayX !== isArrayY) {
307
+ return false;
308
+ }
309
+ if (isArrayX) {
310
+ return equalArrays(x, y, store);
311
+ }
312
+ return equalObjects(x, y, store);
313
+ };
314
+ var equalArrays = (x, y, store) => x.length === y.length && x.every((value, index) => deepEqual(value, y[index], store));
315
+ var equalObjects = (x, y, store) => {
316
+ const keysX = Reflect.ownKeys(x);
317
+ const keysY = Reflect.ownKeys(y);
318
+ if (keysX.length !== keysY.length) {
319
+ return false;
320
+ }
321
+ return keysX.every((key) => {
322
+ if (!Object.hasOwn(y, key)) {
323
+ return false;
324
+ }
325
+ return deepEqual(x[key], y[key], store);
326
+ });
327
+ };
328
+
329
+ // src/equally.ts
330
+ var equally = (a, b) => deepEqual(a, b, /* @__PURE__ */ new WeakMap());
331
+
332
+ // src/clone.ts
333
+ var clone = (value) => {
334
+ const store = /* @__PURE__ */ new WeakMap();
335
+ const cloneValue = (x) => {
336
+ if (x === null || typeof x !== "object") {
337
+ return x;
338
+ }
339
+ const obj = x;
340
+ if (store.has(obj)) {
341
+ return store.get(obj);
342
+ }
343
+ if (obj instanceof Date) {
344
+ return new Date(obj.getTime());
345
+ }
346
+ if (obj instanceof RegExp) {
347
+ return new RegExp(obj.source, obj.flags);
348
+ }
349
+ if (Array.isArray(obj)) {
350
+ const arr = [];
351
+ store.set(obj, arr);
352
+ for (let i = 0; i < obj.length; i++) {
353
+ arr[i] = cloneValue(obj[i]);
354
+ }
355
+ return arr;
356
+ }
357
+ const result = {};
358
+ store.set(obj, result);
359
+ for (const key of Reflect.ownKeys(obj)) {
360
+ result[key] = cloneValue(obj[key]);
361
+ }
362
+ return result;
363
+ };
364
+ return cloneValue(value);
365
+ };
272
366
  // Annotate the CommonJS export names for ESM import in node:
273
367
  0 && (module.exports = {
368
+ clone,
274
369
  each,
370
+ equally,
275
371
  extract,
276
372
  map,
277
373
  merge,
package/dist/index.d.cts CHANGED
@@ -21,4 +21,8 @@ declare const merge: <T extends ObjwalkObject$1>(target: T, ...sources: unknown[
21
21
  type ObjwalkObject = Record<string, any>;
22
22
  declare const set: (data: ObjwalkObject | undefined, path: string, value: any) => ObjwalkObject;
23
23
 
24
- export { type EachCallback, type ExtractMapping, type MapCallback, type ObjwalkObject$1 as ObjwalkObject, type PickOptions, type WalkKey, each, extract, map, merge, pick, set };
24
+ declare const equally: (a: unknown, b: unknown) => boolean;
25
+
26
+ declare const clone: <T>(value: T) => T;
27
+
28
+ export { type EachCallback, type ExtractMapping, type MapCallback, type ObjwalkObject$1 as ObjwalkObject, type PickOptions, type WalkKey, clone, each, equally, extract, map, merge, pick, set };
package/dist/index.d.ts CHANGED
@@ -21,4 +21,8 @@ declare const merge: <T extends ObjwalkObject$1>(target: T, ...sources: unknown[
21
21
  type ObjwalkObject = Record<string, any>;
22
22
  declare const set: (data: ObjwalkObject | undefined, path: string, value: any) => ObjwalkObject;
23
23
 
24
- export { type EachCallback, type ExtractMapping, type MapCallback, type ObjwalkObject$1 as ObjwalkObject, type PickOptions, type WalkKey, each, extract, map, merge, pick, set };
24
+ declare const equally: (a: unknown, b: unknown) => boolean;
25
+
26
+ declare const clone: <T>(value: T) => T;
27
+
28
+ export { type EachCallback, type ExtractMapping, type MapCallback, type ObjwalkObject$1 as ObjwalkObject, type PickOptions, type WalkKey, clone, each, equally, extract, map, merge, pick, set };
package/dist/index.js CHANGED
@@ -238,8 +238,102 @@ var merge = (target, ...sources) => {
238
238
  }
239
239
  return target;
240
240
  };
241
+
242
+ // src/deepEqual.ts
243
+ var deepEqual = (x, y, store) => {
244
+ if (Number.isNaN(x) && Number.isNaN(y)) {
245
+ return true;
246
+ }
247
+ if (x === y) {
248
+ return true;
249
+ }
250
+ if (x == null || y == null) {
251
+ return false;
252
+ }
253
+ if (typeof x !== "object" || typeof y !== "object") {
254
+ return false;
255
+ }
256
+ let seen = store.get(x);
257
+ if (seen !== void 0 && seen.has(y)) {
258
+ return true;
259
+ }
260
+ if (seen === void 0) {
261
+ seen = /* @__PURE__ */ new WeakSet();
262
+ store.set(x, seen);
263
+ }
264
+ seen.add(y);
265
+ if (x instanceof Date && y instanceof Date) {
266
+ return x.getTime() === y.getTime();
267
+ }
268
+ if (x instanceof RegExp && y instanceof RegExp) {
269
+ return x.toString() === y.toString();
270
+ }
271
+ const isArrayX = Array.isArray(x);
272
+ const isArrayY = Array.isArray(y);
273
+ if (isArrayX !== isArrayY) {
274
+ return false;
275
+ }
276
+ if (isArrayX) {
277
+ return equalArrays(x, y, store);
278
+ }
279
+ return equalObjects(x, y, store);
280
+ };
281
+ var equalArrays = (x, y, store) => x.length === y.length && x.every((value, index) => deepEqual(value, y[index], store));
282
+ var equalObjects = (x, y, store) => {
283
+ const keysX = Reflect.ownKeys(x);
284
+ const keysY = Reflect.ownKeys(y);
285
+ if (keysX.length !== keysY.length) {
286
+ return false;
287
+ }
288
+ return keysX.every((key) => {
289
+ if (!Object.hasOwn(y, key)) {
290
+ return false;
291
+ }
292
+ return deepEqual(x[key], y[key], store);
293
+ });
294
+ };
295
+
296
+ // src/equally.ts
297
+ var equally = (a, b) => deepEqual(a, b, /* @__PURE__ */ new WeakMap());
298
+
299
+ // src/clone.ts
300
+ var clone = (value) => {
301
+ const store = /* @__PURE__ */ new WeakMap();
302
+ const cloneValue = (x) => {
303
+ if (x === null || typeof x !== "object") {
304
+ return x;
305
+ }
306
+ const obj = x;
307
+ if (store.has(obj)) {
308
+ return store.get(obj);
309
+ }
310
+ if (obj instanceof Date) {
311
+ return new Date(obj.getTime());
312
+ }
313
+ if (obj instanceof RegExp) {
314
+ return new RegExp(obj.source, obj.flags);
315
+ }
316
+ if (Array.isArray(obj)) {
317
+ const arr = [];
318
+ store.set(obj, arr);
319
+ for (let i = 0; i < obj.length; i++) {
320
+ arr[i] = cloneValue(obj[i]);
321
+ }
322
+ return arr;
323
+ }
324
+ const result = {};
325
+ store.set(obj, result);
326
+ for (const key of Reflect.ownKeys(obj)) {
327
+ result[key] = cloneValue(obj[key]);
328
+ }
329
+ return result;
330
+ };
331
+ return cloneValue(value);
332
+ };
241
333
  export {
334
+ clone,
242
335
  each,
336
+ equally,
243
337
  extract,
244
338
  map,
245
339
  merge,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "objwalk",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Small utilities for reading, writing, walking, and reshaping nested JavaScript objects.",
5
5
  "keywords": [
6
6
  "object",