file-path-helper 1.4.0 → 1.4.4

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/index.d.ts ADDED
@@ -0,0 +1,206 @@
1
+ import { IOptions } 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: IOptions): 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/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "file-path-helper",
3
- "version": "1.4.0",
3
+ "version": "1.4.4",
4
+ "types": "index.d.ts",
4
5
  "description": "Helpful methods for handling file path.",
5
6
  "main": "src/index.js",
6
7
  "scripts": {
@@ -12,12 +13,12 @@
12
13
  "author": "archco",
13
14
  "license": "MIT",
14
15
  "dependencies": {
15
- "fs-extra": "^8.1.0",
16
- "glob": "^7.1.6"
16
+ "fs-extra": "^10.0.0",
17
+ "glob": "^7.2.0"
17
18
  },
18
19
  "devDependencies": {
19
- "@types/jest": "^25.1.2",
20
- "eslint": "^6.8.0",
21
- "jest": "^25.1.0"
20
+ "@types/jest": "^27.4.0",
21
+ "eslint": "^8.8.0",
22
+ "jest": "^27.4.7"
22
23
  }
23
24
  }
package/src/arr.js CHANGED
@@ -1,48 +1,48 @@
1
- /**
2
- * Sorting array of alphanumerical strings naturally.
3
- * @link 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
- module.exports = {
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
+ module.exports = {
45
+ naturalSort,
46
+ filter,
47
+ chunks,
48
+ };
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/file.js CHANGED
@@ -12,7 +12,7 @@ const { pathExists } = require('fs-extra');
12
12
  *
13
13
  * @param {string} pattern
14
14
  * @param {GlobOptions} options
15
- * @returns {Promise.<Array<String>, Error>}
15
+ * @returns {Promise<string[], Error>}
16
16
  */
17
17
  function globPromise(pattern, options) {
18
18
  return new Promise((resolve, reject) => {
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
@@ -2,26 +2,29 @@
2
2
  * truncate string.
3
3
  *
4
4
  * @param {string} str
5
- * @param {number} length
6
- * @param {string} ellipsis
5
+ * @param {number} length default: 40
6
+ * @param {string} ellipsis default: '…'
7
7
  * @returns {string}
8
8
  */
9
9
  function truncate(str, length = 40, ellipsis = '…') {
10
10
  return str.length < length
11
11
  ? str
12
- : str.substr(0, length - ellipsis.length) + ellipsis;
12
+ : str.substring(0, length - ellipsis.length) + ellipsis;
13
13
  }
14
14
 
15
15
  /**
16
16
  * Sanitize string for safe filename.
17
- * @link https://github.com/parshap/node-sanitize-filename#readme
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
- return str.replace(/(https|http)/g, '') // remove 'http'
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();
@@ -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