@ls-stack/utils 3.59.0 → 3.61.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.
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/mutationUtils.ts
21
21
  var mutationUtils_exports = {};
22
22
  __export(mutationUtils_exports, {
23
+ getArrayMethodsFromProduce: () => getArrayMethodsFromProduce,
23
24
  updateObject: () => updateObject
24
25
  });
25
26
  module.exports = __toCommonJS(mutationUtils_exports);
@@ -97,6 +98,11 @@ function keepPrevIfUnchanged({
97
98
  return equalityFn(prev, newValue) ? prev : newValue;
98
99
  }
99
100
 
101
+ // src/typeGuards.ts
102
+ function isFunction(value) {
103
+ return typeof value === "function";
104
+ }
105
+
100
106
  // src/mutationUtils.ts
101
107
  function updateObject(object, updates) {
102
108
  if (!object || typeof object !== "object") {
@@ -112,7 +118,36 @@ function updateObject(object, updates) {
112
118
  }
113
119
  }
114
120
  }
121
+ function getArrayMethodsFromProduce(produceFn, getItemId) {
122
+ return {
123
+ add: (item) => produceFn((draft) => {
124
+ draft.push(item);
125
+ }),
126
+ remove: (id) => produceFn((draft) => {
127
+ const index = draft.findIndex((item) => getItemId(item) === id);
128
+ if (index !== -1) {
129
+ draft.splice(index, 1);
130
+ }
131
+ }),
132
+ update: (id, updateItem) => produceFn((draft) => {
133
+ const index = draft.findIndex((item2) => getItemId(item2) === id);
134
+ const item = draft[index];
135
+ if (!item) {
136
+ throw new Error(`Item with id ${id} not found`);
137
+ }
138
+ if (isFunction(updateItem)) {
139
+ const updatedItem = updateItem(item);
140
+ if (updatedItem) {
141
+ draft[index] = updatedItem;
142
+ }
143
+ } else {
144
+ updateObject(item, updateItem);
145
+ }
146
+ })
147
+ };
148
+ }
115
149
  // Annotate the CommonJS export names for ESM import in node:
116
150
  0 && (module.exports = {
151
+ getArrayMethodsFromProduce,
117
152
  updateObject
118
153
  });
@@ -6,5 +6,10 @@
6
6
  * @param updates - The new values to update the object with.
7
7
  */
8
8
  declare function updateObject<T extends Record<string, unknown>>(object: T | undefined | null, updates: Partial<T>): void;
9
+ declare function getArrayMethodsFromProduce<T extends Record<string, unknown>>(produceFn: (cb: (newVal: T[]) => void | T[]) => T[] | void, getItemId: (item: T) => string): {
10
+ add: (item: T) => void | T[];
11
+ remove: (id: string) => void | T[];
12
+ update: (id: string, updateItem: ((draftItem: T) => T | void) | Partial<T>) => void | T[];
13
+ };
9
14
 
10
- export { updateObject };
15
+ export { getArrayMethodsFromProduce, updateObject };
@@ -6,5 +6,10 @@
6
6
  * @param updates - The new values to update the object with.
7
7
  */
8
8
  declare function updateObject<T extends Record<string, unknown>>(object: T | undefined | null, updates: Partial<T>): void;
9
+ declare function getArrayMethodsFromProduce<T extends Record<string, unknown>>(produceFn: (cb: (newVal: T[]) => void | T[]) => T[] | void, getItemId: (item: T) => string): {
10
+ add: (item: T) => void | T[];
11
+ remove: (id: string) => void | T[];
12
+ update: (id: string, updateItem: ((draftItem: T) => T | void) | Partial<T>) => void | T[];
13
+ };
9
14
 
10
- export { updateObject };
15
+ export { getArrayMethodsFromProduce, updateObject };
@@ -2,6 +2,9 @@ import {
2
2
  keepPrevIfUnchanged
3
3
  } from "./chunk-QQS7I7ZL.js";
4
4
  import "./chunk-JQFUKJU5.js";
5
+ import {
6
+ isFunction
7
+ } from "./chunk-JF2MDHOJ.js";
5
8
 
6
9
  // src/mutationUtils.ts
7
10
  function updateObject(object, updates) {
@@ -18,6 +21,35 @@ function updateObject(object, updates) {
18
21
  }
19
22
  }
20
23
  }
24
+ function getArrayMethodsFromProduce(produceFn, getItemId) {
25
+ return {
26
+ add: (item) => produceFn((draft) => {
27
+ draft.push(item);
28
+ }),
29
+ remove: (id) => produceFn((draft) => {
30
+ const index = draft.findIndex((item) => getItemId(item) === id);
31
+ if (index !== -1) {
32
+ draft.splice(index, 1);
33
+ }
34
+ }),
35
+ update: (id, updateItem) => produceFn((draft) => {
36
+ const index = draft.findIndex((item2) => getItemId(item2) === id);
37
+ const item = draft[index];
38
+ if (!item) {
39
+ throw new Error(`Item with id ${id} not found`);
40
+ }
41
+ if (isFunction(updateItem)) {
42
+ const updatedItem = updateItem(item);
43
+ if (updatedItem) {
44
+ draft[index] = updatedItem;
45
+ }
46
+ } else {
47
+ updateObject(item, updateItem);
48
+ }
49
+ })
50
+ };
51
+ }
21
52
  export {
53
+ getArrayMethodsFromProduce,
22
54
  updateObject
23
55
  };
@@ -1,6 +1,7 @@
1
1
  import { Result } from 't-result';
2
2
  import { SortByValueFn, SortByProps } from './arrayUtils.cjs';
3
3
  import { MakeUndefinedKeysOptional } from './typeUtils.cjs';
4
+ import { AddPrefixToObjKeys, AddSuffixToObjKeys } from './typingUtils.cjs';
4
5
 
5
6
  /**
6
7
  * @deprecated Use typedObjectEntries from @ls-stack/utils/typingFnUtils instead
@@ -21,11 +22,7 @@ declare function filterObjectKeys<T extends Record<string, unknown>>(obj: T, pre
21
22
  declare function sortObjectKeys<T extends Record<string, unknown>>(obj: T, sortByFn: SortByValueFn<[key: keyof T, value: T[keyof T]]>, options?: SortByProps): T;
22
23
  declare function getValueFromPath(obj: Record<string, unknown>, path: string): Result<unknown, Error>;
23
24
  declare function getObjPropertyOrInsert<T extends Record<string, any>, K extends keyof T>(obj: T, prop: K, insertValue: () => Exclude<T[K], undefined>): Exclude<T[K], undefined>;
24
- declare function addPrefixToObjKeys<T extends Record<string, unknown>, P extends string>(obj: T, prefix: P): {
25
- [K in keyof T & string as `${P}${K}`]: T[K];
26
- };
27
- declare function addSuffixToObjKeys<T extends Record<string, unknown>, S extends string>(obj: T, suffix: S): {
28
- [K in keyof T & string as `${K}${S}`]: T[K];
29
- };
25
+ declare function addPrefixToObjKeys<T extends Record<string, unknown>, P extends string>(obj: T, prefix: P): AddPrefixToObjKeys<T, P>;
26
+ declare function addSuffixToObjKeys<T extends Record<string, unknown>, S extends string>(obj: T, suffix: S): AddSuffixToObjKeys<T, S>;
30
27
 
31
28
  export { addPrefixToObjKeys, addSuffixToObjKeys, filterObjectKeys, getObjPropertyOrInsert, getValueFromPath, looseGetObjectProperty, mapArrToObj, mapArrayToObject, mapObjToObj, mapObjectToObject, objectTypedEntries, omit, pick, rejectObjUndefinedValues, sortObjectKeys };
@@ -1,6 +1,7 @@
1
1
  import { Result } from 't-result';
2
2
  import { SortByValueFn, SortByProps } from './arrayUtils.js';
3
3
  import { MakeUndefinedKeysOptional } from './typeUtils.js';
4
+ import { AddPrefixToObjKeys, AddSuffixToObjKeys } from './typingUtils.js';
4
5
 
5
6
  /**
6
7
  * @deprecated Use typedObjectEntries from @ls-stack/utils/typingFnUtils instead
@@ -21,11 +22,7 @@ declare function filterObjectKeys<T extends Record<string, unknown>>(obj: T, pre
21
22
  declare function sortObjectKeys<T extends Record<string, unknown>>(obj: T, sortByFn: SortByValueFn<[key: keyof T, value: T[keyof T]]>, options?: SortByProps): T;
22
23
  declare function getValueFromPath(obj: Record<string, unknown>, path: string): Result<unknown, Error>;
23
24
  declare function getObjPropertyOrInsert<T extends Record<string, any>, K extends keyof T>(obj: T, prop: K, insertValue: () => Exclude<T[K], undefined>): Exclude<T[K], undefined>;
24
- declare function addPrefixToObjKeys<T extends Record<string, unknown>, P extends string>(obj: T, prefix: P): {
25
- [K in keyof T & string as `${P}${K}`]: T[K];
26
- };
27
- declare function addSuffixToObjKeys<T extends Record<string, unknown>, S extends string>(obj: T, suffix: S): {
28
- [K in keyof T & string as `${K}${S}`]: T[K];
29
- };
25
+ declare function addPrefixToObjKeys<T extends Record<string, unknown>, P extends string>(obj: T, prefix: P): AddPrefixToObjKeys<T, P>;
26
+ declare function addSuffixToObjKeys<T extends Record<string, unknown>, S extends string>(obj: T, suffix: S): AddSuffixToObjKeys<T, S>;
30
27
 
31
28
  export { addPrefixToObjKeys, addSuffixToObjKeys, filterObjectKeys, getObjPropertyOrInsert, getValueFromPath, looseGetObjectProperty, mapArrToObj, mapArrayToObject, mapObjToObj, mapObjectToObject, objectTypedEntries, omit, pick, rejectObjUndefinedValues, sortObjectKeys };
@@ -25,5 +25,11 @@ type Prettify<T extends Record<string, unknown>> = {
25
25
  type DeepPrettify<T> = {
26
26
  [K in keyof T]: T[K] extends Record<string, unknown> ? DeepPrettify<T[K]> : T[K];
27
27
  } & {};
28
+ type AddPrefixToObjKeys<T extends Record<string, unknown>, Prefix extends string> = {
29
+ [K in keyof T & string as `${Prefix}${K}`]: T[K];
30
+ };
31
+ type AddSuffixToObjKeys<T extends Record<string, unknown>, Suffix extends string> = {
32
+ [K in keyof T & string as `${K}${Suffix}`]: T[K];
33
+ };
28
34
 
29
- export type { DeepPrettify, IsAny, NonPartial, ObjKeysWithValuesOfType, PartialRecord, Prettify };
35
+ export type { AddPrefixToObjKeys, AddSuffixToObjKeys, DeepPrettify, IsAny, NonPartial, ObjKeysWithValuesOfType, PartialRecord, Prettify };
@@ -25,5 +25,11 @@ type Prettify<T extends Record<string, unknown>> = {
25
25
  type DeepPrettify<T> = {
26
26
  [K in keyof T]: T[K] extends Record<string, unknown> ? DeepPrettify<T[K]> : T[K];
27
27
  } & {};
28
+ type AddPrefixToObjKeys<T extends Record<string, unknown>, Prefix extends string> = {
29
+ [K in keyof T & string as `${Prefix}${K}`]: T[K];
30
+ };
31
+ type AddSuffixToObjKeys<T extends Record<string, unknown>, Suffix extends string> = {
32
+ [K in keyof T & string as `${K}${Suffix}`]: T[K];
33
+ };
28
34
 
29
- export type { DeepPrettify, IsAny, NonPartial, ObjKeysWithValuesOfType, PartialRecord, Prettify };
35
+ export type { AddPrefixToObjKeys, AddSuffixToObjKeys, DeepPrettify, IsAny, NonPartial, ObjKeysWithValuesOfType, PartialRecord, Prettify };
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.59.0",
4
+ "version": "3.61.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist",