@ls-stack/utils 3.60.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
  };
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.60.0",
4
+ "version": "3.61.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist",