@etsoo/shared 1.2.64 → 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.
@@ -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" },
@@ -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);
@@ -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.64",
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",
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);