@etsoo/shared 1.2.63 → 1.2.65

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
@@ -130,8 +130,6 @@ Data type definitions and type safe functions. ListItemType, ListItemType1 and L
130
130
  | Name | Description |
131
131
  | ------------------: | ---------------------------------------------------------------------- |
132
132
  | DataType | Data type enum |
133
- | AddAndEditType | Add and edit data type |
134
- | AddOrEditType | Add or edit conditional type |
135
133
  | addUrlParam | Add parameter to URL |
136
134
  | addUrlParams | Add parameters to URL |
137
135
  | Basic | Basic types, includes number, bigint, Date, boolean, string |
@@ -144,6 +142,7 @@ Data type definitions and type safe functions. ListItemType, ListItemType1 and L
144
142
  | CultureDefinition | Culture definition |
145
143
  | DI | Dynamic interface with multiple properties |
146
144
  | DIS | Dynamic interface with single property |
145
+ | EditType | Create edit type from adding type |
147
146
  | EnumBase | Enum base type |
148
147
  | EnumValue | Enum value type |
149
148
  | ExtendedEnum | Extended type enum |
@@ -202,39 +202,18 @@ test("Tests for jsonReplacer", () => {
202
202
  expect(json2).toBe('{"c":{"c2":false}}');
203
203
  });
204
204
 
205
- test("Tests for AddAndEditType", () => {
206
- type Entity = DataTypes.AddAndEditType<{
207
- id: number;
208
- name: string;
209
- age?: number;
210
- }>;
211
-
212
- const data1: Entity = { id: 1, name: "hello", changedFields: ["name"] };
213
- const data2: Entity = { id: undefined, name: "hello" };
214
- const data3: Entity = { name: "hello" };
215
-
216
- expect(data1.name).toBe(data2.name);
217
- expect(data2.name).toBe(data3.name);
218
- });
219
-
220
- test("Tests for AddOrEditType", () => {
221
- type Entity = {
222
- id: number;
205
+ test("Tests for EditType", () => {
206
+ type AddEntity = {
223
207
  name: string;
224
208
  age?: number;
225
209
  };
226
- type AddEntity = DataTypes.AddOrEditType<Entity, false>;
227
- type EditEntity = DataTypes.AddOrEditType<Entity, true>;
228
210
 
229
- const data1: AddEntity = { id: 1, name: "hello" };
230
- const data2: AddEntity = { id: undefined, name: "hello" };
231
- const data3: AddEntity = { name: "hello" };
211
+ type EditType = DataTypes.EditType<AddEntity>;
232
212
 
233
- const data4: EditEntity = { id: 1, name: "hello", changedFields: ["name"] };
213
+ const data1: EditType = { id: 1, name: "hello", changedFields: ["name"] };
214
+ const data2: EditType = { id: 1, name: "hello", age: 1 };
234
215
 
235
216
  expect(data1.name).toBe(data2.name);
236
- expect(data2.name).toBe(data3.name);
237
- expect(data3.name).toBe(data4.name);
238
217
  });
239
218
 
240
219
  test("Tests for BasicTemplate", () => {
@@ -117,6 +117,7 @@ test("Tests for getDataChanges", () => {
117
117
  data: { d1: 1, d2: false, d3: 1.2, d4: "Hello" },
118
118
  changedFields: ["gender", "brand", "date"]
119
119
  };
120
+
120
121
  const initData = {
121
122
  id: 1,
122
123
  name: "Name",
@@ -129,10 +130,26 @@ test("Tests for getDataChanges", () => {
129
130
  ids: [1, 2],
130
131
  data: { d1: 1, d3: 1.2, d4: "Hello", d2: false }
131
132
  };
133
+
132
134
  const fields = Utils.getDataChanges(input, initData);
133
135
  expect(fields).toStrictEqual(["gender", "brand", "amount"]);
134
136
  expect(input.price).toBeUndefined();
135
137
  expect(input.amount).toBeUndefined();
138
+
139
+ const input1 = {
140
+ id: 2,
141
+ name: "Name",
142
+ gender: "F",
143
+ brand: "",
144
+ price: "6.0",
145
+ amount: "",
146
+ enabled: true,
147
+ value: undefined,
148
+ date: new Date("2023/03/18"),
149
+ ids: [1, 2]
150
+ };
151
+ const fields1 = Utils.getDataChanges(input1, initData, ["brand", "date"]);
152
+ expect(fields1).toStrictEqual(["gender", "amount"]);
136
153
  });
137
154
 
138
155
  test("Tests for object array getDataChanges", () => {
@@ -145,7 +162,7 @@ test("Tests for object array getDataChanges", () => {
145
162
  ]
146
163
  };
147
164
  const initData = {
148
- id: 1,
165
+ id: 2,
149
166
  ids: [1],
150
167
  items: [
151
168
  { id: 1, label: "a" },
@@ -139,25 +139,10 @@ export declare namespace DataTypes {
139
139
  */
140
140
  type Placement = keyof typeof PlacementEnum;
141
141
  /**
142
- * Add and edit data type
143
- * ChangedFields for editing case
144
- */
145
- type AddAndEditType<T extends {
146
- [key in D]: IdType;
147
- }, D extends string = "id"> = (Omit<T, D> & {
148
- [key in D]?: undefined | never;
149
- }) | (Partial<T> & Readonly<Pick<T, D>> & {
150
- changedFields?: (keyof T & string)[];
151
- });
152
- /**
153
- * Add or edit conditional type
154
- * ChangedFields for editing case
142
+ * Edit type from adding type
155
143
  */
156
- type AddOrEditType<T extends {
157
- [key in D]: IdType;
158
- }, // Entity modal
159
- E extends boolean, // Editing or not
160
- D extends string = "id"> = E extends false ? Optional<T, D> : Partial<T> & Readonly<Pick<T, D>> & {
144
+ type EditType<T, I extends IdType = number> = Partial<T> & {
145
+ id: I;
161
146
  changedFields?: (keyof T & string)[];
162
147
  };
163
148
  /**
@@ -89,7 +89,7 @@ declare global {
89
89
  * Utilities
90
90
  */
91
91
  export declare namespace Utils {
92
- const IgnoredProperty: "changedFields";
92
+ const IgnoredProperties: readonly ["changedFields", "id"];
93
93
  /**
94
94
  * Add blank item to collection
95
95
  * @param options Options
@@ -159,7 +159,7 @@ export declare namespace Utils {
159
159
  * @param ignoreFields Ignore fields
160
160
  * @returns
161
161
  */
162
- export function getDataChanges<T extends object>(input: T, initData: object, ignoreFields?: string[]): Exclude<keyof T & string, typeof IgnoredProperty>[];
162
+ export function getDataChanges<T extends object, const I extends (keyof T & string)[]>(input: T, initData: object, ignoreFields?: I): Exclude<keyof T & string, (typeof ignoreFields)[number] | (typeof IgnoredProperties)[number]>[];
163
163
  /**
164
164
  * Get nested value from object
165
165
  * @param data Data
package/lib/cjs/Utils.js CHANGED
@@ -133,7 +133,7 @@ String.prototype.removeNonLetters = function () {
133
133
  */
134
134
  var Utils;
135
135
  (function (Utils) {
136
- const IgnoredProperty = "changedFields";
136
+ const IgnoredProperties = ["changedFields", "id"];
137
137
  /**
138
138
  * Add blank item to collection
139
139
  * @param options Options
@@ -260,12 +260,14 @@ var Utils;
260
260
  * @param ignoreFields Ignore fields
261
261
  * @returns
262
262
  */
263
- function getDataChanges(input, initData, ignoreFields = ["id"]) {
263
+ function getDataChanges(input, initData, ignoreFields = []) {
264
264
  // Changed fields
265
265
  const changes = [];
266
+ // Ignored fields
267
+ const allFields = [...ignoreFields, ...IgnoredProperties];
266
268
  Object.entries(input).forEach(([key, value]) => {
267
269
  // Ignore fields, no process
268
- if (key === IgnoredProperty || ignoreFields.includes(key))
270
+ if (allFields.includes(key))
269
271
  return;
270
272
  // Compare with init value
271
273
  const initValue = Reflect.get(initData, key);
@@ -139,25 +139,10 @@ export declare namespace DataTypes {
139
139
  */
140
140
  type Placement = keyof typeof PlacementEnum;
141
141
  /**
142
- * Add and edit data type
143
- * ChangedFields for editing case
144
- */
145
- type AddAndEditType<T extends {
146
- [key in D]: IdType;
147
- }, D extends string = "id"> = (Omit<T, D> & {
148
- [key in D]?: undefined | never;
149
- }) | (Partial<T> & Readonly<Pick<T, D>> & {
150
- changedFields?: (keyof T & string)[];
151
- });
152
- /**
153
- * Add or edit conditional type
154
- * ChangedFields for editing case
142
+ * Edit type from adding type
155
143
  */
156
- type AddOrEditType<T extends {
157
- [key in D]: IdType;
158
- }, // Entity modal
159
- E extends boolean, // Editing or not
160
- D extends string = "id"> = E extends false ? Optional<T, D> : Partial<T> & Readonly<Pick<T, D>> & {
144
+ type EditType<T, I extends IdType = number> = Partial<T> & {
145
+ id: I;
161
146
  changedFields?: (keyof T & string)[];
162
147
  };
163
148
  /**
@@ -89,7 +89,7 @@ declare global {
89
89
  * Utilities
90
90
  */
91
91
  export declare namespace Utils {
92
- const IgnoredProperty: "changedFields";
92
+ const IgnoredProperties: readonly ["changedFields", "id"];
93
93
  /**
94
94
  * Add blank item to collection
95
95
  * @param options Options
@@ -159,7 +159,7 @@ export declare namespace Utils {
159
159
  * @param ignoreFields Ignore fields
160
160
  * @returns
161
161
  */
162
- export function getDataChanges<T extends object>(input: T, initData: object, ignoreFields?: string[]): Exclude<keyof T & string, typeof IgnoredProperty>[];
162
+ export function getDataChanges<T extends object, const I extends (keyof T & string)[]>(input: T, initData: object, ignoreFields?: I): Exclude<keyof T & string, (typeof ignoreFields)[number] | (typeof IgnoredProperties)[number]>[];
163
163
  /**
164
164
  * Get nested value from object
165
165
  * @param data Data
package/lib/mjs/Utils.js CHANGED
@@ -127,7 +127,7 @@ String.prototype.removeNonLetters = function () {
127
127
  */
128
128
  export var Utils;
129
129
  (function (Utils) {
130
- const IgnoredProperty = "changedFields";
130
+ const IgnoredProperties = ["changedFields", "id"];
131
131
  /**
132
132
  * Add blank item to collection
133
133
  * @param options Options
@@ -254,12 +254,14 @@ export var Utils;
254
254
  * @param ignoreFields Ignore fields
255
255
  * @returns
256
256
  */
257
- function getDataChanges(input, initData, ignoreFields = ["id"]) {
257
+ function getDataChanges(input, initData, ignoreFields = []) {
258
258
  // Changed fields
259
259
  const changes = [];
260
+ // Ignored fields
261
+ const allFields = [...ignoreFields, ...IgnoredProperties];
260
262
  Object.entries(input).forEach(([key, value]) => {
261
263
  // Ignore fields, no process
262
- if (key === IgnoredProperty || ignoreFields.includes(key))
264
+ if (allFields.includes(key))
263
265
  return;
264
266
  // Compare with init value
265
267
  const initValue = Reflect.get(initData, key);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.2.63",
3
+ "version": "1.2.65",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -40,7 +40,7 @@
40
40
  "@vitejs/plugin-react": "^4.3.4",
41
41
  "jsdom": "^26.0.0",
42
42
  "typescript": "^5.8.2",
43
- "vitest": "^3.0.9"
43
+ "vitest": "^3.1.1"
44
44
  },
45
45
  "dependencies": {
46
46
  "lodash.isequal": "^4.5.0"
package/src/DataTypes.ts CHANGED
@@ -183,29 +183,12 @@ export namespace DataTypes {
183
183
  export type Placement = keyof typeof PlacementEnum;
184
184
 
185
185
  /**
186
- * Add and edit data type
187
- * ChangedFields for editing case
188
- */
189
- export type AddAndEditType<
190
- T extends { [key in D]: IdType },
191
- D extends string = "id"
192
- > =
193
- | (Omit<T, D> & { [key in D]?: undefined | never })
194
- | (Partial<T> &
195
- Readonly<Pick<T, D>> & { changedFields?: (keyof T & string)[] });
196
-
197
- /**
198
- * Add or edit conditional type
199
- * ChangedFields for editing case
200
- */
201
- export type AddOrEditType<
202
- T extends { [key in D]: IdType }, // Entity modal
203
- E extends boolean, // Editing or not
204
- D extends string = "id" // Default is 'id' field
205
- > = E extends false
206
- ? Optional<T, D>
207
- : Partial<T> &
208
- Readonly<Pick<T, D>> & { changedFields?: (keyof T & string)[] };
186
+ * Edit type from adding type
187
+ */
188
+ export type EditType<T, I extends IdType = number> = Partial<T> & {
189
+ id: I;
190
+ changedFields?: (keyof T & string)[];
191
+ };
209
192
 
210
193
  /**
211
194
  * Key collection, like { key1: {}, key2: {} }
package/src/Utils.ts CHANGED
@@ -261,7 +261,7 @@ String.prototype.removeNonLetters = function (this: string) {
261
261
  * Utilities
262
262
  */
263
263
  export namespace Utils {
264
- const IgnoredProperty = "changedFields" as const;
264
+ const IgnoredProperties = ["changedFields", "id"] as const;
265
265
 
266
266
  /**
267
267
  * Add blank item to collection
@@ -405,17 +405,29 @@ export namespace Utils {
405
405
  * @param ignoreFields Ignore fields
406
406
  * @returns
407
407
  */
408
- export function getDataChanges<T extends object>(
408
+ export function getDataChanges<
409
+ T extends object,
410
+ const I extends (keyof T & string)[]
411
+ >(
409
412
  input: T,
410
413
  initData: object,
411
- ignoreFields: string[] = ["id"]
412
- ): Exclude<keyof T & string, typeof IgnoredProperty>[] {
414
+ ignoreFields: I = [] as any
415
+ ): Exclude<
416
+ keyof T & string,
417
+ (typeof ignoreFields)[number] | (typeof IgnoredProperties)[number]
418
+ >[] {
413
419
  // Changed fields
414
- const changes: Exclude<keyof T & string, typeof IgnoredProperty>[] = [];
420
+ const changes: Exclude<
421
+ keyof T & string,
422
+ (typeof ignoreFields)[number] | (typeof IgnoredProperties)[number]
423
+ >[] = [];
424
+
425
+ // Ignored fields
426
+ const allFields: string[] = [...ignoreFields, ...IgnoredProperties];
415
427
 
416
428
  Object.entries(input).forEach(([key, value]) => {
417
429
  // Ignore fields, no process
418
- if (key === IgnoredProperty || ignoreFields.includes(key)) return;
430
+ if (allFields.includes(key)) return;
419
431
 
420
432
  // Compare with init value
421
433
  const initValue = Reflect.get(initData, key);