objwalk 0.1.0 → 0.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/dist/index.cjs CHANGED
@@ -28,16 +28,63 @@ __export(index_exports, {
28
28
  });
29
29
  module.exports = __toCommonJS(index_exports);
30
30
 
31
- // src/each.ts
32
- var isWalkable = (value) => {
33
- return value !== null && typeof value === "object";
31
+ // src/helpers/getValueByPath.ts
32
+ var arraySegmentRegex = /^(\w+)\[(\d+)\]$/;
33
+ var getValueByPath = (obj, path) => {
34
+ if (!path) return obj;
35
+ return path.split(".").reduce((result, current) => {
36
+ if (result === null || result === void 0) {
37
+ return void 0;
38
+ }
39
+ const match = current.match(arraySegmentRegex);
40
+ if (match) {
41
+ const [, key, index] = match;
42
+ const array = result[key];
43
+ return Array.isArray(array) ? array[parseInt(index, 10)] : void 0;
44
+ }
45
+ return result[current];
46
+ }, obj);
34
47
  };
48
+
49
+ // src/helpers/parsePathSelection.ts
50
+ var selectionRegex = /(.*)\{([^}]+)\}$/;
51
+ var normalizeSelectionPath = (path) => {
52
+ return path.endsWith(".") ? path.slice(0, -1) : path;
53
+ };
54
+ var parseSelectedKeys = (selection) => {
55
+ if (selection.trim() === "*") {
56
+ return "all";
57
+ }
58
+ return selection.split(",").map((key) => key.trim()).filter(Boolean);
59
+ };
60
+ var parsePathSelection = (path = "") => {
61
+ const matches = path.match(selectionRegex);
62
+ if (!matches) {
63
+ return {
64
+ cleanPath: path,
65
+ keys: []
66
+ };
67
+ }
68
+ return {
69
+ cleanPath: normalizeSelectionPath(matches[1]),
70
+ keys: parseSelectedKeys(matches[2])
71
+ };
72
+ };
73
+
74
+ // src/helpers/createPath.ts
35
75
  var createPath = (parentPath, key, parentIsArray) => {
36
76
  if (parentIsArray) {
37
77
  return parentPath + "[" + key + "]";
38
78
  }
39
79
  return parentPath ? parentPath + "." + key : String(key);
40
80
  };
81
+
82
+ // src/helpers/isWalkable.ts
83
+ var isWalkable = (value) => {
84
+ return value !== null && typeof value === "object";
85
+ };
86
+
87
+ // src/each.ts
41
88
  var each = (data, callback) => {
42
89
  const walk = (value, path, key, parentIsArray) => {
43
90
  const currentPath = path || createPath("", key, parentIsArray);
@@ -69,48 +116,6 @@ var each = (data, callback) => {
69
116
  });
70
117
  };
71
118
 
72
- // src/helpers/getValueByPath.ts
73
- var getValueByPath = (obj, path) => {
74
- if (!path) return obj;
75
- return path.split(".").reduce((result, current) => {
76
- if (result !== null && result !== void 0) {
77
- const match = current.match(/(w+)[(d+)]/);
78
- if (match) {
79
- const [, key, index] = match;
80
- const array = result[key];
81
- return array && Array.isArray(array) ? array[parseInt(index, 10)] : void 0;
82
- }
83
- return result[current];
84
- }
85
- return void 0;
86
- }, obj);
87
- };
88
-
89
- // src/helpers/parsePathSelection.ts
90
- var selectionRegex = /(.*){([^}]+)}$/;
91
- var normalizeSelectionPath = (path) => {
92
- return path.endsWith(".") ? path.slice(0, -1) : path;
93
- };
94
- var parseSelectedKeys = (selection) => {
95
- if (selection.trim() === "*") {
96
- return "all";
97
- }
98
- return selection.split(",").map((key) => key.trim()).filter(Boolean);
99
- };
100
- var parsePathSelection = (path = "") => {
101
- const matches = path.match(selectionRegex);
102
- if (!matches) {
103
- return {
104
- cleanPath: path,
105
- keys: []
106
- };
107
- }
108
- return {
109
- cleanPath: normalizeSelectionPath(matches[1]),
110
- keys: parseSelectedKeys(matches[2])
111
- };
112
- };
113
-
114
119
  // src/pick.ts
115
120
  var pick = (data = {}, path = "") => {
116
121
  const { cleanPath, keys } = parsePathSelection(path);
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  type WalkKey = string | number;
2
+
2
3
  type EachCallback = (key: WalkKey, value: any, path: string) => void;
3
4
  declare const each: (data: any, callback: EachCallback) => void;
4
5
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  type WalkKey = string | number;
2
+
2
3
  type EachCallback = (key: WalkKey, value: any, path: string) => void;
3
4
  declare const each: (data: any, callback: EachCallback) => void;
4
5
 
package/dist/index.js CHANGED
@@ -1,13 +1,60 @@
1
- // src/each.ts
2
- var isWalkable = (value) => {
3
- return value !== null && typeof value === "object";
1
+ // src/helpers/getValueByPath.ts
2
+ var arraySegmentRegex = /^(\w+)\[(\d+)\]$/;
3
+ var getValueByPath = (obj, path) => {
4
+ if (!path) return obj;
5
+ return path.split(".").reduce((result, current) => {
6
+ if (result === null || result === void 0) {
7
+ return void 0;
8
+ }
9
+ const match = current.match(arraySegmentRegex);
10
+ if (match) {
11
+ const [, key, index] = match;
12
+ const array = result[key];
13
+ return Array.isArray(array) ? array[parseInt(index, 10)] : void 0;
14
+ }
15
+ return result[current];
16
+ }, obj);
4
17
  };
18
+
19
+ // src/helpers/parsePathSelection.ts
20
+ var selectionRegex = /(.*)\{([^}]+)\}$/;
21
+ var normalizeSelectionPath = (path) => {
22
+ return path.endsWith(".") ? path.slice(0, -1) : path;
23
+ };
24
+ var parseSelectedKeys = (selection) => {
25
+ if (selection.trim() === "*") {
26
+ return "all";
27
+ }
28
+ return selection.split(",").map((key) => key.trim()).filter(Boolean);
29
+ };
30
+ var parsePathSelection = (path = "") => {
31
+ const matches = path.match(selectionRegex);
32
+ if (!matches) {
33
+ return {
34
+ cleanPath: path,
35
+ keys: []
36
+ };
37
+ }
38
+ return {
39
+ cleanPath: normalizeSelectionPath(matches[1]),
40
+ keys: parseSelectedKeys(matches[2])
41
+ };
42
+ };
43
+
44
+ // src/helpers/createPath.ts
5
45
  var createPath = (parentPath, key, parentIsArray) => {
6
46
  if (parentIsArray) {
7
47
  return parentPath + "[" + key + "]";
8
48
  }
9
49
  return parentPath ? parentPath + "." + key : String(key);
10
50
  };
51
+
52
+ // src/helpers/isWalkable.ts
53
+ var isWalkable = (value) => {
54
+ return value !== null && typeof value === "object";
55
+ };
56
+
57
+ // src/each.ts
11
58
  var each = (data, callback) => {
12
59
  const walk = (value, path, key, parentIsArray) => {
13
60
  const currentPath = path || createPath("", key, parentIsArray);
@@ -39,48 +86,6 @@ var each = (data, callback) => {
39
86
  });
40
87
  };
41
88
 
42
- // src/helpers/getValueByPath.ts
43
- var getValueByPath = (obj, path) => {
44
- if (!path) return obj;
45
- return path.split(".").reduce((result, current) => {
46
- if (result !== null && result !== void 0) {
47
- const match = current.match(/(w+)[(d+)]/);
48
- if (match) {
49
- const [, key, index] = match;
50
- const array = result[key];
51
- return array && Array.isArray(array) ? array[parseInt(index, 10)] : void 0;
52
- }
53
- return result[current];
54
- }
55
- return void 0;
56
- }, obj);
57
- };
58
-
59
- // src/helpers/parsePathSelection.ts
60
- var selectionRegex = /(.*){([^}]+)}$/;
61
- var normalizeSelectionPath = (path) => {
62
- return path.endsWith(".") ? path.slice(0, -1) : path;
63
- };
64
- var parseSelectedKeys = (selection) => {
65
- if (selection.trim() === "*") {
66
- return "all";
67
- }
68
- return selection.split(",").map((key) => key.trim()).filter(Boolean);
69
- };
70
- var parsePathSelection = (path = "") => {
71
- const matches = path.match(selectionRegex);
72
- if (!matches) {
73
- return {
74
- cleanPath: path,
75
- keys: []
76
- };
77
- }
78
- return {
79
- cleanPath: normalizeSelectionPath(matches[1]),
80
- keys: parseSelectedKeys(matches[2])
81
- };
82
- };
83
-
84
89
  // src/pick.ts
85
90
  var pick = (data = {}, path = "") => {
86
91
  const { cleanPath, keys } = parsePathSelection(path);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "objwalk",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Small utilities for reading, writing, walking, and reshaping nested JavaScript objects.",
5
5
  "keywords": [
6
6
  "object",
@@ -18,6 +18,14 @@
18
18
  ],
19
19
  "license": "ISC",
20
20
  "author": "Sergey Khalilov <sergey.khalilov@gmail.com>",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/khalilov/objwalk.git"
24
+ },
25
+ "bugs": {
26
+ "url": "https://github.com/khalilov/objwalk/issues"
27
+ },
28
+ "homepage": "https://github.com/khalilov/objwalk#readme",
21
29
  "type": "module",
22
30
  "main": "./dist/index.cjs",
23
31
  "module": "./dist/index.js",
@@ -35,11 +43,12 @@
35
43
  ],
36
44
  "scripts": {
37
45
  "build": "tsup src/index.ts --format esm,cjs --dts --clean",
38
- "test": "echo \"Error: no test specified\" && exit 1",
46
+ "test": "vitest run",
39
47
  "pack:check": "npm pack --dry-run"
40
48
  },
41
49
  "devDependencies": {
42
50
  "tsup": "^8.5.1",
43
- "typescript": "^5.9.3"
51
+ "typescript": "^5.9.3",
52
+ "vitest": "^4.1.10"
44
53
  }
45
54
  }