@ls-stack/utils 3.60.0 → 3.62.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
  };
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/regexUtils.ts
21
+ var regexUtils_exports = {};
22
+ __export(regexUtils_exports, {
23
+ escapeRegExp: () => escapeRegExp,
24
+ getRegexMatchAll: () => getRegexMatchAll,
25
+ getRegexMatches: () => getRegexMatches
26
+ });
27
+ module.exports = __toCommonJS(regexUtils_exports);
28
+ function getRegexMatches(string, regex) {
29
+ const [fullMatch, ...groups] = regex.exec(string) || [void 0];
30
+ return { groups, fullMatch };
31
+ }
32
+ function* getRegexMatchAll(str, regexp) {
33
+ const flags = regexp.global ? regexp.flags : `${regexp.flags}g`;
34
+ const re = new RegExp(regexp, flags);
35
+ let match;
36
+ let lastIndex = 0;
37
+ while (match = re.exec(str)) {
38
+ const [fullMatch, ...groups] = match;
39
+ const prevLastIndex = lastIndex;
40
+ lastIndex = re.lastIndex;
41
+ yield {
42
+ groups,
43
+ fullMatch,
44
+ namedGroups: match.groups,
45
+ start: match.index,
46
+ end: lastIndex,
47
+ prevEnd: prevLastIndex
48
+ };
49
+ }
50
+ }
51
+ var SPECIAL_REGEX_CHARS = /[.*+?^${}()|[\]\\]/g;
52
+ function escapeRegExp(value) {
53
+ return value.replace(SPECIAL_REGEX_CHARS, "\\$&");
54
+ }
55
+ // Annotate the CommonJS export names for ESM import in node:
56
+ 0 && (module.exports = {
57
+ escapeRegExp,
58
+ getRegexMatchAll,
59
+ getRegexMatches
60
+ });
@@ -0,0 +1,17 @@
1
+ declare function getRegexMatches(string: string, regex: RegExp): {
2
+ groups: (string | undefined)[];
3
+ fullMatch: string | undefined;
4
+ };
5
+ declare function getRegexMatchAll(str: string, regexp: RegExp): Generator<{
6
+ groups: (string | undefined)[];
7
+ fullMatch: string;
8
+ namedGroups: {
9
+ [key: string]: string;
10
+ } | undefined;
11
+ start: number;
12
+ end: number;
13
+ prevEnd: number;
14
+ }, void, unknown>;
15
+ declare function escapeRegExp(value: string): string;
16
+
17
+ export { escapeRegExp, getRegexMatchAll, getRegexMatches };
@@ -0,0 +1,17 @@
1
+ declare function getRegexMatches(string: string, regex: RegExp): {
2
+ groups: (string | undefined)[];
3
+ fullMatch: string | undefined;
4
+ };
5
+ declare function getRegexMatchAll(str: string, regexp: RegExp): Generator<{
6
+ groups: (string | undefined)[];
7
+ fullMatch: string;
8
+ namedGroups: {
9
+ [key: string]: string;
10
+ } | undefined;
11
+ start: number;
12
+ end: number;
13
+ prevEnd: number;
14
+ }, void, unknown>;
15
+ declare function escapeRegExp(value: string): string;
16
+
17
+ export { escapeRegExp, getRegexMatchAll, getRegexMatches };
@@ -0,0 +1,33 @@
1
+ // src/regexUtils.ts
2
+ function getRegexMatches(string, regex) {
3
+ const [fullMatch, ...groups] = regex.exec(string) || [void 0];
4
+ return { groups, fullMatch };
5
+ }
6
+ function* getRegexMatchAll(str, regexp) {
7
+ const flags = regexp.global ? regexp.flags : `${regexp.flags}g`;
8
+ const re = new RegExp(regexp, flags);
9
+ let match;
10
+ let lastIndex = 0;
11
+ while (match = re.exec(str)) {
12
+ const [fullMatch, ...groups] = match;
13
+ const prevLastIndex = lastIndex;
14
+ lastIndex = re.lastIndex;
15
+ yield {
16
+ groups,
17
+ fullMatch,
18
+ namedGroups: match.groups,
19
+ start: match.index,
20
+ end: lastIndex,
21
+ prevEnd: prevLastIndex
22
+ };
23
+ }
24
+ }
25
+ var SPECIAL_REGEX_CHARS = /[.*+?^${}()|[\]\\]/g;
26
+ function escapeRegExp(value) {
27
+ return value.replace(SPECIAL_REGEX_CHARS, "\\$&");
28
+ }
29
+ export {
30
+ escapeRegExp,
31
+ getRegexMatchAll,
32
+ getRegexMatches
33
+ };
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.62.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist",
@@ -152,6 +152,10 @@
152
152
  "import": "./dist/promiseUtils.js",
153
153
  "require": "./dist/promiseUtils.cjs"
154
154
  },
155
+ "./regexUtils": {
156
+ "import": "./dist/regexUtils.js",
157
+ "require": "./dist/regexUtils.cjs"
158
+ },
155
159
  "./retryOnError": {
156
160
  "import": "./dist/retryOnError.js",
157
161
  "require": "./dist/retryOnError.cjs"