@etsoo/shared 1.2.49 → 1.2.51

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/README.md CHANGED
@@ -109,6 +109,7 @@ Array related utilities
109
109
  |maxItem|Get max field value item|
110
110
  |min|Get min number item or number item property|
111
111
  |minItem|Get min field value item|
112
+ |remove|Remove items by value or condition|
112
113
  |sum|Sum number items or number item properties|
113
114
  |toUnique|Make all items are unique|
114
115
 
@@ -63,6 +63,33 @@ test('Tests for maxItem / minItem', () => {
63
63
  expect(emptyItems.maxItem('amount')).toBeUndefined();
64
64
  });
65
65
 
66
+ test('Tests for remove simple', () => {
67
+ const items = [1, 2, 3, 4, 5];
68
+ const result = items.remove(1, 5, (item) => item % 2 === 0);
69
+ expect(items).toStrictEqual([3]);
70
+ expect(result.length).toBe(4);
71
+ });
72
+
73
+ test('Tests for remove object', () => {
74
+ const item = { id: 4, label: 'd', amount: 9.66 };
75
+ const items = [
76
+ { id: 1, label: 'a', amount: 3.14 },
77
+ { id: 2, label: 'b', amount: 4.54 },
78
+ { id: 3, label: 'b', amount: 1.52 },
79
+ item
80
+ ];
81
+
82
+ const result = items.remove(
83
+ item,
84
+ (item) => item.id === 2,
85
+ (item) => item.amount <= 2
86
+ );
87
+
88
+ expect(items.length).toBe(1);
89
+ expect(items[0].id).toBe(1);
90
+ expect(result.length).toBe(3);
91
+ });
92
+
66
93
  test('Tests for sortIds 1', () => {
67
94
  const source = [
68
95
  {
@@ -27,6 +27,11 @@ declare global {
27
27
  * @param field Object field to calculate
28
28
  */
29
29
  minItem(field: T extends object ? DataTypes.Keys<T, number> : never): T | undefined;
30
+ /**
31
+ * Remove items by value or condition
32
+ * @param items Items to remove
33
+ */
34
+ remove(...items: ((T & (DataTypes.Basic | object)) | ((item: T) => boolean))[]): T[];
30
35
  /**
31
36
  * Sort by property
32
37
  * @param property Property
@@ -41,6 +41,29 @@ Array.prototype.minItem = function (field) {
41
41
  return undefined;
42
42
  return this.reduce((prev, curr) => prev[field] < curr[field] ? prev : curr);
43
43
  };
44
+ Array.prototype.remove = function (...items) {
45
+ const funs = [];
46
+ const results = [];
47
+ items.forEach((item) => {
48
+ if (typeof item === 'function') {
49
+ funs.push(item);
50
+ }
51
+ else {
52
+ // For object items, should be removed by reference, not by value
53
+ const index = this.indexOf(item);
54
+ if (index >= 0)
55
+ results.push(...this.splice(index, 1));
56
+ }
57
+ });
58
+ if (funs.length > 0) {
59
+ // Reduce check loops for performance
60
+ for (let i = this.length - 1; i >= 0; i--) {
61
+ if (funs.some((fun) => fun(this[i])))
62
+ results.push(...this.splice(i, 1));
63
+ }
64
+ }
65
+ return results;
66
+ };
44
67
  Array.prototype.sortByProperty = function (property, values) {
45
68
  return this.sort((a, b) => {
46
69
  const ai = values.indexOf(a[property]);
@@ -47,9 +47,7 @@ class NodeStorage {
47
47
  * Removes the key/value pair with the given key from the list associated with the object, if a key/value pair with the given key exists.
48
48
  */
49
49
  removeItem(key) {
50
- const index = this.source.findIndex((p) => p[0] === key);
51
- if (index != -1)
52
- this.source.splice(index, 1);
50
+ this.source.remove((p) => p[0] === key);
53
51
  }
54
52
  /**
55
53
  * Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.
@@ -27,6 +27,11 @@ declare global {
27
27
  * @param field Object field to calculate
28
28
  */
29
29
  minItem(field: T extends object ? DataTypes.Keys<T, number> : never): T | undefined;
30
+ /**
31
+ * Remove items by value or condition
32
+ * @param items Items to remove
33
+ */
34
+ remove(...items: ((T & (DataTypes.Basic | object)) | ((item: T) => boolean))[]): T[];
30
35
  /**
31
36
  * Sort by property
32
37
  * @param property Property
@@ -35,6 +35,29 @@ Array.prototype.minItem = function (field) {
35
35
  return undefined;
36
36
  return this.reduce((prev, curr) => prev[field] < curr[field] ? prev : curr);
37
37
  };
38
+ Array.prototype.remove = function (...items) {
39
+ const funs = [];
40
+ const results = [];
41
+ items.forEach((item) => {
42
+ if (typeof item === 'function') {
43
+ funs.push(item);
44
+ }
45
+ else {
46
+ // For object items, should be removed by reference, not by value
47
+ const index = this.indexOf(item);
48
+ if (index >= 0)
49
+ results.push(...this.splice(index, 1));
50
+ }
51
+ });
52
+ if (funs.length > 0) {
53
+ // Reduce check loops for performance
54
+ for (let i = this.length - 1; i >= 0; i--) {
55
+ if (funs.some((fun) => fun(this[i])))
56
+ results.push(...this.splice(i, 1));
57
+ }
58
+ }
59
+ return results;
60
+ };
38
61
  Array.prototype.sortByProperty = function (property, values) {
39
62
  return this.sort((a, b) => {
40
63
  const ai = values.indexOf(a[property]);
@@ -44,9 +44,7 @@ export class NodeStorage {
44
44
  * Removes the key/value pair with the given key from the list associated with the object, if a key/value pair with the given key exists.
45
45
  */
46
46
  removeItem(key) {
47
- const index = this.source.findIndex((p) => p[0] === key);
48
- if (index != -1)
49
- this.source.splice(index, 1);
47
+ this.source.remove((p) => p[0] === key);
50
48
  }
51
49
  /**
52
50
  * Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.2.49",
3
+ "version": "1.2.51",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
package/src/ArrayUtils.ts CHANGED
@@ -50,6 +50,17 @@ declare global {
50
50
  field: T extends object ? DataTypes.Keys<T, number> : never
51
51
  ): T | undefined;
52
52
 
53
+ /**
54
+ * Remove items by value or condition
55
+ * @param items Items to remove
56
+ */
57
+ remove(
58
+ ...items: (
59
+ | (T & (DataTypes.Basic | object))
60
+ | ((item: T) => boolean)
61
+ )[]
62
+ ): T[];
63
+
53
64
  /**
54
65
  * Sort by property
55
66
  * @param property Property
@@ -141,6 +152,33 @@ Array.prototype.minItem = function <T>(
141
152
  );
142
153
  };
143
154
 
155
+ Array.prototype.remove = function <T>(
156
+ this: Array<T>,
157
+ ...items: ((T & (DataTypes.Basic | object)) | ((item: T) => boolean))[]
158
+ ) {
159
+ const funs: ((item: T) => boolean)[] = [];
160
+ const results: T[] = [];
161
+ items.forEach((item) => {
162
+ if (typeof item === 'function') {
163
+ funs.push(item);
164
+ } else {
165
+ // For object items, should be removed by reference, not by value
166
+ const index = this.indexOf(item);
167
+ if (index >= 0) results.push(...this.splice(index, 1));
168
+ }
169
+ });
170
+
171
+ if (funs.length > 0) {
172
+ // Reduce check loops for performance
173
+ for (let i = this.length - 1; i >= 0; i--) {
174
+ if (funs.some((fun) => fun(this[i])))
175
+ results.push(...this.splice(i, 1));
176
+ }
177
+ }
178
+
179
+ return results;
180
+ };
181
+
144
182
  Array.prototype.sortByProperty = function <T, P extends keyof T>(
145
183
  this: Array<T>,
146
184
  property: P,
@@ -48,8 +48,7 @@ export class NodeStorage {
48
48
  * Removes the key/value pair with the given key from the list associated with the object, if a key/value pair with the given key exists.
49
49
  */
50
50
  removeItem(key: string): void {
51
- const index = this.source.findIndex((p) => p[0] === key);
52
- if (index != -1) this.source.splice(index, 1);
51
+ this.source.remove((p) => p[0] === key);
53
52
  }
54
53
 
55
54
  /**