file-path-helper 1.4.2 → 1.4.5
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/.editorconfig +12 -12
- package/.eslintrc.json +18 -18
- package/CHANGELOG.md +13 -0
- package/README.md +364 -364
- package/index.d.ts +206 -206
- package/package.json +6 -6
- package/src/arr.js +48 -48
- package/src/date.js +77 -77
- package/src/index.js +11 -11
- package/src/str.js +5 -2
- package/test/utils.test.js +2 -0
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
|
-
module.exports = {
|
|
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
|
+
module.exports = {
|
|
74
|
+
parseDate,
|
|
75
|
+
getDates,
|
|
76
|
+
diffDays,
|
|
77
|
+
};
|
package/src/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
const arrUtils = require('./arr');
|
|
2
|
-
const dateUtils = require('./date');
|
|
3
|
-
const fileUtils = require('./file');
|
|
4
|
-
const strUtils = require('./str');
|
|
5
|
-
|
|
6
|
-
module.exports = Object.assign({},
|
|
7
|
-
arrUtils,
|
|
8
|
-
dateUtils,
|
|
9
|
-
fileUtils,
|
|
10
|
-
strUtils,
|
|
11
|
-
);
|
|
1
|
+
const arrUtils = require('./arr');
|
|
2
|
+
const dateUtils = require('./date');
|
|
3
|
+
const fileUtils = require('./file');
|
|
4
|
+
const strUtils = require('./str');
|
|
5
|
+
|
|
6
|
+
module.exports = Object.assign({},
|
|
7
|
+
arrUtils,
|
|
8
|
+
dateUtils,
|
|
9
|
+
fileUtils,
|
|
10
|
+
strUtils,
|
|
11
|
+
);
|
package/src/str.js
CHANGED
|
@@ -9,19 +9,22 @@
|
|
|
9
9
|
function truncate(str, length = 40, ellipsis = '…') {
|
|
10
10
|
return str.length < length
|
|
11
11
|
? str
|
|
12
|
-
: str.
|
|
12
|
+
: str.substring(0, length - ellipsis.length) + ellipsis;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Sanitize string for safe filename.
|
|
17
17
|
* @see https://github.com/parshap/node-sanitize-filename#readme
|
|
18
|
+
* @see https://en.wikipedia.org/wiki/C0_and_C1_control_codes
|
|
18
19
|
*
|
|
19
20
|
* @param {string} str
|
|
20
21
|
* @param {string} replacer default: `''`
|
|
21
22
|
* @returns {string}
|
|
22
23
|
*/
|
|
23
24
|
function sanitize(str, replacer = '') {
|
|
24
|
-
|
|
25
|
+
// eslint-disable-next-line no-control-regex
|
|
26
|
+
return str.replace(/[\x00-\x1f\x80-\x9f]/g, replacer) // control codes
|
|
27
|
+
.replace(/(https|http)/g, '') // remove 'http'
|
|
25
28
|
.replace(/\t|[ ]{2,}/g, ' ') // two spaces or tab -> one space
|
|
26
29
|
.replace(/[/:?<>\\*|"]/g, replacer) // sanitize
|
|
27
30
|
.trim();
|
package/test/utils.test.js
CHANGED
|
@@ -224,6 +224,7 @@ describe('#sanitize', () => {
|
|
|
224
224
|
expect(sanitize(' he*llo?', ' ')).toBe('he llo');
|
|
225
225
|
expect(sanitize(' he*llo/_<wo:rld')).toBe('hello_world');
|
|
226
226
|
expect(sanitize('https://github.com/')).toBe('github.com');
|
|
227
|
+
expect(sanitize('string\btest', ' ')).toBe('string test');
|
|
227
228
|
});
|
|
228
229
|
});
|
|
229
230
|
|
|
@@ -231,6 +232,7 @@ describe('#truncate', () => {
|
|
|
231
232
|
it('truncate string', () => {
|
|
232
233
|
expect(truncate('1234567890', 6)).toBe('12345…');
|
|
233
234
|
expect(truncate('1234567890', 8, '...')).toBe('12345...');
|
|
235
|
+
expect(truncate('a가나다라bcd', 3, '')).toBe('a가나');
|
|
234
236
|
});
|
|
235
237
|
});
|
|
236
238
|
|