@etsoo/shared 1.2.72 → 1.2.73

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
@@ -122,6 +122,7 @@ Array related utilities
122
122
  | minItem | Get min field value item |
123
123
  | remove | Remove items by value or condition |
124
124
  | sum | Sum number items or number item properties |
125
+ | toggleItem | Toggle item in array |
125
126
  | toUnique | Make all items are unique |
126
127
 
127
128
  ## DataTypes
@@ -157,3 +157,39 @@ test("Tests for sortIds 2", () => {
157
157
  "SGD"
158
158
  ]);
159
159
  });
160
+
161
+ test("Tests for toggleItem", () => {
162
+ const items = [1, 2, 3];
163
+ items.toggleItem(2, false);
164
+ expect(items).toStrictEqual([1, 3]);
165
+
166
+ items.toggleItem(4, true);
167
+ expect(items).toStrictEqual([1, 3, 4]);
168
+
169
+ items.toggleItem(1, false);
170
+ expect(items).toStrictEqual([3, 4]);
171
+
172
+ items.toggleItem(3, true);
173
+ expect(items).toStrictEqual([3, 4]);
174
+ });
175
+
176
+ test("Tests for toggleItem with object", () => {
177
+ const items = [
178
+ { id: 1, label: "a" },
179
+ { id: 2, label: "b" }
180
+ ];
181
+ items.toggleItem({ id: 2, label: "b" }, false);
182
+ expect(items).toStrictEqual([{ id: 1, label: "a" }]);
183
+
184
+ items.toggleItem({ id: 3, label: "c" }, true, "id");
185
+ expect(items).toStrictEqual([
186
+ { id: 1, label: "a" },
187
+ { id: 3, label: "c" }
188
+ ]);
189
+
190
+ items.toggleItem(1, false, "id");
191
+ expect(items).toStrictEqual([{ id: 3, label: "c" }]);
192
+
193
+ items.toggleItem({ id: 3, label: "c" }, true, "id");
194
+ expect(items).toStrictEqual([{ id: 3, label: "c" }]);
195
+ });
@@ -1,4 +1,4 @@
1
- import { DataTypes } from "./DataTypes";
1
+ import { DataTypes, IdType } from "./DataTypes";
2
2
  declare global {
3
3
  interface Array<T> {
4
4
  /**
@@ -43,6 +43,13 @@ declare global {
43
43
  * @param field Object field to calculate
44
44
  */
45
45
  sum(...field: T extends number ? [undefined?] : T extends object ? [DataTypes.Keys<T, number>] : [never]): number;
46
+ /**
47
+ * Toggle item in array
48
+ * @param item Item to toggle
49
+ * @param add If true, add the item, otherwise remove it
50
+ * @param idField If item is an object, use this field to check for existence
51
+ */
52
+ toggleItem(item: T extends object ? T | IdType : T, add: boolean, idField?: T extends object ? keyof T : never): Array<T>;
46
53
  /**
47
54
  * Make all items are unique
48
55
  * @param this Input array
@@ -8,6 +8,31 @@ const lodash_isequal_1 = __importDefault(require("lodash.isequal"));
8
8
  Array.prototype.different = function (target, round) {
9
9
  return ArrayUtils.differences(this, target, round);
10
10
  };
11
+ Array.prototype.toggleItem = function (item, add, idField) {
12
+ const isObject = typeof item === "object" && item !== null;
13
+ const index = this.findIndex((i) => {
14
+ if (idField) {
15
+ if (isObject) {
16
+ return i[idField] === item[idField];
17
+ }
18
+ else {
19
+ return i[idField] === item;
20
+ }
21
+ }
22
+ return (0, lodash_isequal_1.default)(i, item);
23
+ });
24
+ if (add) {
25
+ if (index < 0) {
26
+ // Ignore type checking
27
+ this.push(item);
28
+ }
29
+ }
30
+ else {
31
+ if (index >= 0)
32
+ this.splice(index, 1);
33
+ }
34
+ return this;
35
+ };
11
36
  Array.prototype.toUnique = function () {
12
37
  if (this.length === 0 || typeof this[0] !== "object")
13
38
  return Array.from(new Set(this));
@@ -1,4 +1,4 @@
1
- import { DataTypes } from "./DataTypes";
1
+ import { DataTypes, IdType } from "./DataTypes";
2
2
  declare global {
3
3
  interface Array<T> {
4
4
  /**
@@ -43,6 +43,13 @@ declare global {
43
43
  * @param field Object field to calculate
44
44
  */
45
45
  sum(...field: T extends number ? [undefined?] : T extends object ? [DataTypes.Keys<T, number>] : [never]): number;
46
+ /**
47
+ * Toggle item in array
48
+ * @param item Item to toggle
49
+ * @param add If true, add the item, otherwise remove it
50
+ * @param idField If item is an object, use this field to check for existence
51
+ */
52
+ toggleItem(item: T extends object ? T | IdType : T, add: boolean, idField?: T extends object ? keyof T : never): Array<T>;
46
53
  /**
47
54
  * Make all items are unique
48
55
  * @param this Input array
@@ -2,6 +2,31 @@ import isEqual from "lodash.isequal";
2
2
  Array.prototype.different = function (target, round) {
3
3
  return ArrayUtils.differences(this, target, round);
4
4
  };
5
+ Array.prototype.toggleItem = function (item, add, idField) {
6
+ const isObject = typeof item === "object" && item !== null;
7
+ const index = this.findIndex((i) => {
8
+ if (idField) {
9
+ if (isObject) {
10
+ return i[idField] === item[idField];
11
+ }
12
+ else {
13
+ return i[idField] === item;
14
+ }
15
+ }
16
+ return isEqual(i, item);
17
+ });
18
+ if (add) {
19
+ if (index < 0) {
20
+ // Ignore type checking
21
+ this.push(item);
22
+ }
23
+ }
24
+ else {
25
+ if (index >= 0)
26
+ this.splice(index, 1);
27
+ }
28
+ return this;
29
+ };
5
30
  Array.prototype.toUnique = function () {
6
31
  if (this.length === 0 || typeof this[0] !== "object")
7
32
  return Array.from(new Set(this));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.2.72",
3
+ "version": "1.2.73",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -37,10 +37,10 @@
37
37
  "homepage": "https://github.com/ETSOO/Shared#readme",
38
38
  "devDependencies": {
39
39
  "@types/lodash.isequal": "^4.5.8",
40
- "@vitejs/plugin-react": "^4.4.1",
40
+ "@vitejs/plugin-react": "^4.5.0",
41
41
  "jsdom": "^26.1.0",
42
42
  "typescript": "^5.8.3",
43
- "vitest": "^3.1.3"
43
+ "vitest": "^3.1.4"
44
44
  },
45
45
  "dependencies": {
46
46
  "lodash.isequal": "^4.5.0"
package/src/ArrayUtils.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import isEqual from "lodash.isequal";
2
- import { DataTypes } from "./DataTypes";
2
+ import { DataTypes, IdType } from "./DataTypes";
3
3
 
4
4
  declare global {
5
5
  interface Array<T> {
@@ -77,6 +77,18 @@ declare global {
77
77
  : [never]
78
78
  ): number;
79
79
 
80
+ /**
81
+ * Toggle item in array
82
+ * @param item Item to toggle
83
+ * @param add If true, add the item, otherwise remove it
84
+ * @param idField If item is an object, use this field to check for existence
85
+ */
86
+ toggleItem(
87
+ item: T extends object ? T | IdType : T,
88
+ add: boolean,
89
+ idField?: T extends object ? keyof T : never
90
+ ): Array<T>;
91
+
80
92
  /**
81
93
  * Make all items are unique
82
94
  * @param this Input array
@@ -93,6 +105,36 @@ Array.prototype.different = function <T>(
93
105
  return ArrayUtils.differences(this, target, round);
94
106
  };
95
107
 
108
+ Array.prototype.toggleItem = function <T>(
109
+ this: Array<T>,
110
+ item: T extends object ? T | IdType : T,
111
+ add: boolean,
112
+ idField?: T extends object ? keyof T : never
113
+ ) {
114
+ const isObject = typeof item === "object" && item !== null;
115
+ const index = this.findIndex((i) => {
116
+ if (idField) {
117
+ if (isObject) {
118
+ return i[idField] === (item as any)[idField];
119
+ } else {
120
+ return i[idField] === item;
121
+ }
122
+ }
123
+ return isEqual(i, item);
124
+ });
125
+
126
+ if (add) {
127
+ if (index < 0) {
128
+ // Ignore type checking
129
+ this.push(item as T);
130
+ }
131
+ } else {
132
+ if (index >= 0) this.splice(index, 1);
133
+ }
134
+
135
+ return this;
136
+ };
137
+
96
138
  Array.prototype.toUnique = function <T>(this: Array<T>) {
97
139
  if (this.length === 0 || typeof this[0] !== "object")
98
140
  return Array.from(new Set(this));