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/src/date.js
CHANGED
|
@@ -1,77 +1,77 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Parsing the value to date. it's useful handling 'date'(not hours and minutes) purpose.
|
|
3
|
-
*
|
|
4
|
-
* @typedef ParsedDate
|
|
5
|
-
* @property {Date} date
|
|
6
|
-
* @property {number} year
|
|
7
|
-
* @property {number} month
|
|
8
|
-
* @property {number} day
|
|
9
|
-
* @property {function(): string} toDateString e.g. '2019-12-25'
|
|
10
|
-
*
|
|
11
|
-
* @param {string|number|Date} value
|
|
12
|
-
* @returns {ParsedDate}
|
|
13
|
-
*/
|
|
14
|
-
function parseDate(value) {
|
|
15
|
-
const date = new Date(value);
|
|
16
|
-
const twoDigit = x => x.toString().padStart(2, 0);
|
|
17
|
-
return {
|
|
18
|
-
date,
|
|
19
|
-
year: date.getFullYear(),
|
|
20
|
-
month: date.getMonth() + 1,
|
|
21
|
-
day: date.getDate(),
|
|
22
|
-
toDateString: function () {
|
|
23
|
-
return `${this.year}-${twoDigit(this.month)}-${twoDigit(this.day)}`;
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Get array of date strings
|
|
30
|
-
*
|
|
31
|
-
* @param {string} value date string. e.g. '2020-01-01~2020-01-03'
|
|
32
|
-
* @returns {string[]} e.g. ['2020-01-01', '2020-01-02', '2020-01-03']
|
|
33
|
-
*/
|
|
34
|
-
function getDates(value) {
|
|
35
|
-
/** @type {function(string[]): Date[]} */
|
|
36
|
-
const sortedDates = dates => dates.map(d => new Date(d)).sort((a, b) => a - b);
|
|
37
|
-
/** @type {function(Date, number): Date} */
|
|
38
|
-
const addDays = (d, days) => {
|
|
39
|
-
const r = new Date(d);
|
|
40
|
-
r.setDate(r.getDate() + days);
|
|
41
|
-
return r;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (/~/.test(value)) {
|
|
45
|
-
const [from, until] = sortedDates(value.split('~'));
|
|
46
|
-
const dates = [];
|
|
47
|
-
for (let i = 0; i <= diffDays(from, until); i++) {
|
|
48
|
-
dates.push(parseDate(addDays(from, i)).toDateString());
|
|
49
|
-
}
|
|
50
|
-
return dates;
|
|
51
|
-
} else {
|
|
52
|
-
return [value];
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* difference between two dates.
|
|
58
|
-
* @see https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript
|
|
59
|
-
*
|
|
60
|
-
* @param {string|number|Date} a
|
|
61
|
-
* @param {string|number|Date} b
|
|
62
|
-
* @returns {number}
|
|
63
|
-
*/
|
|
64
|
-
function diffDays(a, b) {
|
|
65
|
-
const msPerDay = 1000 * 60 * 60 * 24;
|
|
66
|
-
const [utc1, utc2] = [a, b].map(x => {
|
|
67
|
-
const d = new Date(x);
|
|
68
|
-
return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());
|
|
69
|
-
});
|
|
70
|
-
return Math.floor((utc2 - utc1) / msPerDay);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
parseDate,
|
|
75
|
-
getDates,
|
|
76
|
-
diffDays,
|
|
77
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Parsing the value to date. it's useful handling 'date'(not hours and minutes) purpose.
|
|
3
|
+
*
|
|
4
|
+
* @typedef ParsedDate
|
|
5
|
+
* @property {Date} date
|
|
6
|
+
* @property {number} year
|
|
7
|
+
* @property {number} month
|
|
8
|
+
* @property {number} day
|
|
9
|
+
* @property {function(): string} toDateString e.g. '2019-12-25'
|
|
10
|
+
*
|
|
11
|
+
* @param {string|number|Date} value
|
|
12
|
+
* @returns {ParsedDate}
|
|
13
|
+
*/
|
|
14
|
+
function parseDate(value) {
|
|
15
|
+
const date = new Date(value);
|
|
16
|
+
const twoDigit = x => x.toString().padStart(2, 0);
|
|
17
|
+
return {
|
|
18
|
+
date,
|
|
19
|
+
year: date.getFullYear(),
|
|
20
|
+
month: date.getMonth() + 1,
|
|
21
|
+
day: date.getDate(),
|
|
22
|
+
toDateString: function () {
|
|
23
|
+
return `${this.year}-${twoDigit(this.month)}-${twoDigit(this.day)}`;
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Get array of date strings
|
|
30
|
+
*
|
|
31
|
+
* @param {string} value date string. e.g. '2020-01-01~2020-01-03'
|
|
32
|
+
* @returns {string[]} e.g. ['2020-01-01', '2020-01-02', '2020-01-03']
|
|
33
|
+
*/
|
|
34
|
+
function getDates(value) {
|
|
35
|
+
/** @type {function(string[]): Date[]} */
|
|
36
|
+
const sortedDates = dates => dates.map(d => new Date(d)).sort((a, b) => a - b);
|
|
37
|
+
/** @type {function(Date, number): Date} */
|
|
38
|
+
const addDays = (d, days) => {
|
|
39
|
+
const r = new Date(d);
|
|
40
|
+
r.setDate(r.getDate() + days);
|
|
41
|
+
return r;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (/~/.test(value)) {
|
|
45
|
+
const [from, until] = sortedDates(value.split('~'));
|
|
46
|
+
const dates = [];
|
|
47
|
+
for (let i = 0; i <= diffDays(from, until); i++) {
|
|
48
|
+
dates.push(parseDate(addDays(from, i)).toDateString());
|
|
49
|
+
}
|
|
50
|
+
return dates;
|
|
51
|
+
} else {
|
|
52
|
+
return [value];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* difference between two dates.
|
|
58
|
+
* @see https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript
|
|
59
|
+
*
|
|
60
|
+
* @param {string|number|Date} a
|
|
61
|
+
* @param {string|number|Date} b
|
|
62
|
+
* @returns {number}
|
|
63
|
+
*/
|
|
64
|
+
function diffDays(a, b) {
|
|
65
|
+
const msPerDay = 1000 * 60 * 60 * 24;
|
|
66
|
+
const [utc1, utc2] = [a, b].map(x => {
|
|
67
|
+
const d = new Date(x);
|
|
68
|
+
return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());
|
|
69
|
+
});
|
|
70
|
+
return Math.floor((utc2 - utc1) / msPerDay);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export {
|
|
74
|
+
parseDate,
|
|
75
|
+
getDates,
|
|
76
|
+
diffDays,
|
|
77
|
+
};
|
package/src/file.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { parse } from 'path';
|
|
2
|
+
import { glob } from 'glob';
|
|
3
|
+
import { pathExists } from 'fs-extra';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @typedef {'/'|'\\'} Separator Directory Separator
|
|
@@ -10,17 +10,12 @@ const { pathExists } = require('fs-extra');
|
|
|
10
10
|
/**
|
|
11
11
|
* Glob promise.
|
|
12
12
|
*
|
|
13
|
+
* @deprecated since version 3.0.0. Use `glob` directly instead.
|
|
13
14
|
* @param {string} pattern
|
|
14
15
|
* @param {GlobOptions} options
|
|
15
16
|
* @returns {Promise<string[], Error>}
|
|
16
17
|
*/
|
|
17
|
-
|
|
18
|
-
return new Promise((resolve, reject) => {
|
|
19
|
-
glob(pattern, options, (err, files) => {
|
|
20
|
-
err === null ? resolve(files) : reject(err);
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
}
|
|
18
|
+
const globPromise = glob;
|
|
24
19
|
|
|
25
20
|
/**
|
|
26
21
|
* Replace directory separator.
|
|
@@ -160,7 +155,8 @@ function parseSize(size) {
|
|
|
160
155
|
};
|
|
161
156
|
}
|
|
162
157
|
|
|
163
|
-
|
|
158
|
+
export {
|
|
159
|
+
glob,
|
|
164
160
|
globPromise,
|
|
165
161
|
replaceSeparator,
|
|
166
162
|
trimDir,
|
package/src/index.js
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
module.exports = Object.assign({},
|
|
7
|
-
arrUtils,
|
|
8
|
-
dateUtils,
|
|
9
|
-
fileUtils,
|
|
10
|
-
strUtils,
|
|
11
|
-
);
|
|
1
|
+
export * from './arr.js';
|
|
2
|
+
export * from './date.js';
|
|
3
|
+
export * from './file.js';
|
|
4
|
+
export * from './str.js';
|
package/src/str.js
CHANGED
package/test/utils.test.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import {
|
|
2
2
|
replaceSeparator,
|
|
3
3
|
trimDir,
|
|
4
4
|
setDir,
|
|
@@ -16,7 +16,7 @@ const {
|
|
|
16
16
|
parseDate,
|
|
17
17
|
diffDays,
|
|
18
18
|
getDates,
|
|
19
|
-
}
|
|
19
|
+
} from '../src/index.js';
|
|
20
20
|
|
|
21
21
|
describe('#replaceSeparator', () => {
|
|
22
22
|
it('can replace back-slash to slash.', () => {
|
|
@@ -84,6 +84,7 @@ describe('#getLastNumber', () => {
|
|
|
84
84
|
expect(getLastNumber('test [21].txt')).toEqual('21');
|
|
85
85
|
expect(getLastNumber('2019_test_{11}.txt')).toEqual('11');
|
|
86
86
|
expect(getLastNumber('2019 test.txt')).toEqual('');
|
|
87
|
+
expect(getLastNumber('domain.com@filename_1.mp4')).toEqual('1');
|
|
87
88
|
});
|
|
88
89
|
});
|
|
89
90
|
|