@etsoo/shared 1.1.89 → 1.1.91

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
@@ -272,6 +272,8 @@ String and other related utilities
272
272
  |replaceNullOrEmpty|Replace null or empty with default value|
273
273
  |setLabels|Set source with new labels|
274
274
  |snakeNameToWord|Snake name to works, 'snake_name' to 'Snake Name'|
275
+ |sortByFavor|Sort array by favored values|
276
+ |sortByFieldFavor|Sort array by favored field values|
275
277
  |trim|Trim chars|
276
278
  |trimEnd|Trim end chars|
277
279
  |trimStart|Trim start chars|
@@ -84,6 +84,28 @@ test('Tests for getDataChanges', () => {
84
84
  expect(input.amount).toBeUndefined();
85
85
  });
86
86
 
87
+ test('Tests for object array getDataChanges', () => {
88
+ const input = {
89
+ id: 1,
90
+ ids: [1, 2],
91
+ items: [
92
+ { id: 1, label: 'a' },
93
+ { id: 2, label: 'b' }
94
+ ]
95
+ };
96
+ const initData = {
97
+ id: 1,
98
+ ids: [1],
99
+ items: [
100
+ { id: 1, label: 'a' },
101
+ { id: 2, label: 'b' }
102
+ ]
103
+ };
104
+ const fields = Utils.getDataChanges(input, initData);
105
+ expect(fields).toStrictEqual(['ids']);
106
+ expect(input.items).toBeUndefined();
107
+ });
108
+
87
109
  test('Tests for exclude', () => {
88
110
  const options = [
89
111
  { id1: 1, name: 'a' },
@@ -228,6 +250,29 @@ test('Tests for getResult', () => {
228
250
  expect(valueResult).toBe(5);
229
251
  });
230
252
 
253
+ test('Tests for sortByFavor', () => {
254
+ const items = [1, 2, 3, 4, 5, 6, 7];
255
+ expect(Utils.sortByFavor(items, [5, 1, 3])).toStrictEqual([
256
+ 5, 1, 3, 2, 4, 6, 7
257
+ ]);
258
+ });
259
+
260
+ test('Tests for sortByFieldFavor', () => {
261
+ const options = [
262
+ { id: 'a', name: 'a1' },
263
+ { id: 'b', name: 'b2' },
264
+ { id: 'c', name: 'c3' },
265
+ { id: 'd', name: 'd4' },
266
+ { id: 'e', name: 'e5' },
267
+ { id: 'f', name: 'f6' }
268
+ ];
269
+ expect(
270
+ Utils.sortByFieldFavor(options, 'id', ['e', 'a', 'c']).map(
271
+ (option) => option.name
272
+ )
273
+ ).toStrictEqual(['e5', 'a1', 'c3', 'b2', 'd4', 'f6']);
274
+ });
275
+
231
276
  test('Tests for trim', () => {
232
277
  expect(Utils.trim('//a/', '/')).toBe('a');
233
278
  expect(Utils.trim('/*/a/', ...['/', '*'])).toBe('a');
@@ -99,7 +99,6 @@ var DataTypes;
99
99
  * @returns Converted value
100
100
  */
101
101
  function convert(input, target) {
102
- var _a;
103
102
  // null or undefined
104
103
  if (input == null)
105
104
  return undefined;
@@ -107,7 +106,9 @@ var DataTypes;
107
106
  if (Array.isArray(target)) {
108
107
  // Element item
109
108
  const elementItem = target.length > 0 ? target[0] : input;
110
- const elementType = (_a = getBasicNameByValue(elementItem, true)) !== null && _a !== void 0 ? _a : 'unknown[]';
109
+ const elementType = getBasicNameByValue(elementItem, true);
110
+ if (elementType == null)
111
+ return input;
111
112
  return convertByType(input, elementType);
112
113
  }
113
114
  // Target type
@@ -140,7 +141,7 @@ var DataTypes;
140
141
  ? input.split(/,\s*/g) // Support comma separated array
141
142
  : [input];
142
143
  // Element type
143
- const elementType = (targetType.substr(0, targetType.length - 2));
144
+ const elementType = (targetType.slice(0, targetType.length - 2));
144
145
  // Convert type
145
146
  return (inputArray
146
147
  .map((item) => convertByType(item, elementType))
@@ -254,6 +254,21 @@ export declare namespace Utils {
254
254
  * @param firstOnly Only convert the first word to upper case
255
255
  */
256
256
  const snakeNameToWord: (name: string, firstOnly?: boolean) => string;
257
+ /**
258
+ * Sort array by favored values
259
+ * @param items Items
260
+ * @param favored Favored values
261
+ * @returns Sorted array
262
+ */
263
+ const sortByFavor: <T>(items: T[], favored: T[]) => T[];
264
+ /**
265
+ * Sort array by favored field values
266
+ * @param items Items
267
+ * @param field Field to sort
268
+ * @param favored Favored field values
269
+ * @returns Sorted array
270
+ */
271
+ const sortByFieldFavor: <T, F extends keyof T>(items: T[], field: F, favored: T[F][]) => T[];
257
272
  /**
258
273
  * Trim chars
259
274
  * @param input Input string
package/lib/cjs/Utils.js CHANGED
@@ -487,6 +487,42 @@ var Utils;
487
487
  }
488
488
  return items.map((part) => part.formatInitial(true)).join(' ');
489
489
  };
490
+ function getSortValue(n1, n2) {
491
+ if (n1 === n2)
492
+ return 0;
493
+ if (n1 === -1)
494
+ return 1;
495
+ if (n2 === -1)
496
+ return -1;
497
+ return n1 - n2;
498
+ }
499
+ /**
500
+ * Sort array by favored values
501
+ * @param items Items
502
+ * @param favored Favored values
503
+ * @returns Sorted array
504
+ */
505
+ Utils.sortByFavor = (items, favored) => {
506
+ return items.sort((r1, r2) => {
507
+ const n1 = favored.indexOf(r1);
508
+ const n2 = favored.indexOf(r2);
509
+ return getSortValue(n1, n2);
510
+ });
511
+ };
512
+ /**
513
+ * Sort array by favored field values
514
+ * @param items Items
515
+ * @param field Field to sort
516
+ * @param favored Favored field values
517
+ * @returns Sorted array
518
+ */
519
+ Utils.sortByFieldFavor = (items, field, favored) => {
520
+ return items.sort((r1, r2) => {
521
+ const n1 = favored.indexOf(r1[field]);
522
+ const n2 = favored.indexOf(r2[field]);
523
+ return getSortValue(n1, n2);
524
+ });
525
+ };
490
526
  /**
491
527
  * Trim chars
492
528
  * @param input Input string
@@ -96,7 +96,6 @@ export var DataTypes;
96
96
  * @returns Converted value
97
97
  */
98
98
  function convert(input, target) {
99
- var _a;
100
99
  // null or undefined
101
100
  if (input == null)
102
101
  return undefined;
@@ -104,7 +103,9 @@ export var DataTypes;
104
103
  if (Array.isArray(target)) {
105
104
  // Element item
106
105
  const elementItem = target.length > 0 ? target[0] : input;
107
- const elementType = (_a = getBasicNameByValue(elementItem, true)) !== null && _a !== void 0 ? _a : 'unknown[]';
106
+ const elementType = getBasicNameByValue(elementItem, true);
107
+ if (elementType == null)
108
+ return input;
108
109
  return convertByType(input, elementType);
109
110
  }
110
111
  // Target type
@@ -137,7 +138,7 @@ export var DataTypes;
137
138
  ? input.split(/,\s*/g) // Support comma separated array
138
139
  : [input];
139
140
  // Element type
140
- const elementType = (targetType.substr(0, targetType.length - 2));
141
+ const elementType = (targetType.slice(0, targetType.length - 2));
141
142
  // Convert type
142
143
  return (inputArray
143
144
  .map((item) => convertByType(item, elementType))
@@ -254,6 +254,21 @@ export declare namespace Utils {
254
254
  * @param firstOnly Only convert the first word to upper case
255
255
  */
256
256
  const snakeNameToWord: (name: string, firstOnly?: boolean) => string;
257
+ /**
258
+ * Sort array by favored values
259
+ * @param items Items
260
+ * @param favored Favored values
261
+ * @returns Sorted array
262
+ */
263
+ const sortByFavor: <T>(items: T[], favored: T[]) => T[];
264
+ /**
265
+ * Sort array by favored field values
266
+ * @param items Items
267
+ * @param field Field to sort
268
+ * @param favored Favored field values
269
+ * @returns Sorted array
270
+ */
271
+ const sortByFieldFavor: <T, F extends keyof T>(items: T[], field: F, favored: T[F][]) => T[];
257
272
  /**
258
273
  * Trim chars
259
274
  * @param input Input string
package/lib/mjs/Utils.js CHANGED
@@ -481,6 +481,42 @@ export var Utils;
481
481
  }
482
482
  return items.map((part) => part.formatInitial(true)).join(' ');
483
483
  };
484
+ function getSortValue(n1, n2) {
485
+ if (n1 === n2)
486
+ return 0;
487
+ if (n1 === -1)
488
+ return 1;
489
+ if (n2 === -1)
490
+ return -1;
491
+ return n1 - n2;
492
+ }
493
+ /**
494
+ * Sort array by favored values
495
+ * @param items Items
496
+ * @param favored Favored values
497
+ * @returns Sorted array
498
+ */
499
+ Utils.sortByFavor = (items, favored) => {
500
+ return items.sort((r1, r2) => {
501
+ const n1 = favored.indexOf(r1);
502
+ const n2 = favored.indexOf(r2);
503
+ return getSortValue(n1, n2);
504
+ });
505
+ };
506
+ /**
507
+ * Sort array by favored field values
508
+ * @param items Items
509
+ * @param field Field to sort
510
+ * @param favored Favored field values
511
+ * @returns Sorted array
512
+ */
513
+ Utils.sortByFieldFavor = (items, field, favored) => {
514
+ return items.sort((r1, r2) => {
515
+ const n1 = favored.indexOf(r1[field]);
516
+ const n2 = favored.indexOf(r2[field]);
517
+ return getSortValue(n1, n2);
518
+ });
519
+ };
484
520
  /**
485
521
  * Trim chars
486
522
  * @param input Input string
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.89",
3
+ "version": "1.1.91",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -54,10 +54,10 @@
54
54
  },
55
55
  "homepage": "https://github.com/ETSOO/Shared#readme",
56
56
  "devDependencies": {
57
- "@types/jest": "^29.4.0",
57
+ "@types/jest": "^29.5.0",
58
58
  "@types/lodash.isequal": "^4.5.6",
59
- "jest": "^29.4.3",
60
- "jest-environment-jsdom": "^29.4.3",
59
+ "jest": "^29.5.0",
60
+ "jest-environment-jsdom": "^29.5.0",
61
61
  "ts-jest": "^29.0.5",
62
62
  "typescript": "^4.9.5"
63
63
  },
package/src/DataTypes.ts CHANGED
@@ -356,9 +356,9 @@ export namespace DataTypes {
356
356
  if (Array.isArray(target)) {
357
357
  // Element item
358
358
  const elementItem = target.length > 0 ? target[0] : input;
359
- const elementType =
360
- getBasicNameByValue(elementItem, true) ?? 'unknown[]';
359
+ const elementType = getBasicNameByValue(elementItem, true);
361
360
 
361
+ if (elementType == null) return <T>input;
362
362
  return <any>convertByType(input, elementType);
363
363
  }
364
364
 
@@ -400,7 +400,7 @@ export namespace DataTypes {
400
400
 
401
401
  // Element type
402
402
  const elementType = <BasicNames>(
403
- targetType.substr(0, targetType.length - 2)
403
+ targetType.slice(0, targetType.length - 2)
404
404
  );
405
405
 
406
406
  // Convert type
package/src/Utils.ts CHANGED
@@ -661,6 +661,46 @@ export namespace Utils {
661
661
  return items.map((part) => part.formatInitial(true)).join(' ');
662
662
  };
663
663
 
664
+ function getSortValue(n1: number, n2: number) {
665
+ if (n1 === n2) return 0;
666
+ if (n1 === -1) return 1;
667
+ if (n2 === -1) return -1;
668
+ return n1 - n2;
669
+ }
670
+
671
+ /**
672
+ * Sort array by favored values
673
+ * @param items Items
674
+ * @param favored Favored values
675
+ * @returns Sorted array
676
+ */
677
+ export const sortByFavor = <T>(items: T[], favored: T[]) => {
678
+ return items.sort((r1, r2) => {
679
+ const n1 = favored.indexOf(r1);
680
+ const n2 = favored.indexOf(r2);
681
+ return getSortValue(n1, n2);
682
+ });
683
+ };
684
+
685
+ /**
686
+ * Sort array by favored field values
687
+ * @param items Items
688
+ * @param field Field to sort
689
+ * @param favored Favored field values
690
+ * @returns Sorted array
691
+ */
692
+ export const sortByFieldFavor = <T, F extends keyof T>(
693
+ items: T[],
694
+ field: F,
695
+ favored: T[F][]
696
+ ) => {
697
+ return items.sort((r1, r2) => {
698
+ const n1 = favored.indexOf(r1[field]);
699
+ const n2 = favored.indexOf(r2[field]);
700
+ return getSortValue(n1, n2);
701
+ });
702
+ };
703
+
664
704
  /**
665
705
  * Trim chars
666
706
  * @param input Input string