@etsoo/shared 1.1.88 → 1.1.90

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
@@ -246,6 +246,7 @@ String and other related utilities
246
246
  |addBlankItem|Add blank item to collection|
247
247
  |arrayDifferences|Array 1 items do not exist in Array 2 or reverse match|
248
248
  |charsToNumber|Base64 chars to number|
249
+ |containChinese|Check the input string contains Chinese character or not|
249
250
  |correctTypes|Correct object's property value type|
250
251
  |equals|Two values equal|
251
252
  |exclude|Exclude specific items|
@@ -271,6 +272,8 @@ String and other related utilities
271
272
  |replaceNullOrEmpty|Replace null or empty with default value|
272
273
  |setLabels|Set source with new labels|
273
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|
274
277
  |trim|Trim chars|
275
278
  |trimEnd|Trim end chars|
276
279
  |trimStart|Trim start chars|
@@ -13,6 +13,14 @@ test('Tests for addBlankItem', () => {
13
13
  expect(options.length).toBe(3);
14
14
  });
15
15
 
16
+ test('Tests for containChinese', () => {
17
+ expect('123 abC'.containChinese()).toBeFalsy();
18
+ expect('亿速思维'.containChinese()).toBeTruthy();
19
+ expect('亿速Etsoo'.containChinese()).toBeTruthy();
20
+ expect('김 민수'.containKorean()).toBeTruthy();
21
+ expect('ぁ-ん'.containJapanese()).toBeTruthy();
22
+ });
23
+
16
24
  test('Tests for arrayDifferences', () => {
17
25
  const a1 = ['a', 'b', 'c', 'e'];
18
26
  const a2 = ['a', 'c', 'd'];
@@ -220,6 +228,29 @@ test('Tests for getResult', () => {
220
228
  expect(valueResult).toBe(5);
221
229
  });
222
230
 
231
+ test('Tests for sortByFavor', () => {
232
+ const items = [1, 2, 3, 4, 5, 6, 7];
233
+ expect(Utils.sortByFavor(items, [5, 1, 3])).toStrictEqual([
234
+ 5, 1, 3, 2, 4, 6, 7
235
+ ]);
236
+ });
237
+
238
+ test('Tests for sortByFieldFavor', () => {
239
+ const options = [
240
+ { id: 'a', name: 'a1' },
241
+ { id: 'b', name: 'b2' },
242
+ { id: 'c', name: 'c3' },
243
+ { id: 'd', name: 'd4' },
244
+ { id: 'e', name: 'e5' },
245
+ { id: 'f', name: 'f6' }
246
+ ];
247
+ expect(
248
+ Utils.sortByFieldFavor(options, 'id', ['e', 'a', 'c']).map(
249
+ (option) => option.name
250
+ )
251
+ ).toStrictEqual(['e5', 'a1', 'c3', 'b2', 'd4', 'f6']);
252
+ });
253
+
223
254
  test('Tests for trim', () => {
224
255
  expect(Utils.trim('//a/', '/')).toBe('a');
225
256
  expect(Utils.trim('/*/a/', ...['/', '*'])).toBe('a');
@@ -1,9 +1,27 @@
1
1
  import { DataTypes } from './DataTypes';
2
2
  declare global {
3
3
  interface String {
4
+ /**
5
+ * Check the input string contains Chinese character or not
6
+ * @param this Input
7
+ * @param test Test string
8
+ */
9
+ containChinese(this: string): boolean;
10
+ /**
11
+ * Check the input string contains Korean character or not
12
+ * @param this Input
13
+ * @param test Test string
14
+ */
15
+ containKorean(this: string): boolean;
16
+ /**
17
+ * Check the input string contains Japanese character or not
18
+ * @param this Input
19
+ * @param test Test string
20
+ */
21
+ containJapanese(this: string): boolean;
4
22
  /**
5
23
  * Format string with parameters
6
- * @param this Template
24
+ * @param this Input template
7
25
  * @param parameters Parameters to fill the template
8
26
  */
9
27
  format(this: string, ...parameters: string[]): string;
@@ -236,6 +254,21 @@ export declare namespace Utils {
236
254
  * @param firstOnly Only convert the first word to upper case
237
255
  */
238
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[];
239
272
  /**
240
273
  * Trim chars
241
274
  * @param input Input string
package/lib/cjs/Utils.js CHANGED
@@ -15,6 +15,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.Utils = void 0;
16
16
  const DataTypes_1 = require("./DataTypes");
17
17
  const lodash_isequal_1 = __importDefault(require("lodash.isequal"));
18
+ String.prototype.containChinese = function () {
19
+ const regExp = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
20
+ return regExp.test(this);
21
+ };
22
+ String.prototype.containKorean = function () {
23
+ const regExp = /[\uac00-\ud7af\u1100-\u11ff\u3130-\u318f\ua960-\ua97f\ud7b0-\ud7ff\u3400-\u4dbf]/g;
24
+ return regExp.test(this);
25
+ };
26
+ String.prototype.containJapanese = function () {
27
+ const regExp = /[\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf]/g;
28
+ return regExp.test(this);
29
+ };
18
30
  String.prototype.format = function (...parameters) {
19
31
  let template = this;
20
32
  parameters.forEach((value, index) => {
@@ -475,6 +487,42 @@ var Utils;
475
487
  }
476
488
  return items.map((part) => part.formatInitial(true)).join(' ');
477
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
+ };
478
526
  /**
479
527
  * Trim chars
480
528
  * @param input Input string
@@ -1,9 +1,27 @@
1
1
  import { DataTypes } from './DataTypes';
2
2
  declare global {
3
3
  interface String {
4
+ /**
5
+ * Check the input string contains Chinese character or not
6
+ * @param this Input
7
+ * @param test Test string
8
+ */
9
+ containChinese(this: string): boolean;
10
+ /**
11
+ * Check the input string contains Korean character or not
12
+ * @param this Input
13
+ * @param test Test string
14
+ */
15
+ containKorean(this: string): boolean;
16
+ /**
17
+ * Check the input string contains Japanese character or not
18
+ * @param this Input
19
+ * @param test Test string
20
+ */
21
+ containJapanese(this: string): boolean;
4
22
  /**
5
23
  * Format string with parameters
6
- * @param this Template
24
+ * @param this Input template
7
25
  * @param parameters Parameters to fill the template
8
26
  */
9
27
  format(this: string, ...parameters: string[]): string;
@@ -236,6 +254,21 @@ export declare namespace Utils {
236
254
  * @param firstOnly Only convert the first word to upper case
237
255
  */
238
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[];
239
272
  /**
240
273
  * Trim chars
241
274
  * @param input Input string
package/lib/mjs/Utils.js CHANGED
@@ -9,6 +9,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { DataTypes } from './DataTypes';
11
11
  import isEqual from 'lodash.isequal';
12
+ String.prototype.containChinese = function () {
13
+ const regExp = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
14
+ return regExp.test(this);
15
+ };
16
+ String.prototype.containKorean = function () {
17
+ const regExp = /[\uac00-\ud7af\u1100-\u11ff\u3130-\u318f\ua960-\ua97f\ud7b0-\ud7ff\u3400-\u4dbf]/g;
18
+ return regExp.test(this);
19
+ };
20
+ String.prototype.containJapanese = function () {
21
+ const regExp = /[\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf]/g;
22
+ return regExp.test(this);
23
+ };
12
24
  String.prototype.format = function (...parameters) {
13
25
  let template = this;
14
26
  parameters.forEach((value, index) => {
@@ -469,6 +481,42 @@ export var Utils;
469
481
  }
470
482
  return items.map((part) => part.formatInitial(true)).join(' ');
471
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
+ };
472
520
  /**
473
521
  * Trim chars
474
522
  * @param input Input string
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.88",
3
+ "version": "1.1.90",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -54,12 +54,12 @@
54
54
  },
55
55
  "homepage": "https://github.com/ETSOO/Shared#readme",
56
56
  "devDependencies": {
57
- "@types/jest": "^29.2.5",
57
+ "@types/jest": "^29.4.0",
58
58
  "@types/lodash.isequal": "^4.5.6",
59
- "jest": "^29.3.1",
60
- "jest-environment-jsdom": "^29.3.1",
61
- "ts-jest": "^29.0.4",
62
- "typescript": "^4.9.4"
59
+ "jest": "^29.5.0",
60
+ "jest-environment-jsdom": "^29.5.0",
61
+ "ts-jest": "^29.0.5",
62
+ "typescript": "^4.9.5"
63
63
  },
64
64
  "dependencies": {
65
65
  "lodash.isequal": "^4.5.0"
package/src/Utils.ts CHANGED
@@ -3,9 +3,30 @@ import isEqual from 'lodash.isequal';
3
3
 
4
4
  declare global {
5
5
  interface String {
6
+ /**
7
+ * Check the input string contains Chinese character or not
8
+ * @param this Input
9
+ * @param test Test string
10
+ */
11
+ containChinese(this: string): boolean;
12
+
13
+ /**
14
+ * Check the input string contains Korean character or not
15
+ * @param this Input
16
+ * @param test Test string
17
+ */
18
+ containKorean(this: string): boolean;
19
+
20
+ /**
21
+ * Check the input string contains Japanese character or not
22
+ * @param this Input
23
+ * @param test Test string
24
+ */
25
+ containJapanese(this: string): boolean;
26
+
6
27
  /**
7
28
  * Format string with parameters
8
- * @param this Template
29
+ * @param this Input template
9
30
  * @param parameters Parameters to fill the template
10
31
  */
11
32
  format(this: string, ...parameters: string[]): string;
@@ -51,6 +72,23 @@ declare global {
51
72
  }
52
73
  }
53
74
 
75
+ String.prototype.containChinese = function (this: string): boolean {
76
+ const regExp =
77
+ /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
78
+ return regExp.test(this);
79
+ };
80
+
81
+ String.prototype.containKorean = function (this: string): boolean {
82
+ const regExp =
83
+ /[\uac00-\ud7af\u1100-\u11ff\u3130-\u318f\ua960-\ua97f\ud7b0-\ud7ff\u3400-\u4dbf]/g;
84
+ return regExp.test(this);
85
+ };
86
+
87
+ String.prototype.containJapanese = function (this: string): boolean {
88
+ const regExp = /[\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf]/g;
89
+ return regExp.test(this);
90
+ };
91
+
54
92
  String.prototype.format = function (
55
93
  this: string,
56
94
  ...parameters: string[]
@@ -623,6 +661,46 @@ export namespace Utils {
623
661
  return items.map((part) => part.formatInitial(true)).join(' ');
624
662
  };
625
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
+
626
704
  /**
627
705
  * Trim chars
628
706
  * @param input Input string