@etsoo/shared 1.2.73 → 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.
@@ -1,4 +1,4 @@
1
- import { DataTypes, IdType } from "./DataTypes";
1
+ import { DataTypes } from "./DataTypes";
2
2
  declare global {
3
3
  interface Array<T> {
4
4
  /**
@@ -49,7 +49,7 @@ declare global {
49
49
  * @param add If true, add the item, otherwise remove it
50
50
  * @param idField If item is an object, use this field to check for existence
51
51
  */
52
- toggleItem(item: T extends object ? T | IdType : T, add: boolean, idField?: T extends object ? keyof T : never): Array<T>;
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>;
53
53
  /**
54
54
  * Make all items are unique
55
55
  * @param this Input array
@@ -23,7 +23,8 @@ Array.prototype.toggleItem = function (item, add, idField) {
23
23
  });
24
24
  if (add) {
25
25
  if (index < 0) {
26
- // Ignore type checking
26
+ // Ignore type checking as the type assertion is ready
27
+ // when add is true, item should be T, not a field value
27
28
  this.push(item);
28
29
  }
29
30
  }
@@ -1,4 +1,4 @@
1
- import { DataTypes, IdType } from "./DataTypes";
1
+ import { DataTypes } from "./DataTypes";
2
2
  declare global {
3
3
  interface Array<T> {
4
4
  /**
@@ -49,7 +49,7 @@ declare global {
49
49
  * @param add If true, add the item, otherwise remove it
50
50
  * @param idField If item is an object, use this field to check for existence
51
51
  */
52
- toggleItem(item: T extends object ? T | IdType : T, add: boolean, idField?: T extends object ? keyof T : never): Array<T>;
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>;
53
53
  /**
54
54
  * Make all items are unique
55
55
  * @param this Input array
@@ -17,7 +17,8 @@ Array.prototype.toggleItem = function (item, add, idField) {
17
17
  });
18
18
  if (add) {
19
19
  if (index < 0) {
20
- // Ignore type checking
20
+ // Ignore type checking as the type assertion is ready
21
+ // when add is true, item should be T, not a field value
21
22
  this.push(item);
22
23
  }
23
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.2.73",
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",
package/src/ArrayUtils.ts CHANGED
@@ -83,10 +83,13 @@ declare global {
83
83
  * @param add If true, add the item, otherwise remove it
84
84
  * @param idField If item is an object, use this field to check for existence
85
85
  */
86
- toggleItem(
87
- item: T extends object ? T | IdType : T,
88
- add: boolean,
89
- idField?: T extends object ? keyof T : never
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
90
93
  ): Array<T>;
91
94
 
92
95
  /**
@@ -105,19 +108,23 @@ Array.prototype.different = function <T>(
105
108
  return ArrayUtils.differences(this, target, round);
106
109
  };
107
110
 
108
- Array.prototype.toggleItem = function <T>(
111
+ Array.prototype.toggleItem = function <
112
+ T,
113
+ const A extends true | false,
114
+ const F = T extends object ? keyof T : never
115
+ >(
109
116
  this: Array<T>,
110
- item: T extends object ? T | IdType : T,
111
- add: boolean,
112
- idField?: T extends object ? keyof T : never
117
+ item: T | (F extends keyof T ? (A extends true ? T : T | T[F]) : T),
118
+ add: A,
119
+ idField?: F
113
120
  ) {
114
121
  const isObject = typeof item === "object" && item !== null;
115
122
  const index = this.findIndex((i) => {
116
123
  if (idField) {
117
124
  if (isObject) {
118
- return i[idField] === (item as any)[idField];
125
+ return i[idField as keyof T] === (item as T)[idField as keyof T];
119
126
  } else {
120
- return i[idField] === item;
127
+ return i[idField as keyof T] === item;
121
128
  }
122
129
  }
123
130
  return isEqual(i, item);
@@ -125,7 +132,8 @@ Array.prototype.toggleItem = function <T>(
125
132
 
126
133
  if (add) {
127
134
  if (index < 0) {
128
- // Ignore type checking
135
+ // Ignore type checking as the type assertion is ready
136
+ // when add is true, item should be T, not a field value
129
137
  this.push(item as T);
130
138
  }
131
139
  } else {