file-path-helper 1.4.5 → 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/README.md CHANGED
@@ -1,364 +1,364 @@
1
- # File Path Helper
2
-
3
- Helpful methods for handling file path.
4
-
5
- ## Table of contents
6
-
7
- - File Methods
8
- - [globPromise](#globPromise)
9
- - [replaceSeparator](#replaceSeparator)
10
- - [trimDir](#trimDir)
11
- - [setDir](#setDir)
12
- - [getLastNumber](#getLastNumber)
13
- - [removeLastNumber](#removeLastNumber)
14
- - [autoIncrease](#autoIncrease)
15
- - [resolveOutputFile](#resolveOutputFile)
16
- - [bytesToSize](#bytesToSize)
17
- - [parseSize](#parseSize)
18
- - String Methods
19
- - [truncate](#truncate)
20
- - [sanitize](#sanitize)
21
- - Array Methods
22
- - [naturalSort](#naturalSort)
23
- - [filter](#filter)
24
- - [chunks](#chunks)
25
- - Date Methods
26
- - [parseDate](#parseDate)
27
- - [getDates](#getDates)
28
- - [diffDays](#diffDays)
29
- - [License](#License)
30
-
31
- ## File Methods
32
-
33
- ### globPromise
34
-
35
- > This method returns **Promise** object.
36
-
37
- [Glob](https://github.com/isaacs/node-glob#glob) promise.
38
-
39
- - @param `string` pattern
40
- - @param `GlobOptions` options
41
- - @returns `Promise.<Array<string>, Error>`
42
-
43
- Example
44
-
45
- ``` js
46
- const { globPromise } = require('file-path-helper');
47
-
48
- async function getFiles() {
49
- const files = await globPromise('*.*');
50
- return files;
51
- }
52
- ```
53
-
54
- ### replaceSeparator
55
-
56
- Replace directory separator.
57
-
58
- - @param `string` path
59
- - @param `Separator` separator default: '/'
60
- - @returns `string`
61
-
62
- ### trimDir
63
-
64
- Append last slash to directory.
65
-
66
- - @param `string` dir
67
- - @param `Separator` separator default: '/'
68
-
69
- ### setDir
70
-
71
- Set directory part of path.
72
-
73
- - @param `string` path
74
- - @param `string` dir
75
- - @param `Separator` separator default: '/'
76
- - @returns `string`
77
-
78
- Example
79
-
80
- ``` js
81
- const { setDir } = require('file-path-helper');
82
-
83
- const newPath = setDir('dir/old/file.txt', 'new');
84
- // newPath = 'new/file.txt'
85
- ```
86
-
87
- Set directory part of path.
88
-
89
- ### getLastNumber
90
-
91
- Get last number from path.
92
-
93
- - @param `string` path
94
- - @returns `string`
95
-
96
- Example
97
-
98
- ``` js
99
- const { getLastNumber } = require('file-path-helper');
100
-
101
- const num = getLastNumber('my-favorite-13.txt');
102
- // num = '13'
103
- ```
104
-
105
- ### removeLastNumber
106
-
107
- Remove last number from file name.
108
-
109
- - @param `string` file
110
- - @returns `string`
111
-
112
- Example
113
-
114
- ``` js
115
- const { removeLastNumber } = require('file-path-helper');
116
-
117
- const file = removeLastNumber('my-favorite-13.txt');
118
- // file = 'my-favorite.txt'
119
- ```
120
-
121
- ### autoIncrease
122
-
123
- > This method returns **Promise** object.
124
-
125
- If the same file exists, It's returns filename what increased number.
126
-
127
- - @param `string` path
128
- - @returns `Promise<string>` auto increased path.
129
-
130
- Example
131
-
132
- ``` js
133
- const { autoIncrease } = require('file-path-helper');
134
-
135
- const file = await autoIncrease('dogs.txt');
136
- // file = 'dogs (2).txt';
137
- ```
138
-
139
- ### resolveOutputFile
140
-
141
- Resolve output filename using templates such as `{name}`, `{source}` or `{ext}`.
142
-
143
- - @param `string` output
144
- - @param `string` source
145
- - @returns `string`
146
-
147
- Example
148
-
149
- ``` js
150
- const { resolveOutputFile } = require('file-path-helper');
151
-
152
- const output = resolveOutputFile('{name}-fixed.{ext}', 'dogs.txt');
153
- // output = 'dogs-fixed.txt'
154
- ```
155
-
156
- ### bytesToSize
157
-
158
- Converts bytes to human readable size. e.g. `10 MB` `1.25 GB`
159
-
160
- - @param `number` bytes
161
- - @param `number` decimals - default: 2
162
- - @returns `string`
163
-
164
- Example
165
-
166
- ``` js
167
- const { bytesToSize } = require('file-path-helper');
168
-
169
- const fileSize = bytesToSize(2048);
170
- // fileSize = '2 KB'
171
- ```
172
-
173
- ### parseSize
174
-
175
- Parses string that includes file size and operator.
176
-
177
- ``` ts
178
- interface Size {
179
- bytes: number;
180
- operator: string; // '>', '>=', '=' ...
181
- }
182
- ```
183
-
184
- - @param `string` size - e.g `1kb` `10.5 MB` `>1gb` `=< 10 kb`
185
- - @returns `Size`
186
-
187
- Example
188
-
189
- ``` js
190
- const { parseSize } = require('file-path-helper');
191
-
192
- const size = parseSize('> 1mb');
193
- // size.bytes = 1048576
194
- // size.operator = '>'
195
- ```
196
-
197
- ## String Methods
198
-
199
- ### truncate
200
-
201
- Truncate string what given length.
202
-
203
- - @param `string` str
204
- - @param `number` length
205
- - @param `string` ellipsis - default: `'…'`
206
- - @returns `string`
207
-
208
- Example
209
-
210
- ``` js
211
- const { truncate } = require('file-path-helper');
212
-
213
- const str = truncate('1234567890', 6);
214
- // str = '12345…'
215
- ```
216
-
217
- ### sanitize
218
-
219
- Sanitize string for filename safe.
220
-
221
- - @param `string` str
222
- - @returns `string` replacer - default: `''`
223
-
224
- Example
225
-
226
- ``` js
227
- const { sanitize } = require('file-path-helper');
228
-
229
- const str = sanitize(' he*llo/_<wo:rld');
230
- // str = 'hello_world'
231
- ```
232
-
233
- ## Array Methods
234
-
235
- ### naturalSort
236
-
237
- Sorting array of alphanumerical strings naturally.
238
-
239
- - @param `string[]` arr
240
- - @returns `string[]`
241
-
242
- Example
243
-
244
- ``` js
245
- const arr = [
246
- 'test 1.txt',
247
- 'test 11.txt',
248
- 'test 3.txt',
249
- ];
250
- const sorted = naturalSort(arr);
251
- // sorted = [
252
- // 'test 1.txt',
253
- // 'test 3.txt',
254
- // 'test 11.txt',
255
- // ];
256
- ```
257
-
258
- ### filter
259
-
260
- Filtering an array with `Promise`.
261
-
262
- - @param `T[]` arr - filtering target array.
263
- - @param `function(T, number, T[]): Promise<boolean>` cb - callback function for filtering. arguments is value, index, array.
264
- - @returns `Promise<T[]>`
265
-
266
- Example
267
-
268
- ``` js
269
- const arr = [1, 2, 3, 4, 5];
270
- const doSomething = () => Promise.resolve();
271
-
272
- const res = await filter(arr, async v => {
273
- await doSomething();
274
- return (v % 2) == 1;
275
- });
276
- // res = [1, 3, 5]
277
- ```
278
-
279
- ### chunks
280
-
281
- Split array into chunks.
282
-
283
- - @param `T[]` arr
284
- - @param `number` size
285
- - @returns `T[][]`
286
-
287
- Example
288
-
289
- ``` js
290
- const arr = [1, 2, 3, 4, 5, 6, 7];
291
- const res = chunks(arr, 3);
292
- // res = [[1, 2, 3], [4, 5, 6], [7]]
293
- ```
294
-
295
- ## Date Methods
296
-
297
- ### parseDate
298
-
299
- Parsing the value to date. it's useful handling 'date'(not hours and minutes) purpose.
300
-
301
- ``` ts
302
- interface ParsedDate {
303
- date: Date;
304
- year: number;
305
- month: number;
306
- day: number;
307
- toDateString: () => string;
308
- }
309
- ```
310
-
311
- - @param `string`|`number`|`Date` value
312
- - @returns `ParsedDate`
313
-
314
- Example
315
-
316
- ``` js
317
- const parsed = parseDate('feb 17, 1995 03:24:00');
318
- // parsed = {
319
- // date: new Date('feb 17, 1995 03:24:00'), // Date object
320
- // year: 1995,
321
- // month: 2,
322
- // day: 17,
323
- // }
324
- // parsed.toDateString() // '1995-02-17' ISO date format
325
- ```
326
-
327
- ### getDates
328
-
329
- Returns array of date strings.
330
-
331
- - @param `string` value - date string. e.g. '2020-01-01' or '2020-01-01~2020-01-31'
332
- - @returns `string[]`
333
-
334
- Example
335
-
336
- ``` js
337
- const dates = getDates('2020-02-01~2020-02-05');
338
- // dates = [
339
- // '2020-02-01',
340
- // '2020-02-02',
341
- // '2020-02-03',
342
- // '2020-02-04',
343
- // '2020-02-05',
344
- // ]
345
- ```
346
-
347
- ### diffDays
348
-
349
- Returns difference between two dates.
350
-
351
- - @param `string`|`number`|`Date` a
352
- - @param `string`|`number`|`Date` b
353
- - @returns `number`
354
-
355
- Example
356
-
357
- ``` js
358
- const days = diffDays('2020-02-24', '2020-03-02');
359
- // days = 7
360
- ```
361
-
362
- ## License
363
-
364
- [MIT License](https://github.com/archco/file-path-helper/blob/master/LICENSE)
1
+ # File Path Helper
2
+
3
+ Helpful methods for handling file path.
4
+
5
+ ## Table of contents
6
+
7
+ - File Methods
8
+ - [glob](#glob)
9
+ - [replaceSeparator](#replaceSeparator)
10
+ - [trimDir](#trimDir)
11
+ - [setDir](#setDir)
12
+ - [getLastNumber](#getLastNumber)
13
+ - [removeLastNumber](#removeLastNumber)
14
+ - [autoIncrease](#autoIncrease)
15
+ - [resolveOutputFile](#resolveOutputFile)
16
+ - [bytesToSize](#bytesToSize)
17
+ - [parseSize](#parseSize)
18
+ - String Methods
19
+ - [truncate](#truncate)
20
+ - [sanitize](#sanitize)
21
+ - Array Methods
22
+ - [naturalSort](#naturalSort)
23
+ - [filter](#filter)
24
+ - [chunks](#chunks)
25
+ - Date Methods
26
+ - [parseDate](#parseDate)
27
+ - [getDates](#getDates)
28
+ - [diffDays](#diffDays)
29
+ - [License](#License)
30
+
31
+ ## File Methods
32
+
33
+ ### glob
34
+
35
+ > This method returns **Promise** object.
36
+
37
+ [Glob](https://github.com/isaacs/node-glob#glob)
38
+
39
+ - @param `string` pattern
40
+ - @param `GlobOptions` options
41
+ - @returns `Promise.<Array<string>, Error>`
42
+
43
+ Example
44
+
45
+ ``` js
46
+ import { glob } from 'file-path-helper';
47
+
48
+ async function getFiles() {
49
+ const files = await glob('*.*');
50
+ return files;
51
+ }
52
+ ```
53
+
54
+ ### replaceSeparator
55
+
56
+ Replace directory separator.
57
+
58
+ - @param `string` path
59
+ - @param `Separator` separator default: '/'
60
+ - @returns `string`
61
+
62
+ ### trimDir
63
+
64
+ Append last slash to directory.
65
+
66
+ - @param `string` dir
67
+ - @param `Separator` separator default: '/'
68
+
69
+ ### setDir
70
+
71
+ Set directory part of path.
72
+
73
+ - @param `string` path
74
+ - @param `string` dir
75
+ - @param `Separator` separator default: '/'
76
+ - @returns `string`
77
+
78
+ Example
79
+
80
+ ``` js
81
+ import { setDir } from 'file-path-helper';
82
+
83
+ const newPath = setDir('dir/old/file.txt', 'new');
84
+ // newPath = 'new/file.txt'
85
+ ```
86
+
87
+ Set directory part of path.
88
+
89
+ ### getLastNumber
90
+
91
+ Get last number from path.
92
+
93
+ - @param `string` path
94
+ - @returns `string`
95
+
96
+ Example
97
+
98
+ ``` js
99
+ import { getLastNumber } from 'file-path-helper';
100
+
101
+ const num = getLastNumber('my-favorite-13.txt');
102
+ // num = '13'
103
+ ```
104
+
105
+ ### removeLastNumber
106
+
107
+ Remove last number from file name.
108
+
109
+ - @param `string` file
110
+ - @returns `string`
111
+
112
+ Example
113
+
114
+ ``` js
115
+ import { removeLastNumber } from 'file-path-helper';
116
+
117
+ const file = removeLastNumber('my-favorite-13.txt');
118
+ // file = 'my-favorite.txt'
119
+ ```
120
+
121
+ ### autoIncrease
122
+
123
+ > This method returns **Promise** object.
124
+
125
+ If the same file exists, It's returns filename what increased number.
126
+
127
+ - @param `string` path
128
+ - @returns `Promise<string>` auto increased path.
129
+
130
+ Example
131
+
132
+ ``` js
133
+ import { autoIncrease } from 'file-path-helper';
134
+
135
+ const file = await autoIncrease('dogs.txt');
136
+ // file = 'dogs (2).txt';
137
+ ```
138
+
139
+ ### resolveOutputFile
140
+
141
+ Resolve output filename using templates such as `{name}`, `{source}` or `{ext}`.
142
+
143
+ - @param `string` output
144
+ - @param `string` source
145
+ - @returns `string`
146
+
147
+ Example
148
+
149
+ ``` js
150
+ import { resolveOutputFile } from 'file-path-helper';
151
+
152
+ const output = resolveOutputFile('{name}-fixed.{ext}', 'dogs.txt');
153
+ // output = 'dogs-fixed.txt'
154
+ ```
155
+
156
+ ### bytesToSize
157
+
158
+ Converts bytes to human readable size. e.g. `10 MB` `1.25 GB`
159
+
160
+ - @param `number` bytes
161
+ - @param `number` decimals - default: 2
162
+ - @returns `string`
163
+
164
+ Example
165
+
166
+ ``` js
167
+ import { bytesToSize } from 'file-path-helper';
168
+
169
+ const fileSize = bytesToSize(2048);
170
+ // fileSize = '2 KB'
171
+ ```
172
+
173
+ ### parseSize
174
+
175
+ Parses string that includes file size and operator.
176
+
177
+ ``` ts
178
+ interface Size {
179
+ bytes: number;
180
+ operator: string; // '>', '>=', '=' ...
181
+ }
182
+ ```
183
+
184
+ - @param `string` size - e.g `1kb` `10.5 MB` `>1gb` `=< 10 kb`
185
+ - @returns `Size`
186
+
187
+ Example
188
+
189
+ ``` js
190
+ import { parseSize } from 'file-path-helper';
191
+
192
+ const size = parseSize('> 1mb');
193
+ // size.bytes = 1048576
194
+ // size.operator = '>'
195
+ ```
196
+
197
+ ## String Methods
198
+
199
+ ### truncate
200
+
201
+ Truncate string what given length.
202
+
203
+ - @param `string` str
204
+ - @param `number` length
205
+ - @param `string` ellipsis - default: `'…'`
206
+ - @returns `string`
207
+
208
+ Example
209
+
210
+ ``` js
211
+ import { truncate } from 'file-path-helper';
212
+
213
+ const str = truncate('1234567890', 6);
214
+ // str = '12345…'
215
+ ```
216
+
217
+ ### sanitize
218
+
219
+ Sanitize string for filename safe.
220
+
221
+ - @param `string` str
222
+ - @returns `string` replacer - default: `''`
223
+
224
+ Example
225
+
226
+ ``` js
227
+ import { sanitize } from 'file-path-helper';
228
+
229
+ const str = sanitize(' he*llo/_<wo:rld');
230
+ // str = 'hello_world'
231
+ ```
232
+
233
+ ## Array Methods
234
+
235
+ ### naturalSort
236
+
237
+ Sorting array of alphanumerical strings naturally.
238
+
239
+ - @param `string[]` arr
240
+ - @returns `string[]`
241
+
242
+ Example
243
+
244
+ ``` js
245
+ const arr = [
246
+ 'test 1.txt',
247
+ 'test 11.txt',
248
+ 'test 3.txt',
249
+ ];
250
+ const sorted = naturalSort(arr);
251
+ // sorted = [
252
+ // 'test 1.txt',
253
+ // 'test 3.txt',
254
+ // 'test 11.txt',
255
+ // ];
256
+ ```
257
+
258
+ ### filter
259
+
260
+ Filtering an array with `Promise`.
261
+
262
+ - @param `T[]` arr - filtering target array.
263
+ - @param `function(T, number, T[]): Promise<boolean>` cb - callback function for filtering. arguments is value, index, array.
264
+ - @returns `Promise<T[]>`
265
+
266
+ Example
267
+
268
+ ``` js
269
+ const arr = [1, 2, 3, 4, 5];
270
+ const doSomething = () => Promise.resolve();
271
+
272
+ const res = await filter(arr, async v => {
273
+ await doSomething();
274
+ return (v % 2) == 1;
275
+ });
276
+ // res = [1, 3, 5]
277
+ ```
278
+
279
+ ### chunks
280
+
281
+ Split array into chunks.
282
+
283
+ - @param `T[]` arr
284
+ - @param `number` size
285
+ - @returns `T[][]`
286
+
287
+ Example
288
+
289
+ ``` js
290
+ const arr = [1, 2, 3, 4, 5, 6, 7];
291
+ const res = chunks(arr, 3);
292
+ // res = [[1, 2, 3], [4, 5, 6], [7]]
293
+ ```
294
+
295
+ ## Date Methods
296
+
297
+ ### parseDate
298
+
299
+ Parsing the value to date. it's useful handling 'date'(not hours and minutes) purpose.
300
+
301
+ ``` ts
302
+ interface ParsedDate {
303
+ date: Date;
304
+ year: number;
305
+ month: number;
306
+ day: number;
307
+ toDateString: () => string;
308
+ }
309
+ ```
310
+
311
+ - @param `string`|`number`|`Date` value
312
+ - @returns `ParsedDate`
313
+
314
+ Example
315
+
316
+ ``` js
317
+ const parsed = parseDate('feb 17, 1995 03:24:00');
318
+ // parsed = {
319
+ // date: new Date('feb 17, 1995 03:24:00'), // Date object
320
+ // year: 1995,
321
+ // month: 2,
322
+ // day: 17,
323
+ // }
324
+ // parsed.toDateString() // '1995-02-17' ISO date format
325
+ ```
326
+
327
+ ### getDates
328
+
329
+ Returns array of date strings.
330
+
331
+ - @param `string` value - date string. e.g. '2020-01-01' or '2020-01-01~2020-01-31'
332
+ - @returns `string[]`
333
+
334
+ Example
335
+
336
+ ``` js
337
+ const dates = getDates('2020-02-01~2020-02-05');
338
+ // dates = [
339
+ // '2020-02-01',
340
+ // '2020-02-02',
341
+ // '2020-02-03',
342
+ // '2020-02-04',
343
+ // '2020-02-05',
344
+ // ]
345
+ ```
346
+
347
+ ### diffDays
348
+
349
+ Returns difference between two dates.
350
+
351
+ - @param `string`|`number`|`Date` a
352
+ - @param `string`|`number`|`Date` b
353
+ - @returns `number`
354
+
355
+ Example
356
+
357
+ ``` js
358
+ const days = diffDays('2020-02-24', '2020-03-02');
359
+ // days = 7
360
+ ```
361
+
362
+ ## License
363
+
364
+ [MIT License](https://github.com/archco/file-path-helper/blob/master/LICENSE)