-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
203 lines (180 loc) · 7.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*!
* file-name <https://github.com/jonschlinkert/file-name>
*
* Copyright (c) 2015-present, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
const path = require('path');
const isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val);
const constants = {
REGEX_DARWIN: /( copy( [0-9]+)?)+$/i,
REGEX_DEFAULT: /(( copy)?( \([0-9]+\)|[0-9]+)?)+$/i,
REGEX_WIN32: /( \([0-9]+\))+$/i,
REGEX_NON_STANDARD: /( \.\(incomplete\)| \([0-9]+\)|[- ]+)+$/i,
REGEX_LINUX: /( \(((another|[0-9]+(th|st|nd|rd)) )?copy\))+$/i,
REGEX_RAW_NUMBERS: '| [0-9]+',
REGEX_SOURCE: ' \\((?:(another|[0-9]+(th|st|nd|rd)) )?copy\\)|copy( [0-9]+)?|\\.\\(incomplete\\)| \\([0-9]+\\)|[- ]+'
};
/**
* Remove trailing increments from the `dirname` and/or `stem` (basename
* without extension) of the given file path or object.
*
* @name strip
* @param {Sring|Object} `file` If the file is an object, it must have a `path` property.
* @param {Object} `options` See [available options](#options).
* @return {String|Object} Returns the same type that was given.
* @api public
*/
const strip = (file, options) => {
if (!file) return file;
if (isObject(file) && file.path) {
return strip.file(file, options);
}
let filepath = strip.increment(file, options);
let extname = path.extname(filepath);
let dirname = strip.increment(path.dirname(filepath), options);
let stem = strip.increment(path.basename(filepath, extname), options);
return path.join(dirname, stem + extname);
};
/**
* Removes trailing increments from the given string.
*
* ```js
* console.log(strip.increment('foo (2)')); => 'foo'
* console.log(strip.increment('foo (copy)')); => 'foo'
* console.log(strip.increment('foo copy 2')); => 'foo'
* ```
* @name .increment
* @param {String} `input`
* @param {Object} `options` See [available options](#options).
* @return {String}
* @api public
*/
strip.increment = (input, options = {}) => {
if (typeof input === 'string' && input !== '') {
let suffix = options.removeRawNumbers === true ? constants.REGEX_RAW_NUMBERS : '';
let source = constants.REGEX_SOURCE + suffix;
return input.replace(new RegExp(`(${source})+$`, 'i'), '');
}
return input;
};
/**
* Removes trailing increments and returns the `dirname` of the given `filepath`.
*
* ```js
* console.log(strip.dirname('foo (2)/bar.txt')); => 'foo'
* console.log(strip.dirname('foo (copy)/bar.txt')); => 'foo'
* console.log(strip.dirname('foo copy 2/bar.txt')); => 'foo'
* ```
* @name .dirname
* @param {String} `filepath`
* @param {Object} `options` See [available options](#options).
* @return {String} Returns the `dirname` of the filepath, without increments.
* @api public
*/
strip.dirname = (filepath, options) => {
return strip.increment(path.dirname(filepath), options);
};
/**
* Removes trailing increments and returns the `stem` of the given `filepath`.
*
* ```js
* console.log(strip.stem('foo/bar (2).txt')); //=> 'bar'
* console.log(strip.stem('foo/bar (copy).txt')); //=> 'bar'
* console.log(strip.stem('foo/bar copy 2.txt')); //=> 'bar'
* console.log(strip.stem('foo/bar (2) copy.txt')); //=> 'bar'
* console.log(strip.stem('foo/bar (2) - copy.txt')); //=> 'bar'
* ```
* @name .stem
* @param {String} `filepath`
* @param {Object} `options` See [available options](#options).
* @return {String} Returns the `stem` of the filepath, without increments.
* @api public
*/
strip.stem = (filepath, options) => {
return strip.increment(path.basename(filepath, path.extname(filepath)), options);
};
/**
* Removes trailing increments and returns the `basename` of the given `filepath`.
*
* ```js
* console.log(strip.basename('foo/bar (2).txt')); //=> 'bar.txt'
* console.log(strip.basename('foo/bar (copy).txt')); //=> 'bar.txt'
* console.log(strip.basename('foo/bar copy 2.txt')); //=> 'bar.txt'
* console.log(strip.basename('foo/bar (2) copy.txt')); //=> 'bar.txt'
* console.log(strip.basename('foo/bar (2) - copy.txt')); //=> 'bar.txt'
* ```
* @name .basename
* @param {String} `filepath`
* @param {Object} `options` See [available options](#options).
* @return {String} Returns the `basename` of the filepath, without increments.
* @api public
*/
strip.basename = (filepath, options) => {
let extname = path.extname(filepath);
let stem = path.basename(filepath, extname);
return strip.increment(stem, options) + extname;
};
/**
* Removes trailing increments from the `dirname` and `stem` of the given `filepath`.
*
* ```js
* console.log(strip.path('foo copy/bar (2).txt')); //=> 'foo/bar.txt'
* console.log(strip.path('foo (2)/bar (copy).txt')); //=> 'foo/bar.txt'
* console.log(strip.path('foo (2)/bar copy 2.txt')); //=> 'foo/bar.txt'
* console.log(strip.path('foo copy/bar (2) copy.txt')); //=> 'foo/bar.txt'
* console.log(strip.path('foo copy/bar (2) - copy.txt')); //=> 'foo/bar.txt'
* ```
* @name .path
* @param {String} `filepath`
* @param {Object} `options` See [available options](#options).
* @return {String} Returns the `basename` of the filepath, without increments.
* @api public
*/
strip.path = (filepath, options) => {
let extname = path.extname(filepath);
let stem = strip.increment(path.basename(filepath, extname), options);
let dirname = strip.increment(path.dirname(filepath), options);
return path.join(dirname, stem + extname);
};
/**
* Removes trailing increments from the `dirname` and `stem` properties
* of the given `file`.
*
* ```js
* console.log(strip({ path: 'foo copy/bar (2).txt' }));
* //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
* console.log(strip({ path: 'foo (2)/bar (copy).txt' }));
* //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
* console.log(strip({ path: 'foo (2)/bar copy 2.txt' }));
* //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
* console.log(strip({ path: 'foo copy/bar (2) copy.txt' }));
* //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
* console.log(strip({ path: 'foo copy/bar (2) - copy.txt' }));
* //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
* ```
* @name .file
* @param {String} `filepath`
* @param {Object} `options` See [available options](#options).
* @return {String} Returns the `basename` of the filepath, without increments.
* @api public
*/
strip.file = (file, options = {}) => {
if (!isObject(file)) return file;
if (!file.path) return file;
if (file.dirname && !file.dir) file.dir = file.dirname;
if (file.basename && !file.base) file.base = file.basename;
if (file.extname && !file.ext) file.ext = file.extname;
if (file.stem && !file.name) file.name = file.stem;
if (file.dir === void 0) file.dir = path.dirname(file.path);
if (file.ext === void 0) file.ext = path.extname(file.path);
if (file.base === void 0) file.base = path.basename(file.path);
if (file.name === void 0) file.name = path.basename(file.path, file.ext);
file.name = strip.increment(file.name, options);
file.dir = strip.increment(file.dir, options);
file.base = file.name + file.ext;
file.path = path.join(file.dir, file.base);
return file;
};
module.exports = strip;