@etsoo/shared 1.2.65 → 1.2.67

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
@@ -306,6 +306,8 @@ String and other related utilities
306
306
  | getDataChanges | Get data changed fields with input data updated |
307
307
  | getNestedValue | Get nested value from object |
308
308
  | getTimeZone | Get time zone |
309
+ | hasHtmlEntity | Check the input string contains HTML entity or not |
310
+ | hasHtmlTag | Check the input string contains HTML tag or not |
309
311
  | hideData | Hide data |
310
312
  | hideEmail | Hide email data |
311
313
  | isDigits | Is digits string |
@@ -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: "",
@@ -146,10 +146,11 @@ test("Tests for getDataChanges", () => {
146
146
  enabled: true,
147
147
  value: undefined,
148
148
  date: new Date("2023/03/18"),
149
- ids: [1, 2]
149
+ ids: [1, 2],
150
+ changedFields: ["gender", "brand", "date"]
150
151
  };
151
152
  const fields1 = Utils.getDataChanges(input1, initData, ["brand", "date"]);
152
- expect(fields1).toStrictEqual(["gender", "amount"]);
153
+ expect(fields1).toStrictEqual(["id", "gender", "amount", "changedFields"]);
153
154
  });
154
155
 
155
156
  test("Tests for object array getDataChanges", () => {
@@ -197,6 +198,21 @@ test("Tests for formatString", () => {
197
198
  expect(template.format("aa", "bb")).toBe(result);
198
199
  });
199
200
 
201
+ test("Tests for hasHtmlEntity", () => {
202
+ expect(Utils.hasHtmlEntity("&nbsp")).toBeFalsy();
203
+ expect(Utils.hasHtmlEntity(" ")).toBeTruthy();
204
+ expect(Utils.hasHtmlEntity("< >")).toBeTruthy();
205
+ expect(Utils.hasHtmlEntity("&180;")).toBeFalsy();
206
+ expect(Utils.hasHtmlEntity("&160;")).toBeTruthy();
207
+ });
208
+
209
+ test("Tests for hasHtmlTag", () => {
210
+ expect(Utils.hasHtmlTag("<>")).toBeFalsy();
211
+ expect(Utils.hasHtmlTag("<div>")).toBeTruthy();
212
+ expect(Utils.hasHtmlTag("</h1>")).toBeTruthy();
213
+ expect(Utils.hasHtmlTag("<br>")).toBeTruthy();
214
+ });
215
+
200
216
  test("Tests for hideData", () => {
201
217
  expect("xz@etsoo.com".hideEmail()).toBe("x***@etsoo.com");
202
218
  expect("info@etsoo.com".hideEmail()).toBe("in***@etsoo.com");
@@ -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, const I extends (keyof T & string)[]>(input: T, initData: object, ignoreFields?: I): Exclude<keyof T & string, (typeof ignoreFields)[number] | (typeof IgnoredProperties)[number]>[];
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
@@ -180,6 +180,18 @@ export declare namespace Utils {
180
180
  * @returns Timezone
181
181
  */
182
182
  export const getTimeZone: (tz?: string) => string;
183
+ /**
184
+ * Check the input string contains HTML entity or not
185
+ * @param input Input string
186
+ * @returns Result
187
+ */
188
+ export function hasHtmlEntity(input: string): boolean;
189
+ /**
190
+ * Check the input string contains HTML tag or not
191
+ * @param input Input string
192
+ * @returns Result
193
+ */
194
+ export function hasHtmlTag(input: string): boolean;
183
195
  /**
184
196
  * Is digits string
185
197
  * @param input Input string
package/lib/cjs/Utils.js CHANGED
@@ -257,17 +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 = []) {
263
+ function getDataChanges(input, initData, ignoreFields) {
264
+ // Default ignore fields
265
+ const fields = ignoreFields ?? IgnoredProperties;
264
266
  // Changed fields
265
267
  const changes = [];
266
- // Ignored fields
267
- const allFields = [...ignoreFields, ...IgnoredProperties];
268
268
  Object.entries(input).forEach(([key, value]) => {
269
269
  // Ignore fields, no process
270
- if (allFields.includes(key))
270
+ if (fields.includes(key))
271
271
  return;
272
272
  // Compare with init value
273
273
  const initValue = Reflect.get(initData, key);
@@ -357,6 +357,24 @@ var Utils;
357
357
  // Default timezone
358
358
  return tz ?? "UTC";
359
359
  };
360
+ /**
361
+ * Check the input string contains HTML entity or not
362
+ * @param input Input string
363
+ * @returns Result
364
+ */
365
+ function hasHtmlEntity(input) {
366
+ return /&(lt|gt|nbsp|60|62|160);/.test(input);
367
+ }
368
+ Utils.hasHtmlEntity = hasHtmlEntity;
369
+ /**
370
+ * Check the input string contains HTML tag or not
371
+ * @param input Input string
372
+ * @returns Result
373
+ */
374
+ function hasHtmlTag(input) {
375
+ return /<\/?[a-z]+[^<>]*>/i.test(input);
376
+ }
377
+ Utils.hasHtmlTag = hasHtmlTag;
360
378
  /**
361
379
  * Is digits string
362
380
  * @param input Input string
@@ -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, const I extends (keyof T & string)[]>(input: T, initData: object, ignoreFields?: I): Exclude<keyof T & string, (typeof ignoreFields)[number] | (typeof IgnoredProperties)[number]>[];
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
@@ -180,6 +180,18 @@ export declare namespace Utils {
180
180
  * @returns Timezone
181
181
  */
182
182
  export const getTimeZone: (tz?: string) => string;
183
+ /**
184
+ * Check the input string contains HTML entity or not
185
+ * @param input Input string
186
+ * @returns Result
187
+ */
188
+ export function hasHtmlEntity(input: string): boolean;
189
+ /**
190
+ * Check the input string contains HTML tag or not
191
+ * @param input Input string
192
+ * @returns Result
193
+ */
194
+ export function hasHtmlTag(input: string): boolean;
183
195
  /**
184
196
  * Is digits string
185
197
  * @param input Input string
package/lib/mjs/Utils.js CHANGED
@@ -251,17 +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 = []) {
257
+ function getDataChanges(input, initData, ignoreFields) {
258
+ // Default ignore fields
259
+ const fields = ignoreFields ?? IgnoredProperties;
258
260
  // Changed fields
259
261
  const changes = [];
260
- // Ignored fields
261
- const allFields = [...ignoreFields, ...IgnoredProperties];
262
262
  Object.entries(input).forEach(([key, value]) => {
263
263
  // Ignore fields, no process
264
- if (allFields.includes(key))
264
+ if (fields.includes(key))
265
265
  return;
266
266
  // Compare with init value
267
267
  const initValue = Reflect.get(initData, key);
@@ -351,6 +351,24 @@ export var Utils;
351
351
  // Default timezone
352
352
  return tz ?? "UTC";
353
353
  };
354
+ /**
355
+ * Check the input string contains HTML entity or not
356
+ * @param input Input string
357
+ * @returns Result
358
+ */
359
+ function hasHtmlEntity(input) {
360
+ return /&(lt|gt|nbsp|60|62|160);/.test(input);
361
+ }
362
+ Utils.hasHtmlEntity = hasHtmlEntity;
363
+ /**
364
+ * Check the input string contains HTML tag or not
365
+ * @param input Input string
366
+ * @returns Result
367
+ */
368
+ function hasHtmlTag(input) {
369
+ return /<\/?[a-z]+[^<>]*>/i.test(input);
370
+ }
371
+ Utils.hasHtmlTag = hasHtmlTag;
354
372
  /**
355
373
  * Is digits string
356
374
  * @param input Input string
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.2.65",
3
+ "version": "1.2.67",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -39,7 +39,7 @@
39
39
  "@types/lodash.isequal": "^4.5.8",
40
40
  "@vitejs/plugin-react": "^4.3.4",
41
41
  "jsdom": "^26.0.0",
42
- "typescript": "^5.8.2",
42
+ "typescript": "^5.8.3",
43
43
  "vitest": "^3.1.1"
44
44
  },
45
45
  "dependencies": {
package/src/Utils.ts CHANGED
@@ -402,32 +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
408
  export function getDataChanges<
409
409
  T extends object,
410
- const I extends (keyof T & string)[]
410
+ const I extends (keyof T & string)[] | undefined = undefined
411
411
  >(
412
412
  input: T,
413
413
  initData: object,
414
- ignoreFields: I = [] as any
414
+ ignoreFields?: I
415
415
  ): Exclude<
416
416
  keyof T & string,
417
- (typeof ignoreFields)[number] | (typeof IgnoredProperties)[number]
417
+ I extends undefined
418
+ ? (typeof IgnoredProperties)[number]
419
+ : Exclude<I, undefined>[number]
418
420
  >[] {
421
+ // Default ignore fields
422
+ const fields = ignoreFields ?? IgnoredProperties;
423
+
419
424
  // Changed fields
420
425
  const changes: Exclude<
421
426
  keyof T & string,
422
- (typeof ignoreFields)[number] | (typeof IgnoredProperties)[number]
427
+ I extends undefined
428
+ ? (typeof IgnoredProperties)[number]
429
+ : Exclude<I, undefined>[number]
423
430
  >[] = [];
424
431
 
425
- // Ignored fields
426
- const allFields: string[] = [...ignoreFields, ...IgnoredProperties];
427
-
428
432
  Object.entries(input).forEach(([key, value]) => {
429
433
  // Ignore fields, no process
430
- if (allFields.includes(key)) return;
434
+ if (fields.includes(key as any)) return;
431
435
 
432
436
  // Compare with init value
433
437
  const initValue = Reflect.get(initData, key);
@@ -530,6 +534,24 @@ export namespace Utils {
530
534
  return tz ?? "UTC";
531
535
  };
532
536
 
537
+ /**
538
+ * Check the input string contains HTML entity or not
539
+ * @param input Input string
540
+ * @returns Result
541
+ */
542
+ export function hasHtmlEntity(input: string) {
543
+ return /&(lt|gt|nbsp|60|62|160);/.test(input);
544
+ }
545
+
546
+ /**
547
+ * Check the input string contains HTML tag or not
548
+ * @param input Input string
549
+ * @returns Result
550
+ */
551
+ export function hasHtmlTag(input: string) {
552
+ return /<\/?[a-z]+[^<>]*>/i.test(input);
553
+ }
554
+
533
555
  /**
534
556
  * Is digits string
535
557
  * @param input Input string