@ls-stack/utils 3.50.0 → 3.51.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.
@@ -0,0 +1,151 @@
1
+ import {
2
+ typedObjectEntries
3
+ } from "./chunk-GMJTLFM6.js";
4
+ import {
5
+ sortBy
6
+ } from "./chunk-27AL66CH.js";
7
+
8
+ // src/objUtils.ts
9
+ import { Result } from "t-result";
10
+ function objectTypedEntries(obj) {
11
+ return Object.entries(obj);
12
+ }
13
+ function pick(obj, keys) {
14
+ const result = {};
15
+ for (const key of keys) {
16
+ result[key] = obj[key];
17
+ }
18
+ return result;
19
+ }
20
+ function mapArrayToObject(array, mapper) {
21
+ return Object.fromEntries(array.map(mapper));
22
+ }
23
+ function mapObjectToObject(obj, mapper) {
24
+ return Object.fromEntries(
25
+ objectTypedEntries(obj).map(([key, value]) => mapper(key, value))
26
+ );
27
+ }
28
+ function omit(obj, keys) {
29
+ const result = {};
30
+ for (const key of Object.keys(obj)) {
31
+ if (!keys.includes(key)) {
32
+ result[key] = obj[key];
33
+ }
34
+ }
35
+ return result;
36
+ }
37
+ function looseGetObjectProperty(obj, key) {
38
+ return obj[key];
39
+ }
40
+ function rejectObjUndefinedValues(obj) {
41
+ const result = {};
42
+ for (const key in obj) {
43
+ if (obj[key] !== void 0) {
44
+ result[key] = obj[key];
45
+ }
46
+ }
47
+ return result;
48
+ }
49
+ function filterObjectKeys(obj, predicate) {
50
+ return Object.fromEntries(
51
+ Object.entries(obj).filter(
52
+ ([key, value]) => predicate(key, value)
53
+ )
54
+ );
55
+ }
56
+ function sortObjectKeys(obj, sortByFn, options) {
57
+ return Object.fromEntries(
58
+ sortBy(typedObjectEntries(obj), sortByFn, options)
59
+ );
60
+ }
61
+ function getValueFromPath(obj, path) {
62
+ if (!path.trim()) {
63
+ return Result.err(new Error("Path cannot be empty"));
64
+ }
65
+ const segments = parsePath(path);
66
+ let current = obj;
67
+ for (let i = 0; i < segments.length; i++) {
68
+ const segment = segments[i];
69
+ if (!segment) {
70
+ return Result.err(new Error("Invalid empty segment in path"));
71
+ }
72
+ if (current == null) {
73
+ return Result.err(
74
+ new Error(`Cannot access property '${segment}' on null or undefined`)
75
+ );
76
+ }
77
+ if (isNumericString(segment)) {
78
+ if (!Array.isArray(current)) {
79
+ return Result.err(
80
+ new Error(
81
+ `Cannot access array index '${segment}' on non-array value`
82
+ )
83
+ );
84
+ }
85
+ const index = parseInt(segment, 10);
86
+ if (index < 0 || index >= current.length) {
87
+ return Result.err(new Error(`Array index '${index}' out of bounds`));
88
+ }
89
+ current = current[index];
90
+ } else {
91
+ if (typeof current !== "object" || current === null) {
92
+ return Result.err(
93
+ new Error(`Cannot access property '${segment}' on non-object value`)
94
+ );
95
+ }
96
+ if (!(segment in current)) {
97
+ return Result.err(new Error(`Property '${segment}' not found`));
98
+ }
99
+ current = current[segment];
100
+ }
101
+ }
102
+ return Result.ok(current);
103
+ }
104
+ function parsePath(path) {
105
+ const segments = [];
106
+ let current = "";
107
+ let inBrackets = false;
108
+ for (let i = 0; i < path.length; i++) {
109
+ const char = path[i];
110
+ if (char === "[") {
111
+ if (current) {
112
+ segments.push(current);
113
+ current = "";
114
+ }
115
+ inBrackets = true;
116
+ } else if (char === "]") {
117
+ if (inBrackets && current) {
118
+ segments.push(current);
119
+ current = "";
120
+ }
121
+ inBrackets = false;
122
+ } else if (char === "." && !inBrackets) {
123
+ if (current) {
124
+ segments.push(current);
125
+ current = "";
126
+ }
127
+ } else {
128
+ current += char;
129
+ }
130
+ }
131
+ if (current) {
132
+ segments.push(current);
133
+ }
134
+ return segments;
135
+ }
136
+ function isNumericString(str) {
137
+ return /^\d+$/.test(str);
138
+ }
139
+
140
+ export {
141
+ objectTypedEntries,
142
+ pick,
143
+ mapArrayToObject,
144
+ mapObjectToObject,
145
+ omit,
146
+ looseGetObjectProperty,
147
+ rejectObjUndefinedValues,
148
+ filterObjectKeys,
149
+ sortObjectKeys,
150
+ getValueFromPath
151
+ };
package/dist/objUtils.cjs CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var objUtils_exports = {};
22
22
  __export(objUtils_exports, {
23
23
  filterObjectKeys: () => filterObjectKeys,
24
+ getValueFromPath: () => getValueFromPath,
24
25
  looseGetObjectProperty: () => looseGetObjectProperty,
25
26
  mapArrayToObject: () => mapArrayToObject,
26
27
  mapObjectToObject: () => mapObjectToObject,
@@ -31,6 +32,7 @@ __export(objUtils_exports, {
31
32
  sortObjectKeys: () => sortObjectKeys
32
33
  });
33
34
  module.exports = __toCommonJS(objUtils_exports);
35
+ var import_t_result = require("t-result");
34
36
 
35
37
  // src/arrayUtils.ts
36
38
  function sortBy(arr, sortByValue, props = "asc") {
@@ -115,9 +117,88 @@ function sortObjectKeys(obj, sortByFn, options) {
115
117
  sortBy(typedObjectEntries(obj), sortByFn, options)
116
118
  );
117
119
  }
120
+ function getValueFromPath(obj, path) {
121
+ if (!path.trim()) {
122
+ return import_t_result.Result.err(new Error("Path cannot be empty"));
123
+ }
124
+ const segments = parsePath(path);
125
+ let current = obj;
126
+ for (let i = 0; i < segments.length; i++) {
127
+ const segment = segments[i];
128
+ if (!segment) {
129
+ return import_t_result.Result.err(new Error("Invalid empty segment in path"));
130
+ }
131
+ if (current == null) {
132
+ return import_t_result.Result.err(
133
+ new Error(`Cannot access property '${segment}' on null or undefined`)
134
+ );
135
+ }
136
+ if (isNumericString(segment)) {
137
+ if (!Array.isArray(current)) {
138
+ return import_t_result.Result.err(
139
+ new Error(
140
+ `Cannot access array index '${segment}' on non-array value`
141
+ )
142
+ );
143
+ }
144
+ const index = parseInt(segment, 10);
145
+ if (index < 0 || index >= current.length) {
146
+ return import_t_result.Result.err(new Error(`Array index '${index}' out of bounds`));
147
+ }
148
+ current = current[index];
149
+ } else {
150
+ if (typeof current !== "object" || current === null) {
151
+ return import_t_result.Result.err(
152
+ new Error(`Cannot access property '${segment}' on non-object value`)
153
+ );
154
+ }
155
+ if (!(segment in current)) {
156
+ return import_t_result.Result.err(new Error(`Property '${segment}' not found`));
157
+ }
158
+ current = current[segment];
159
+ }
160
+ }
161
+ return import_t_result.Result.ok(current);
162
+ }
163
+ function parsePath(path) {
164
+ const segments = [];
165
+ let current = "";
166
+ let inBrackets = false;
167
+ for (let i = 0; i < path.length; i++) {
168
+ const char = path[i];
169
+ if (char === "[") {
170
+ if (current) {
171
+ segments.push(current);
172
+ current = "";
173
+ }
174
+ inBrackets = true;
175
+ } else if (char === "]") {
176
+ if (inBrackets && current) {
177
+ segments.push(current);
178
+ current = "";
179
+ }
180
+ inBrackets = false;
181
+ } else if (char === "." && !inBrackets) {
182
+ if (current) {
183
+ segments.push(current);
184
+ current = "";
185
+ }
186
+ } else {
187
+ current += char;
188
+ }
189
+ }
190
+ if (current) {
191
+ segments.push(current);
192
+ }
193
+ return segments;
194
+ }
195
+ function isNumericString(str) {
196
+ return /^\d+$/.test(str);
197
+ }
118
198
  // Annotate the CommonJS export names for ESM import in node:
119
199
  0 && (module.exports = {
120
200
  filterObjectKeys,
201
+ getValueFromPath,
121
202
  looseGetObjectProperty,
122
203
  mapArrayToObject,
123
204
  mapObjectToObject,
@@ -1,3 +1,4 @@
1
+ import { Result } from 't-result';
1
2
  import { SortByValueFn, SortByProps } from './arrayUtils.cjs';
2
3
  import { MakeUndefinedKeysOptional } from './typeUtils.cjs';
3
4
 
@@ -14,5 +15,6 @@ declare function looseGetObjectProperty<T extends Record<string, unknown>>(obj:
14
15
  declare function rejectObjUndefinedValues<T extends Record<string, unknown>>(obj: T): MakeUndefinedKeysOptional<T>;
15
16
  declare function filterObjectKeys<T extends Record<string, unknown>>(obj: T, predicate: (key: keyof T, value: T[keyof T]) => boolean): Partial<T>;
16
17
  declare function sortObjectKeys<T extends Record<string, unknown>>(obj: T, sortByFn: SortByValueFn<[key: keyof T, value: T[keyof T]]>, options?: SortByProps): T;
18
+ declare function getValueFromPath(obj: Record<string, unknown>, path: string): Result<unknown, Error>;
17
19
 
18
- export { filterObjectKeys, looseGetObjectProperty, mapArrayToObject, mapObjectToObject, objectTypedEntries, omit, pick, rejectObjUndefinedValues, sortObjectKeys };
20
+ export { filterObjectKeys, getValueFromPath, looseGetObjectProperty, mapArrayToObject, mapObjectToObject, objectTypedEntries, omit, pick, rejectObjUndefinedValues, sortObjectKeys };
@@ -1,3 +1,4 @@
1
+ import { Result } from 't-result';
1
2
  import { SortByValueFn, SortByProps } from './arrayUtils.js';
2
3
  import { MakeUndefinedKeysOptional } from './typeUtils.js';
3
4
 
@@ -14,5 +15,6 @@ declare function looseGetObjectProperty<T extends Record<string, unknown>>(obj:
14
15
  declare function rejectObjUndefinedValues<T extends Record<string, unknown>>(obj: T): MakeUndefinedKeysOptional<T>;
15
16
  declare function filterObjectKeys<T extends Record<string, unknown>>(obj: T, predicate: (key: keyof T, value: T[keyof T]) => boolean): Partial<T>;
16
17
  declare function sortObjectKeys<T extends Record<string, unknown>>(obj: T, sortByFn: SortByValueFn<[key: keyof T, value: T[keyof T]]>, options?: SortByProps): T;
18
+ declare function getValueFromPath(obj: Record<string, unknown>, path: string): Result<unknown, Error>;
17
19
 
18
- export { filterObjectKeys, looseGetObjectProperty, mapArrayToObject, mapObjectToObject, objectTypedEntries, omit, pick, rejectObjUndefinedValues, sortObjectKeys };
20
+ export { filterObjectKeys, getValueFromPath, looseGetObjectProperty, mapArrayToObject, mapObjectToObject, objectTypedEntries, omit, pick, rejectObjUndefinedValues, sortObjectKeys };
package/dist/objUtils.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  filterObjectKeys,
3
+ getValueFromPath,
3
4
  looseGetObjectProperty,
4
5
  mapArrayToObject,
5
6
  mapObjectToObject,
@@ -8,13 +9,14 @@ import {
8
9
  pick,
9
10
  rejectObjUndefinedValues,
10
11
  sortObjectKeys
11
- } from "./chunk-Y45CE75W.js";
12
+ } from "./chunk-Y76LZUOB.js";
12
13
  import "./chunk-GMJTLFM6.js";
13
14
  import "./chunk-27AL66CH.js";
14
15
  import "./chunk-C2SVCIWE.js";
15
16
  import "./chunk-JF2MDHOJ.js";
16
17
  export {
17
18
  filterObjectKeys,
19
+ getValueFromPath,
18
20
  looseGetObjectProperty,
19
21
  mapArrayToObject,
20
22
  mapObjectToObject,
@@ -757,6 +757,7 @@ function clampMin(value, min) {
757
757
  }
758
758
 
759
759
  // src/objUtils.ts
760
+ var import_t_result = require("t-result");
760
761
  function pick(obj, keys) {
761
762
  const result = {};
762
763
  for (const key of keys) {
package/dist/testUtils.js CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  import {
5
5
  omit,
6
6
  pick
7
- } from "./chunk-Y45CE75W.js";
7
+ } from "./chunk-Y76LZUOB.js";
8
8
  import "./chunk-GMJTLFM6.js";
9
9
  import {
10
10
  filterObjectOrArrayKeys
@@ -56,5 +56,6 @@ type PickRequiredKeys<T> = {
56
56
  [K in keyof T]: undefined extends T[K] ? never : K;
57
57
  }[keyof T];
58
58
  type MakeUndefinedKeysOptional<T> = Prettify<Partial<Pick<T, PickUndefinedKeys<T>>> & Pick<T, PickRequiredKeys<T>>>;
59
+ type StringWithAutoComplete<T extends string> = T | (string & {});
59
60
 
60
- export type { DeepPrettify, DeepReplaceValue, DefaultSkipTransverseDeepReplace, IsAny, MakeUndefinedKeysOptional, NonPartial, ObjKeysWithValuesOfType, PartialPossiblyUndefinedValues, PartialRecord, Prettify };
61
+ export type { DeepPrettify, DeepReplaceValue, DefaultSkipTransverseDeepReplace, IsAny, MakeUndefinedKeysOptional, NonPartial, ObjKeysWithValuesOfType, PartialPossiblyUndefinedValues, PartialRecord, Prettify, StringWithAutoComplete };
@@ -56,5 +56,6 @@ type PickRequiredKeys<T> = {
56
56
  [K in keyof T]: undefined extends T[K] ? never : K;
57
57
  }[keyof T];
58
58
  type MakeUndefinedKeysOptional<T> = Prettify<Partial<Pick<T, PickUndefinedKeys<T>>> & Pick<T, PickRequiredKeys<T>>>;
59
+ type StringWithAutoComplete<T extends string> = T | (string & {});
59
60
 
60
- export type { DeepPrettify, DeepReplaceValue, DefaultSkipTransverseDeepReplace, IsAny, MakeUndefinedKeysOptional, NonPartial, ObjKeysWithValuesOfType, PartialPossiblyUndefinedValues, PartialRecord, Prettify };
61
+ export type { DeepPrettify, DeepReplaceValue, DefaultSkipTransverseDeepReplace, IsAny, MakeUndefinedKeysOptional, NonPartial, ObjKeysWithValuesOfType, PartialPossiblyUndefinedValues, PartialRecord, Prettify, StringWithAutoComplete };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Universal TypeScript utilities for browser and Node.js",
4
- "version": "3.50.0",
4
+ "version": "3.51.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist",
@@ -1,71 +0,0 @@
1
- import {
2
- typedObjectEntries
3
- } from "./chunk-GMJTLFM6.js";
4
- import {
5
- sortBy
6
- } from "./chunk-27AL66CH.js";
7
-
8
- // src/objUtils.ts
9
- function objectTypedEntries(obj) {
10
- return Object.entries(obj);
11
- }
12
- function pick(obj, keys) {
13
- const result = {};
14
- for (const key of keys) {
15
- result[key] = obj[key];
16
- }
17
- return result;
18
- }
19
- function mapArrayToObject(array, mapper) {
20
- return Object.fromEntries(array.map(mapper));
21
- }
22
- function mapObjectToObject(obj, mapper) {
23
- return Object.fromEntries(
24
- objectTypedEntries(obj).map(([key, value]) => mapper(key, value))
25
- );
26
- }
27
- function omit(obj, keys) {
28
- const result = {};
29
- for (const key of Object.keys(obj)) {
30
- if (!keys.includes(key)) {
31
- result[key] = obj[key];
32
- }
33
- }
34
- return result;
35
- }
36
- function looseGetObjectProperty(obj, key) {
37
- return obj[key];
38
- }
39
- function rejectObjUndefinedValues(obj) {
40
- const result = {};
41
- for (const key in obj) {
42
- if (obj[key] !== void 0) {
43
- result[key] = obj[key];
44
- }
45
- }
46
- return result;
47
- }
48
- function filterObjectKeys(obj, predicate) {
49
- return Object.fromEntries(
50
- Object.entries(obj).filter(
51
- ([key, value]) => predicate(key, value)
52
- )
53
- );
54
- }
55
- function sortObjectKeys(obj, sortByFn, options) {
56
- return Object.fromEntries(
57
- sortBy(typedObjectEntries(obj), sortByFn, options)
58
- );
59
- }
60
-
61
- export {
62
- objectTypedEntries,
63
- pick,
64
- mapArrayToObject,
65
- mapObjectToObject,
66
- omit,
67
- looseGetObjectProperty,
68
- rejectObjUndefinedValues,
69
- filterObjectKeys,
70
- sortObjectKeys
71
- };