@etsoo/shared 1.2.64 → 1.2.66

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.
@@ -104,7 +104,7 @@ test("Tests for correctTypes", () => {
104
104
 
105
105
  test("Tests for getDataChanges", () => {
106
106
  const input = {
107
- id: 1,
107
+ id: 2,
108
108
  name: "Name",
109
109
  gender: "F",
110
110
  brand: "",
@@ -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,27 @@ 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
+ changedFields: ["gender", "brand", "date"]
151
+ };
152
+ const fields1 = Utils.getDataChanges(input1, initData, ["brand", "date"]);
153
+ expect(fields1).toStrictEqual(["id", "gender", "amount", "changedFields"]);
136
154
  });
137
155
 
138
156
  test("Tests for object array getDataChanges", () => {
@@ -145,7 +163,7 @@ test("Tests for object array getDataChanges", () => {
145
163
  ]
146
164
  };
147
165
  const initData = {
148
- id: 1,
166
+ id: 2,
149
167
  ids: [1],
150
168
  items: [
151
169
  { id: 1, label: "a" },
@@ -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
@@ -156,10 +156,10 @@ export declare namespace Utils {
156
156
  * Get data changed fields (ignored changedFields) with input data updated
157
157
  * @param input Input data
158
158
  * @param initData Initial data
159
- * @param ignoreFields Ignore fields
159
+ * @param ignoreFields Ignore fields, default is ['changedFields', 'id']
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)[] | undefined = undefined>(input: T, initData: object, ignoreFields?: I): Exclude<keyof T & string, I extends undefined ? (typeof IgnoredProperties)[number] : Exclude<I, undefined>[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
@@ -257,15 +257,17 @@ var Utils;
257
257
  * Get data changed fields (ignored changedFields) with input data updated
258
258
  * @param input Input data
259
259
  * @param initData Initial data
260
- * @param ignoreFields Ignore fields
260
+ * @param ignoreFields Ignore fields, default is ['changedFields', 'id']
261
261
  * @returns
262
262
  */
263
- function getDataChanges(input, initData, ignoreFields = ["id"]) {
263
+ function getDataChanges(input, initData, ignoreFields) {
264
+ // Default ignore fields
265
+ const fields = ignoreFields ?? IgnoredProperties;
264
266
  // Changed fields
265
267
  const changes = [];
266
268
  Object.entries(input).forEach(([key, value]) => {
267
269
  // Ignore fields, no process
268
- if (key === IgnoredProperty || ignoreFields.includes(key))
270
+ if (fields.includes(key))
269
271
  return;
270
272
  // Compare with init value
271
273
  const initValue = Reflect.get(initData, key);
@@ -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
@@ -156,10 +156,10 @@ export declare namespace Utils {
156
156
  * Get data changed fields (ignored changedFields) with input data updated
157
157
  * @param input Input data
158
158
  * @param initData Initial data
159
- * @param ignoreFields Ignore fields
159
+ * @param ignoreFields Ignore fields, default is ['changedFields', 'id']
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)[] | undefined = undefined>(input: T, initData: object, ignoreFields?: I): Exclude<keyof T & string, I extends undefined ? (typeof IgnoredProperties)[number] : Exclude<I, undefined>[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
@@ -251,15 +251,17 @@ export var Utils;
251
251
  * Get data changed fields (ignored changedFields) with input data updated
252
252
  * @param input Input data
253
253
  * @param initData Initial data
254
- * @param ignoreFields Ignore fields
254
+ * @param ignoreFields Ignore fields, default is ['changedFields', 'id']
255
255
  * @returns
256
256
  */
257
- function getDataChanges(input, initData, ignoreFields = ["id"]) {
257
+ function getDataChanges(input, initData, ignoreFields) {
258
+ // Default ignore fields
259
+ const fields = ignoreFields ?? IgnoredProperties;
258
260
  // Changed fields
259
261
  const changes = [];
260
262
  Object.entries(input).forEach(([key, value]) => {
261
263
  // Ignore fields, no process
262
- if (key === IgnoredProperty || ignoreFields.includes(key))
264
+ if (fields.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.64",
3
+ "version": "1.2.66",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
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
@@ -402,20 +402,36 @@ export namespace Utils {
402
402
  * Get data changed fields (ignored changedFields) with input data updated
403
403
  * @param input Input data
404
404
  * @param initData Initial data
405
- * @param ignoreFields Ignore fields
405
+ * @param ignoreFields Ignore fields, default is ['changedFields', 'id']
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)[] | undefined = undefined
411
+ >(
409
412
  input: T,
410
413
  initData: object,
411
- ignoreFields: string[] = ["id"]
412
- ): Exclude<keyof T & string, typeof IgnoredProperty>[] {
414
+ ignoreFields?: I
415
+ ): Exclude<
416
+ keyof T & string,
417
+ I extends undefined
418
+ ? (typeof IgnoredProperties)[number]
419
+ : Exclude<I, undefined>[number]
420
+ >[] {
421
+ // Default ignore fields
422
+ const fields = ignoreFields ?? IgnoredProperties;
423
+
413
424
  // Changed fields
414
- const changes: Exclude<keyof T & string, typeof IgnoredProperty>[] = [];
425
+ const changes: Exclude<
426
+ keyof T & string,
427
+ I extends undefined
428
+ ? (typeof IgnoredProperties)[number]
429
+ : Exclude<I, undefined>[number]
430
+ >[] = [];
415
431
 
416
432
  Object.entries(input).forEach(([key, value]) => {
417
433
  // Ignore fields, no process
418
- if (key === IgnoredProperty || ignoreFields.includes(key)) return;
434
+ if (fields.includes(key as any)) return;
419
435
 
420
436
  // Compare with init value
421
437
  const initValue = Reflect.get(initData, key);