@etsoo/shared 1.2.48 → 1.2.50

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,31 @@ 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
+ items.remove(1, 5, (item) => item % 2 === 0);
69
+ expect(items).toStrictEqual([3]);
70
+ });
71
+
72
+ test('Tests for remove object', () => {
73
+ const item = { id: 4, label: 'd', amount: 9.66 };
74
+ const items = [
75
+ { id: 1, label: 'a', amount: 3.14 },
76
+ { id: 2, label: 'b', amount: 4.54 },
77
+ { id: 3, label: 'b', amount: 1.52 },
78
+ item
79
+ ];
80
+
81
+ items.remove(
82
+ item,
83
+ (item) => item.id === 2,
84
+ (item) => item.amount <= 2
85
+ );
86
+
87
+ expect(items.length).toBe(1);
88
+ expect(items[0].id).toBe(1);
89
+ });
90
+
66
91
  test('Tests for sortIds 1', () => {
67
92
  const source = [
68
93
  {
@@ -46,6 +46,13 @@ describe('Tests for addUrlParams', () => {
46
46
  );
47
47
  });
48
48
 
49
+ const result22 = '/'.addUrlParams(data);
50
+ test('addUrlParams with relative path', () => {
51
+ expect(result22).toBe(
52
+ '/?a=a&b=false&c=123&d=2022-01-28T10%3A00%3A00.000Z&e=1&e=2&f=a&f=b&g='
53
+ );
54
+ });
55
+
49
56
  global.URL = undefined as any;
50
57
 
51
58
  const result3 = url.addUrlParams(data);
@@ -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))[]): void;
30
35
  /**
31
36
  * Sort by property
32
37
  * @param property Property
@@ -41,6 +41,27 @@ 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
+ items.forEach((item) => {
47
+ if (typeof item === 'function') {
48
+ funs.push(item);
49
+ }
50
+ else {
51
+ // For object items, should be removed by reference, not by value
52
+ const index = this.indexOf(item);
53
+ if (index >= 0)
54
+ this.splice(index, 1);
55
+ }
56
+ });
57
+ if (funs.length > 0) {
58
+ // Reduce check loops for performance
59
+ for (let i = this.length - 1; i >= 0; i--) {
60
+ if (funs.some((fun) => fun(this[i])))
61
+ this.splice(i, 1);
62
+ }
63
+ }
64
+ };
44
65
  Array.prototype.sortByProperty = function (property, values) {
45
66
  return this.sort((a, b) => {
46
67
  const ai = values.indexOf(a[property]);
package/lib/cjs/Utils.js CHANGED
@@ -23,7 +23,8 @@ String.prototype.addUrlParams = function (data, arrayFormat) {
23
23
  }
24
24
  return url + data;
25
25
  }
26
- if (typeof URL === 'undefined') {
26
+ // Simple check
27
+ if (typeof URL === 'undefined' || !this.includes('://')) {
27
28
  const params = Object.entries(data)
28
29
  .map(([key, value]) => {
29
30
  let v;
@@ -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))[]): void;
30
35
  /**
31
36
  * Sort by property
32
37
  * @param property Property
@@ -35,6 +35,27 @@ 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
+ items.forEach((item) => {
41
+ if (typeof item === 'function') {
42
+ funs.push(item);
43
+ }
44
+ else {
45
+ // For object items, should be removed by reference, not by value
46
+ const index = this.indexOf(item);
47
+ if (index >= 0)
48
+ this.splice(index, 1);
49
+ }
50
+ });
51
+ if (funs.length > 0) {
52
+ // Reduce check loops for performance
53
+ for (let i = this.length - 1; i >= 0; i--) {
54
+ if (funs.some((fun) => fun(this[i])))
55
+ this.splice(i, 1);
56
+ }
57
+ }
58
+ };
38
59
  Array.prototype.sortByProperty = function (property, values) {
39
60
  return this.sort((a, b) => {
40
61
  const ai = values.indexOf(a[property]);
package/lib/mjs/Utils.js CHANGED
@@ -17,7 +17,8 @@ String.prototype.addUrlParams = function (data, arrayFormat) {
17
17
  }
18
18
  return url + data;
19
19
  }
20
- if (typeof URL === 'undefined') {
20
+ // Simple check
21
+ if (typeof URL === 'undefined' || !this.includes('://')) {
21
22
  const params = Object.entries(data)
22
23
  .map(([key, value]) => {
23
24
  let v;
@@ -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.48",
3
+ "version": "1.2.50",
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
+ ): void;
63
+
53
64
  /**
54
65
  * Sort by property
55
66
  * @param property Property
@@ -141,6 +152,29 @@ 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
+ items.forEach((item) => {
161
+ if (typeof item === 'function') {
162
+ funs.push(item);
163
+ } else {
164
+ // For object items, should be removed by reference, not by value
165
+ const index = this.indexOf(item);
166
+ if (index >= 0) this.splice(index, 1);
167
+ }
168
+ });
169
+
170
+ if (funs.length > 0) {
171
+ // Reduce check loops for performance
172
+ for (let i = this.length - 1; i >= 0; i--) {
173
+ if (funs.some((fun) => fun(this[i]))) this.splice(i, 1);
174
+ }
175
+ }
176
+ };
177
+
144
178
  Array.prototype.sortByProperty = function <T, P extends keyof T>(
145
179
  this: Array<T>,
146
180
  property: P,
package/src/Utils.ts CHANGED
@@ -135,7 +135,8 @@ String.prototype.addUrlParams = function (
135
135
  return url + data;
136
136
  }
137
137
 
138
- if (typeof URL === 'undefined') {
138
+ // Simple check
139
+ if (typeof URL === 'undefined' || !this.includes('://')) {
139
140
  const params = Object.entries(data)
140
141
  .map(([key, value]) => {
141
142
  let v: string;
@@ -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
  /**