@etsoo/shared 1.2.72 → 1.2.74

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
+ });
@@ -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<const A extends true | false, const F = T extends object ? keyof T : never>(item: T | (F extends keyof T ? (A extends true ? T : T | T[F]) : T), add: A, idField?: F): Array<T>;
46
53
  /**
47
54
  * Make all items are unique
48
55
  * @param this Input array
@@ -8,6 +8,32 @@ 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 as the type assertion is ready
27
+ // when add is true, item should be T, not a field value
28
+ this.push(item);
29
+ }
30
+ }
31
+ else {
32
+ if (index >= 0)
33
+ this.splice(index, 1);
34
+ }
35
+ return this;
36
+ };
11
37
  Array.prototype.toUnique = function () {
12
38
  if (this.length === 0 || typeof this[0] !== "object")
13
39
  return Array.from(new Set(this));
@@ -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<const A extends true | false, const F = T extends object ? keyof T : never>(item: T | (F extends keyof T ? (A extends true ? T : T | T[F]) : T), add: A, idField?: F): Array<T>;
46
53
  /**
47
54
  * Make all items are unique
48
55
  * @param this Input array
@@ -2,6 +2,32 @@ 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 as the type assertion is ready
21
+ // when add is true, item should be T, not a field value
22
+ this.push(item);
23
+ }
24
+ }
25
+ else {
26
+ if (index >= 0)
27
+ this.splice(index, 1);
28
+ }
29
+ return this;
30
+ };
5
31
  Array.prototype.toUnique = function () {
6
32
  if (this.length === 0 || typeof this[0] !== "object")
7
33
  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.74",
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,21 @@ 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
+ const A extends true | false,
88
+ const F = T extends object ? keyof T : never
89
+ >(
90
+ item: T | (F extends keyof T ? (A extends true ? T : T | T[F]) : T),
91
+ add: A,
92
+ idField?: F
93
+ ): Array<T>;
94
+
80
95
  /**
81
96
  * Make all items are unique
82
97
  * @param this Input array
@@ -93,6 +108,41 @@ Array.prototype.different = function <T>(
93
108
  return ArrayUtils.differences(this, target, round);
94
109
  };
95
110
 
111
+ Array.prototype.toggleItem = function <
112
+ T,
113
+ const A extends true | false,
114
+ const F = T extends object ? keyof T : never
115
+ >(
116
+ this: Array<T>,
117
+ item: T | (F extends keyof T ? (A extends true ? T : T | T[F]) : T),
118
+ add: A,
119
+ idField?: F
120
+ ) {
121
+ const isObject = typeof item === "object" && item !== null;
122
+ const index = this.findIndex((i) => {
123
+ if (idField) {
124
+ if (isObject) {
125
+ return i[idField as keyof T] === (item as T)[idField as keyof T];
126
+ } else {
127
+ return i[idField as keyof T] === item;
128
+ }
129
+ }
130
+ return isEqual(i, item);
131
+ });
132
+
133
+ if (add) {
134
+ if (index < 0) {
135
+ // Ignore type checking as the type assertion is ready
136
+ // when add is true, item should be T, not a field value
137
+ this.push(item as T);
138
+ }
139
+ } else {
140
+ if (index >= 0) this.splice(index, 1);
141
+ }
142
+
143
+ return this;
144
+ };
145
+
96
146
  Array.prototype.toUnique = function <T>(this: Array<T>) {
97
147
  if (this.length === 0 || typeof this[0] !== "object")
98
148
  return Array.from(new Set(this));