file-path-helper 1.4.4 → 2.0.0
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/.eslintrc.json +19 -18
- package/.github/workflows/nodejs.yml +34 -26
- package/.markdownlint.json +6 -0
- package/.vscode/settings.json +7 -0
- package/CHANGELOG.md +10 -0
- package/README.md +364 -364
- package/index.d.ts +206 -206
- package/jest.config.js +5 -0
- package/package.json +9 -7
- package/src/arr.js +48 -48
- package/src/date.js +77 -77
- package/src/file.js +7 -11
- package/src/index.js +4 -11
- package/src/str.js +1 -1
- package/test/utils.test.js +3 -2
package/index.d.ts
CHANGED
|
@@ -1,206 +1,206 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Directory Separator
|
|
5
|
-
*/
|
|
6
|
-
export type Separator = '/'|'\\';
|
|
7
|
-
|
|
8
|
-
export interface Size {
|
|
9
|
-
bytes: number;
|
|
10
|
-
/**
|
|
11
|
-
* e.g. '>' or '<='
|
|
12
|
-
*/
|
|
13
|
-
operator: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface ParsedDate {
|
|
17
|
-
date: Date;
|
|
18
|
-
year: number;
|
|
19
|
-
month: number;
|
|
20
|
-
day: number;
|
|
21
|
-
/**
|
|
22
|
-
* Returns only date as string. e.g. '2019-01-01'
|
|
23
|
-
*/
|
|
24
|
-
toDateString(): string;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
//
|
|
28
|
-
// file methods
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Glob promise.
|
|
33
|
-
*
|
|
34
|
-
* @param {string} pattern
|
|
35
|
-
* @param {GlobOptions} options
|
|
36
|
-
* @returns {Promise<string[], Error>}
|
|
37
|
-
*/
|
|
38
|
-
export function globPromise(pattern: string, options:
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Replace directory separator.
|
|
42
|
-
*
|
|
43
|
-
* @param {string} path
|
|
44
|
-
* @param {Separator} separator default: '/'
|
|
45
|
-
* @returns {string}
|
|
46
|
-
*/
|
|
47
|
-
export function replaceSeparator(path: string, separator: Separator): string;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Append last slash to directory.
|
|
51
|
-
*
|
|
52
|
-
* @param {string} dir
|
|
53
|
-
* @param {Separator} separator default: '/'
|
|
54
|
-
*/
|
|
55
|
-
export function trimDir(dir:string, separator?: Separator): string;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Set directory part of path.
|
|
59
|
-
*
|
|
60
|
-
* @param {string} path
|
|
61
|
-
* @param {string} dir
|
|
62
|
-
* @param {Separator} separator default: '/'
|
|
63
|
-
* @returns {string}
|
|
64
|
-
*/
|
|
65
|
-
export function setDir(path: string, dir: string, separator?: Separator): string;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Get last number from path.
|
|
69
|
-
*
|
|
70
|
-
* @param {string} path
|
|
71
|
-
* @returns {string}
|
|
72
|
-
*/
|
|
73
|
-
export function getLastNumber(path: string): string;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Remove last number from file name.
|
|
77
|
-
*
|
|
78
|
-
* @param {string} file
|
|
79
|
-
* @returns {string}
|
|
80
|
-
*/
|
|
81
|
-
export function removeLastNumber(file: string): string;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Auto increase path.
|
|
85
|
-
* If the same file exists, It's returns filename what increased number.
|
|
86
|
-
*
|
|
87
|
-
* @param {string} path
|
|
88
|
-
* @returns {Promise<string>} auto increased path.
|
|
89
|
-
*/
|
|
90
|
-
export function autoIncrease(path: string): Promise<string>;
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Resolve output file name.
|
|
94
|
-
*
|
|
95
|
-
* @param {string} output
|
|
96
|
-
* @param {string} source
|
|
97
|
-
* @returns {string}
|
|
98
|
-
*/
|
|
99
|
-
export function resolveOutputFile(output: string, source: string): string;
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Convert size in bytes.
|
|
103
|
-
* @see https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
|
|
104
|
-
*
|
|
105
|
-
* @param {number} bytes
|
|
106
|
-
* @param {number} decimals default: 2
|
|
107
|
-
* @returns {string}
|
|
108
|
-
*/
|
|
109
|
-
export function bytesToSize(bytes: number, decimals?: number): string;
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Parses string that includes file size and operator.
|
|
113
|
-
*
|
|
114
|
-
* @param {string} size e.g '10.5mb' '>1GB' '=<10kb'
|
|
115
|
-
* @returns {Size} Size
|
|
116
|
-
*/
|
|
117
|
-
export function parseSize(size: string): Size;
|
|
118
|
-
|
|
119
|
-
//
|
|
120
|
-
// string methods
|
|
121
|
-
//
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* truncate string.
|
|
125
|
-
*
|
|
126
|
-
* @param {string} str
|
|
127
|
-
* @param {number} length default: 40
|
|
128
|
-
* @param {string} ellipsis default: '…'
|
|
129
|
-
* @returns {string}
|
|
130
|
-
*/
|
|
131
|
-
export function truncate(str: string, length?: number, ellipsis?: string): string;
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Sanitize string for safe filename.
|
|
135
|
-
* @see https://github.com/parshap/node-sanitize-filename#readme
|
|
136
|
-
*
|
|
137
|
-
* @param {string} str
|
|
138
|
-
* @param {string} replacer default: `''`
|
|
139
|
-
* @returns {string}
|
|
140
|
-
*/
|
|
141
|
-
export function sanitize(str: string, replacer?: string): string;
|
|
142
|
-
|
|
143
|
-
//
|
|
144
|
-
// array methods
|
|
145
|
-
//
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Sorting array of alphanumerical strings naturally.
|
|
149
|
-
* @see https://stackoverflow.com/questions/2802341/javascript-natural-sort-of-alphanumerical-strings
|
|
150
|
-
*
|
|
151
|
-
* @param {string[]} arr
|
|
152
|
-
* @returns {string[]}
|
|
153
|
-
*/
|
|
154
|
-
export function naturalSort(arr: string[]): string[];
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Filtering an array with Promise
|
|
158
|
-
* @see https://stackoverflow.com/questions/33355528/filtering-an-array-with-a-function-that-returns-a-promise
|
|
159
|
-
*
|
|
160
|
-
* @template T
|
|
161
|
-
* @param {T[]} arr - filtering target array.
|
|
162
|
-
* @param {function(T, number, T[]): Promise<boolean>} cb - callback function for filtering. arguments is value, index, array.
|
|
163
|
-
* @returns {Promise<T[]>}
|
|
164
|
-
*/
|
|
165
|
-
export function filter<T>(arr: T[], cb: (value: T, index: number, arr: T[]) => Promise<boolean>): Promise<T[]>;
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Split array into chunks
|
|
169
|
-
* @see https://stackoverflow.com/questions/8495687/split-array-into-chunks#answer-8495740
|
|
170
|
-
*
|
|
171
|
-
* @template T
|
|
172
|
-
* @param {T[]} arr
|
|
173
|
-
* @param {number} size
|
|
174
|
-
* @returns {T[][]}
|
|
175
|
-
*/
|
|
176
|
-
export function chunks<T>(arr: T[], size: number): T[][];
|
|
177
|
-
|
|
178
|
-
//
|
|
179
|
-
// date methods
|
|
180
|
-
//
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Parsing the value to date. it's useful handling 'date'(not hours and minutes) purpose.
|
|
184
|
-
*
|
|
185
|
-
* @param {string|number|Date} value
|
|
186
|
-
* @returns {ParsedDate}
|
|
187
|
-
*/
|
|
188
|
-
export function parseDate(value: string|number|Date): ParsedDate;
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Get array of date strings
|
|
192
|
-
*
|
|
193
|
-
* @param {string} value date string. e.g. '2020-01-01~2020-01-03'
|
|
194
|
-
* @returns {string[]} e.g. ['2020-01-01', '2020-01-02', '2020-01-03']
|
|
195
|
-
*/
|
|
196
|
-
export function getDates(value: string): string[];
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* difference between two dates.
|
|
200
|
-
* @see https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript
|
|
201
|
-
*
|
|
202
|
-
* @param {string|number|Date} a
|
|
203
|
-
* @param {string|number|Date} b
|
|
204
|
-
* @returns {number}
|
|
205
|
-
*/
|
|
206
|
-
export function diffDays(a: string|number|Date, b: string|number|Date): number;
|
|
1
|
+
import { GlobOptions } from 'glob';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Directory Separator
|
|
5
|
+
*/
|
|
6
|
+
export type Separator = '/'|'\\';
|
|
7
|
+
|
|
8
|
+
export interface Size {
|
|
9
|
+
bytes: number;
|
|
10
|
+
/**
|
|
11
|
+
* e.g. '>' or '<='
|
|
12
|
+
*/
|
|
13
|
+
operator: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ParsedDate {
|
|
17
|
+
date: Date;
|
|
18
|
+
year: number;
|
|
19
|
+
month: number;
|
|
20
|
+
day: number;
|
|
21
|
+
/**
|
|
22
|
+
* Returns only date as string. e.g. '2019-01-01'
|
|
23
|
+
*/
|
|
24
|
+
toDateString(): string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//
|
|
28
|
+
// file methods
|
|
29
|
+
//
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Glob promise.
|
|
33
|
+
*
|
|
34
|
+
* @param {string} pattern
|
|
35
|
+
* @param {GlobOptions} options
|
|
36
|
+
* @returns {Promise<string[], Error>}
|
|
37
|
+
*/
|
|
38
|
+
export function globPromise(pattern: string, options: GlobOptions): Promise<string[]>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Replace directory separator.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} path
|
|
44
|
+
* @param {Separator} separator default: '/'
|
|
45
|
+
* @returns {string}
|
|
46
|
+
*/
|
|
47
|
+
export function replaceSeparator(path: string, separator: Separator): string;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Append last slash to directory.
|
|
51
|
+
*
|
|
52
|
+
* @param {string} dir
|
|
53
|
+
* @param {Separator} separator default: '/'
|
|
54
|
+
*/
|
|
55
|
+
export function trimDir(dir:string, separator?: Separator): string;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Set directory part of path.
|
|
59
|
+
*
|
|
60
|
+
* @param {string} path
|
|
61
|
+
* @param {string} dir
|
|
62
|
+
* @param {Separator} separator default: '/'
|
|
63
|
+
* @returns {string}
|
|
64
|
+
*/
|
|
65
|
+
export function setDir(path: string, dir: string, separator?: Separator): string;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Get last number from path.
|
|
69
|
+
*
|
|
70
|
+
* @param {string} path
|
|
71
|
+
* @returns {string}
|
|
72
|
+
*/
|
|
73
|
+
export function getLastNumber(path: string): string;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Remove last number from file name.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} file
|
|
79
|
+
* @returns {string}
|
|
80
|
+
*/
|
|
81
|
+
export function removeLastNumber(file: string): string;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Auto increase path.
|
|
85
|
+
* If the same file exists, It's returns filename what increased number.
|
|
86
|
+
*
|
|
87
|
+
* @param {string} path
|
|
88
|
+
* @returns {Promise<string>} auto increased path.
|
|
89
|
+
*/
|
|
90
|
+
export function autoIncrease(path: string): Promise<string>;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Resolve output file name.
|
|
94
|
+
*
|
|
95
|
+
* @param {string} output
|
|
96
|
+
* @param {string} source
|
|
97
|
+
* @returns {string}
|
|
98
|
+
*/
|
|
99
|
+
export function resolveOutputFile(output: string, source: string): string;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Convert size in bytes.
|
|
103
|
+
* @see https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
|
|
104
|
+
*
|
|
105
|
+
* @param {number} bytes
|
|
106
|
+
* @param {number} decimals default: 2
|
|
107
|
+
* @returns {string}
|
|
108
|
+
*/
|
|
109
|
+
export function bytesToSize(bytes: number, decimals?: number): string;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Parses string that includes file size and operator.
|
|
113
|
+
*
|
|
114
|
+
* @param {string} size e.g '10.5mb' '>1GB' '=<10kb'
|
|
115
|
+
* @returns {Size} Size
|
|
116
|
+
*/
|
|
117
|
+
export function parseSize(size: string): Size;
|
|
118
|
+
|
|
119
|
+
//
|
|
120
|
+
// string methods
|
|
121
|
+
//
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* truncate string.
|
|
125
|
+
*
|
|
126
|
+
* @param {string} str
|
|
127
|
+
* @param {number} length default: 40
|
|
128
|
+
* @param {string} ellipsis default: '…'
|
|
129
|
+
* @returns {string}
|
|
130
|
+
*/
|
|
131
|
+
export function truncate(str: string, length?: number, ellipsis?: string): string;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Sanitize string for safe filename.
|
|
135
|
+
* @see https://github.com/parshap/node-sanitize-filename#readme
|
|
136
|
+
*
|
|
137
|
+
* @param {string} str
|
|
138
|
+
* @param {string} replacer default: `''`
|
|
139
|
+
* @returns {string}
|
|
140
|
+
*/
|
|
141
|
+
export function sanitize(str: string, replacer?: string): string;
|
|
142
|
+
|
|
143
|
+
//
|
|
144
|
+
// array methods
|
|
145
|
+
//
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Sorting array of alphanumerical strings naturally.
|
|
149
|
+
* @see https://stackoverflow.com/questions/2802341/javascript-natural-sort-of-alphanumerical-strings
|
|
150
|
+
*
|
|
151
|
+
* @param {string[]} arr
|
|
152
|
+
* @returns {string[]}
|
|
153
|
+
*/
|
|
154
|
+
export function naturalSort(arr: string[]): string[];
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Filtering an array with Promise
|
|
158
|
+
* @see https://stackoverflow.com/questions/33355528/filtering-an-array-with-a-function-that-returns-a-promise
|
|
159
|
+
*
|
|
160
|
+
* @template T
|
|
161
|
+
* @param {T[]} arr - filtering target array.
|
|
162
|
+
* @param {function(T, number, T[]): Promise<boolean>} cb - callback function for filtering. arguments is value, index, array.
|
|
163
|
+
* @returns {Promise<T[]>}
|
|
164
|
+
*/
|
|
165
|
+
export function filter<T>(arr: T[], cb: (value: T, index: number, arr: T[]) => Promise<boolean>): Promise<T[]>;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Split array into chunks
|
|
169
|
+
* @see https://stackoverflow.com/questions/8495687/split-array-into-chunks#answer-8495740
|
|
170
|
+
*
|
|
171
|
+
* @template T
|
|
172
|
+
* @param {T[]} arr
|
|
173
|
+
* @param {number} size
|
|
174
|
+
* @returns {T[][]}
|
|
175
|
+
*/
|
|
176
|
+
export function chunks<T>(arr: T[], size: number): T[][];
|
|
177
|
+
|
|
178
|
+
//
|
|
179
|
+
// date methods
|
|
180
|
+
//
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Parsing the value to date. it's useful handling 'date'(not hours and minutes) purpose.
|
|
184
|
+
*
|
|
185
|
+
* @param {string|number|Date} value
|
|
186
|
+
* @returns {ParsedDate}
|
|
187
|
+
*/
|
|
188
|
+
export function parseDate(value: string|number|Date): ParsedDate;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Get array of date strings
|
|
192
|
+
*
|
|
193
|
+
* @param {string} value date string. e.g. '2020-01-01~2020-01-03'
|
|
194
|
+
* @returns {string[]} e.g. ['2020-01-01', '2020-01-02', '2020-01-03']
|
|
195
|
+
*/
|
|
196
|
+
export function getDates(value: string): string[];
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* difference between two dates.
|
|
200
|
+
* @see https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript
|
|
201
|
+
*
|
|
202
|
+
* @param {string|number|Date} a
|
|
203
|
+
* @param {string|number|Date} b
|
|
204
|
+
* @returns {number}
|
|
205
|
+
*/
|
|
206
|
+
export function diffDays(a: string|number|Date, b: string|number|Date): number;
|
package/jest.config.js
ADDED
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "file-path-helper",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"types": "index.d.ts",
|
|
5
6
|
"description": "Helpful methods for handling file path.",
|
|
6
7
|
"main": "src/index.js",
|
|
7
8
|
"scripts": {
|
|
8
|
-
"test": "jest"
|
|
9
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
|
|
9
10
|
},
|
|
10
11
|
"keywords": [
|
|
11
12
|
"path"
|
|
@@ -13,12 +14,13 @@
|
|
|
13
14
|
"author": "archco",
|
|
14
15
|
"license": "MIT",
|
|
15
16
|
"dependencies": {
|
|
16
|
-
"fs-extra": "^
|
|
17
|
-
"glob": "^
|
|
17
|
+
"fs-extra": "^11.3.4",
|
|
18
|
+
"glob": "^13.0.6"
|
|
18
19
|
},
|
|
19
20
|
"devDependencies": {
|
|
20
|
-
"@types/
|
|
21
|
-
"
|
|
22
|
-
"
|
|
21
|
+
"@types/glob": "^8.1.0",
|
|
22
|
+
"@types/jest": "^30.0.0",
|
|
23
|
+
"eslint": "^10.0.3",
|
|
24
|
+
"jest": "^30.3.0"
|
|
23
25
|
}
|
|
24
26
|
}
|
package/src/arr.js
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sorting array of alphanumerical strings naturally.
|
|
3
|
-
* @see https://stackoverflow.com/questions/2802341/javascript-natural-sort-of-alphanumerical-strings
|
|
4
|
-
*
|
|
5
|
-
* @param {string[]} arr
|
|
6
|
-
* @returns {string[]}
|
|
7
|
-
*/
|
|
8
|
-
function naturalSort(arr) {
|
|
9
|
-
const collator = new Intl.Collator(undefined, {
|
|
10
|
-
numeric: true,
|
|
11
|
-
sensitivity: 'base',
|
|
12
|
-
});
|
|
13
|
-
return arr.sort(collator.compare);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Filtering an array with Promise
|
|
18
|
-
* @see https://stackoverflow.com/questions/33355528/filtering-an-array-with-a-function-that-returns-a-promise
|
|
19
|
-
*
|
|
20
|
-
* @template T
|
|
21
|
-
* @param {T[]} arr - filtering target array.
|
|
22
|
-
* @param {function(T, number, T[]): Promise<boolean>} cb - callback function for filtering. arguments is value, index, array.
|
|
23
|
-
* @returns {Promise<T[]>}
|
|
24
|
-
*/
|
|
25
|
-
async function filter(arr, cb) {
|
|
26
|
-
const fail = Symbol();
|
|
27
|
-
return (await Promise.all(arr.map(async (v, i, a) => (await cb(v, i, a)) ? v : fail))).filter(i => i !== fail);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Split array into chunks
|
|
32
|
-
* @see https://stackoverflow.com/questions/8495687/split-array-into-chunks#answer-8495740
|
|
33
|
-
*
|
|
34
|
-
* @template T
|
|
35
|
-
* @param {T[]} arr
|
|
36
|
-
* @param {number} size
|
|
37
|
-
* @returns {T[][]}
|
|
38
|
-
*/
|
|
39
|
-
function chunks(arr, size) {
|
|
40
|
-
size = size < 1 ? arr.length : size;
|
|
41
|
-
return Array(Math.ceil(arr.length / size)).fill().map((_, index) => index * size).map(begin => arr.slice(begin, begin + size));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
naturalSort,
|
|
46
|
-
filter,
|
|
47
|
-
chunks,
|
|
48
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Sorting array of alphanumerical strings naturally.
|
|
3
|
+
* @see https://stackoverflow.com/questions/2802341/javascript-natural-sort-of-alphanumerical-strings
|
|
4
|
+
*
|
|
5
|
+
* @param {string[]} arr
|
|
6
|
+
* @returns {string[]}
|
|
7
|
+
*/
|
|
8
|
+
function naturalSort(arr) {
|
|
9
|
+
const collator = new Intl.Collator(undefined, {
|
|
10
|
+
numeric: true,
|
|
11
|
+
sensitivity: 'base',
|
|
12
|
+
});
|
|
13
|
+
return arr.sort(collator.compare);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Filtering an array with Promise
|
|
18
|
+
* @see https://stackoverflow.com/questions/33355528/filtering-an-array-with-a-function-that-returns-a-promise
|
|
19
|
+
*
|
|
20
|
+
* @template T
|
|
21
|
+
* @param {T[]} arr - filtering target array.
|
|
22
|
+
* @param {function(T, number, T[]): Promise<boolean>} cb - callback function for filtering. arguments is value, index, array.
|
|
23
|
+
* @returns {Promise<T[]>}
|
|
24
|
+
*/
|
|
25
|
+
async function filter(arr, cb) {
|
|
26
|
+
const fail = Symbol();
|
|
27
|
+
return (await Promise.all(arr.map(async (v, i, a) => (await cb(v, i, a)) ? v : fail))).filter(i => i !== fail);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Split array into chunks
|
|
32
|
+
* @see https://stackoverflow.com/questions/8495687/split-array-into-chunks#answer-8495740
|
|
33
|
+
*
|
|
34
|
+
* @template T
|
|
35
|
+
* @param {T[]} arr
|
|
36
|
+
* @param {number} size
|
|
37
|
+
* @returns {T[][]}
|
|
38
|
+
*/
|
|
39
|
+
function chunks(arr, size) {
|
|
40
|
+
size = size < 1 ? arr.length : size;
|
|
41
|
+
return Array(Math.ceil(arr.length / size)).fill().map((_, index) => index * size).map(begin => arr.slice(begin, begin + size));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export {
|
|
45
|
+
naturalSort,
|
|
46
|
+
filter,
|
|
47
|
+
chunks,
|
|
48
|
+
};
|