objwalk 1.0.1 → 1.2.0

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/README.md CHANGED
@@ -13,7 +13,7 @@ npm install objwalk
13
13
  ## Usage
14
14
 
15
15
  ```ts
16
- import { each, extract, map, merge, pick, set } from 'objwalk'
16
+ import { each, equally, extract, map, merge, pick, set } from 'objwalk'
17
17
  ```
18
18
 
19
19
  ## pick
@@ -59,6 +59,19 @@ pick(data, 'items[1].title')
59
59
 
60
60
  If the path cannot be resolved, `pick` returns `undefined`.
61
61
 
62
+ By default, `pick` keeps its permissive property lookup behavior. Pass options to restrict
63
+ lookups to own properties or ignore specific keys.
64
+
65
+ ```ts
66
+ pick(data, path, {
67
+ inherited: false,
68
+ ignore: ['__proto__', 'prototype', 'constructor'],
69
+ })
70
+ ```
71
+
72
+ The options apply to every path segment, array index, and selected key. With `{*}`, ignored
73
+ keys are omitted from the returned list.
74
+
62
75
  ## set
63
76
 
64
77
  Set a value inside an object by path.
@@ -81,6 +94,21 @@ set(data, 'items[0].title', 'First')
81
94
 
82
95
  `set` mutates the object passed to it and returns the same object.
83
96
 
97
+ ## equally
98
+
99
+ Deeply compare two values for structural equality.
100
+
101
+ ```ts
102
+ equally({ a: { b: [1, 2] } }, { a: { b: [1, 2] } })
103
+ // true
104
+
105
+ equally({ a: 1 }, { a: 1, b: 2 })
106
+ // false
107
+ ```
108
+
109
+ `equally` treats `NaN` as equal to `NaN`, compares `Date` by timestamp and `RegExp` by
110
+ source and flags, and follows circular references without looping.
111
+
84
112
  ## merge
85
113
 
86
114
  Deeply merge plain objects into a target object.
@@ -205,4 +233,5 @@ extract(data, mapping)
205
233
  each(data, callback)
206
234
  map(data, callback)
207
235
  merge(target, ...sources)
236
+ equally(a, b)
208
237
  ```
package/dist/index.cjs CHANGED
@@ -29,9 +29,17 @@ __export(index_exports, {
29
29
  });
30
30
  module.exports = __toCommonJS(index_exports);
31
31
 
32
+ // src/helpers/getValueByKey.ts
33
+ var getValueByKey = (object, key, { inherited = true, ignore = [] } = {}) => {
34
+ if (object === null || object === void 0 || ignore.includes(key) || !inherited && !Object.hasOwn(object, key)) {
35
+ return void 0;
36
+ }
37
+ return object[key];
38
+ };
39
+
32
40
  // src/helpers/getValueByPath.ts
33
41
  var arraySegmentRegex = /^(\w+)\[(\d+)\]$/;
34
- var getValueByPath = (obj, path) => {
42
+ var getValueByPath = (obj, path, options = {}) => {
35
43
  if (!path) return obj;
36
44
  return path.split(".").reduce((result, current) => {
37
45
  if (result === null || result === void 0) {
@@ -40,10 +48,10 @@ var getValueByPath = (obj, path) => {
40
48
  const match = current.match(arraySegmentRegex);
41
49
  if (match) {
42
50
  const [, key, index] = match;
43
- const array = result[key];
44
- return Array.isArray(array) ? array[parseInt(index, 10)] : void 0;
51
+ const array = getValueByKey(result, key, options);
52
+ return Array.isArray(array) ? getValueByKey(array, index, options) : void 0;
45
53
  }
46
- return result[current];
54
+ return getValueByKey(result, current, options);
47
55
  }, obj);
48
56
  };
49
57
 
@@ -118,26 +126,26 @@ var each = (data, callback) => {
118
126
  };
119
127
 
120
128
  // src/pick.ts
121
- var pick = (data = {}, path = "") => {
129
+ var pick = (data = {}, path = "", options = {}) => {
122
130
  const { cleanPath, keys } = parsePathSelection(path);
123
- const value = getValueByPath(data, cleanPath);
131
+ const value = getValueByPath(data, cleanPath, options);
124
132
  if (keys.length === 0 && keys !== "all") {
125
133
  return value;
126
134
  }
127
135
  if (keys === "all") {
128
136
  if (value && typeof value === "object" && !Array.isArray(value)) {
129
- return Object.keys(value);
137
+ return Object.keys(value).filter((key) => !options.ignore?.includes(key));
130
138
  }
131
139
  return [];
132
140
  }
133
141
  if (value && typeof value === "object") {
134
142
  if (keys.length === 1) {
135
- return value[keys[0]];
143
+ return getValueByKey(value, keys[0], options);
136
144
  }
137
145
  return keys.reduce(
138
146
  (result, key) => ({
139
147
  ...result,
140
- [key]: value[key]
148
+ [key]: getValueByKey(value, key, options)
141
149
  }),
142
150
  {}
143
151
  );
package/dist/index.d.cts CHANGED
@@ -1,10 +1,14 @@
1
1
  type WalkKey = string | number;
2
+ type PickOptions = {
3
+ inherited?: boolean;
4
+ ignore?: readonly string[];
5
+ };
2
6
 
3
7
  type EachCallback = (key: WalkKey, value: any, path: string) => void;
4
8
  declare const each: (data: any, callback: EachCallback) => void;
5
9
 
6
10
  type ObjwalkObject$1 = Record<string, any>;
7
- declare const pick: (data?: ObjwalkObject$1, path?: string) => any;
11
+ declare const pick: (data?: ObjwalkObject$1, path?: string, options?: PickOptions) => any;
8
12
 
9
13
  type ExtractMapping = Record<string, string | string[]>;
10
14
  declare const extract: (source: ObjwalkObject$1, mapping: ExtractMapping) => ObjwalkObject$1;
@@ -17,4 +21,4 @@ declare const merge: <T extends ObjwalkObject$1>(target: T, ...sources: unknown[
17
21
  type ObjwalkObject = Record<string, any>;
18
22
  declare const set: (data: ObjwalkObject | undefined, path: string, value: any) => ObjwalkObject;
19
23
 
20
- export { type EachCallback, type ExtractMapping, type MapCallback, type ObjwalkObject$1 as ObjwalkObject, type WalkKey, each, extract, map, merge, pick, set };
24
+ export { type EachCallback, type ExtractMapping, type MapCallback, type ObjwalkObject$1 as ObjwalkObject, type PickOptions, type WalkKey, each, extract, map, merge, pick, set };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,14 @@
1
1
  type WalkKey = string | number;
2
+ type PickOptions = {
3
+ inherited?: boolean;
4
+ ignore?: readonly string[];
5
+ };
2
6
 
3
7
  type EachCallback = (key: WalkKey, value: any, path: string) => void;
4
8
  declare const each: (data: any, callback: EachCallback) => void;
5
9
 
6
10
  type ObjwalkObject$1 = Record<string, any>;
7
- declare const pick: (data?: ObjwalkObject$1, path?: string) => any;
11
+ declare const pick: (data?: ObjwalkObject$1, path?: string, options?: PickOptions) => any;
8
12
 
9
13
  type ExtractMapping = Record<string, string | string[]>;
10
14
  declare const extract: (source: ObjwalkObject$1, mapping: ExtractMapping) => ObjwalkObject$1;
@@ -17,4 +21,4 @@ declare const merge: <T extends ObjwalkObject$1>(target: T, ...sources: unknown[
17
21
  type ObjwalkObject = Record<string, any>;
18
22
  declare const set: (data: ObjwalkObject | undefined, path: string, value: any) => ObjwalkObject;
19
23
 
20
- export { type EachCallback, type ExtractMapping, type MapCallback, type ObjwalkObject$1 as ObjwalkObject, type WalkKey, each, extract, map, merge, pick, set };
24
+ export { type EachCallback, type ExtractMapping, type MapCallback, type ObjwalkObject$1 as ObjwalkObject, type PickOptions, type WalkKey, each, extract, map, merge, pick, set };
package/dist/index.js CHANGED
@@ -1,6 +1,14 @@
1
+ // src/helpers/getValueByKey.ts
2
+ var getValueByKey = (object, key, { inherited = true, ignore = [] } = {}) => {
3
+ if (object === null || object === void 0 || ignore.includes(key) || !inherited && !Object.hasOwn(object, key)) {
4
+ return void 0;
5
+ }
6
+ return object[key];
7
+ };
8
+
1
9
  // src/helpers/getValueByPath.ts
2
10
  var arraySegmentRegex = /^(\w+)\[(\d+)\]$/;
3
- var getValueByPath = (obj, path) => {
11
+ var getValueByPath = (obj, path, options = {}) => {
4
12
  if (!path) return obj;
5
13
  return path.split(".").reduce((result, current) => {
6
14
  if (result === null || result === void 0) {
@@ -9,10 +17,10 @@ var getValueByPath = (obj, path) => {
9
17
  const match = current.match(arraySegmentRegex);
10
18
  if (match) {
11
19
  const [, key, index] = match;
12
- const array = result[key];
13
- return Array.isArray(array) ? array[parseInt(index, 10)] : void 0;
20
+ const array = getValueByKey(result, key, options);
21
+ return Array.isArray(array) ? getValueByKey(array, index, options) : void 0;
14
22
  }
15
- return result[current];
23
+ return getValueByKey(result, current, options);
16
24
  }, obj);
17
25
  };
18
26
 
@@ -87,26 +95,26 @@ var each = (data, callback) => {
87
95
  };
88
96
 
89
97
  // src/pick.ts
90
- var pick = (data = {}, path = "") => {
98
+ var pick = (data = {}, path = "", options = {}) => {
91
99
  const { cleanPath, keys } = parsePathSelection(path);
92
- const value = getValueByPath(data, cleanPath);
100
+ const value = getValueByPath(data, cleanPath, options);
93
101
  if (keys.length === 0 && keys !== "all") {
94
102
  return value;
95
103
  }
96
104
  if (keys === "all") {
97
105
  if (value && typeof value === "object" && !Array.isArray(value)) {
98
- return Object.keys(value);
106
+ return Object.keys(value).filter((key) => !options.ignore?.includes(key));
99
107
  }
100
108
  return [];
101
109
  }
102
110
  if (value && typeof value === "object") {
103
111
  if (keys.length === 1) {
104
- return value[keys[0]];
112
+ return getValueByKey(value, keys[0], options);
105
113
  }
106
114
  return keys.reduce(
107
115
  (result, key) => ({
108
116
  ...result,
109
- [key]: value[key]
117
+ [key]: getValueByKey(value, key, options)
110
118
  }),
111
119
  {}
112
120
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "objwalk",
3
- "version": "1.0.1",
3
+ "version": "1.2.0",
4
4
  "description": "Small utilities for reading, writing, walking, and reshaping nested JavaScript objects.",
5
5
  "keywords": [
6
6
  "object",