-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
185 lines (162 loc) · 5.31 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
'use strict';
const fs = require('fs');
const path = require('path');
const strip = require('strip-filename-increment');
const ordinals = ['th', 'st', 'nd', 'rd'];
const ordinal = n => {
if (isNaN(n)) {
throw new TypeError('expected a number');
}
return ordinals[((n % 100) - 20) % 10] || ordinals[n % 100] || ordinals[0];
};
const toOrdinal = number => {
return `${Number(number)}${ordinal(Math.abs(number))}`;
};
const format = {
darwin(stem, n) {
if (n === 1) return `${stem} copy`;
if (n > 1) return `${stem} copy ${n}`;
return stem;
},
default: (stem, n) => n > 1 ? `${stem} (${n})` : stem,
win32: (stem, n) => n > 1 ? `${stem} (${n})` : stem,
windows: (stem, n) => format.win32(stem, n),
linux(stem, n) {
if (n === 0) return stem;
if (n === 1) return `${stem} (copy)`;
if (n === 2) return `${stem} (another copy)`;
return `${stem} (${toOrdinal(n)} copy)`;
}
};
/**
* The main export is a function that adds a trailing increment to
* the `stem` (basename without extension) of the given file path or object.
* ```js
* console.log(increment('foo/bar.txt', { platform: 'darwin' }));
* //=> foo/bar copy.txt
* console.log(increment('foo/bar.txt', { platform: 'linux' }));
* //=> foo/bar (copy).txt
* console.log(increment('foo/bar.txt', { platform: 'win32' }));
* //=> foo/bar (2).txt
* ```
* @name increment
* @param {String|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 a file of the same type that was given, with an increment added to the file name.
* @api public
*/
const increment = (...args) => {
return typeof args[0] === 'string' ? increment.path(...args) : increment.file(...args);
};
/**
* Add a trailing increment to the given `filepath`.
*
* ```js
* console.log(increment.path('foo/bar.txt', { platform: 'darwin' }));
* //=> foo/bar copy.txt
* console.log(increment.path('foo/bar.txt', { platform: 'linux' }));
* //=> foo/bar (copy).txt
* console.log(increment.path('foo/bar.txt', { platform: 'win32' }));
* //=> foo/bar (2).txt
* ```
* @name .path
* @param {String} `filepath`
* @param {Object} `options` See [available options](#options).
* @return {String}
* @api public
*/
increment.path = (filepath, options = {}) => {
return path.format(increment.file(path.parse(filepath), options));
};
/**
* Add a trailing increment to the `file.base` of the given file object.
*
* ```js
* console.log(increment.file({ path: 'foo/bar.txt' }, { platform: 'darwin' }));
* //=> { path: 'foo/bar copy.txt', base: 'bar copy.txt' }
* console.log(increment.file({ path: 'foo/bar.txt' }, { platform: 'linux' }));
* //=> { path: 'foo/bar (copy).txt', base: 'bar (copy).txt' }
* console.log(increment.file({ path: 'foo/bar.txt' }, { platform: 'win32' }));
* //=> { path: 'foo/bar (2).txt', base: 'bar (2).txt' }
* ```
* @name .file
* @param {String|Object} `file` If passed as a string, the path will be parsed to create an object using `path.parse()`.
* @param {Object} `options` See [available options](#options).
* @return {Object} Returns an object.
* @api public
*/
increment.file = (file, options = {}) => {
if (typeof file === 'string') {
let temp = file;
file = path.parse(file);
file.path = temp;
}
file = { ...file };
if (file.path && Object.keys(file).length === 1) {
let temp = file.path;
file = path.parse(file.path);
file.path = temp;
}
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;
let { start = 1, platform = process.platform } = options;
let fn = options.increment || format[platform] || format.default;
if (start === 1 && (platform === 'win32' || platform === 'windows')) {
if (!options.increment) {
start++;
}
}
if (options.strip === true) {
file.name = strip.increment(file.name, options);
file.dir = strip.increment(file.dir, options);
file.base = file.name + file.ext;
}
if (options.fs === true) {
let name = file.name;
let dest = path.format(file);
while (fs.existsSync(dest)) {
file.base = fn(name, start++) + file.ext;
dest = path.format(file);
}
} else {
file.base = fn(file.name, start) + file.ext;
}
file.path = path.join(file.dir, file.base);
return file;
};
/**
* Returns an ordinal-suffix for the given number. This is used
* when creating increments for files on Linux.
*
* ```js
* const { ordinal } = require('add-filename-increment');
* console.log(ordinal(1)); //=> 'st'
* console.log(ordinal(2)); //=> 'nd'
* console.log(ordinal(3)); //=> 'rd'
* console.log(ordinal(110)); //=> 'th'
* ```
* @name .ordinal
* @param {Number} `num`
* @return {String}
* @api public
*/
increment.ordinal = ordinal;
/**
* Returns an ordinal for the given number.
*
* ```js
* const { toOrdinal } = require('add-filename-increment');
* console.log(toOrdinal(1)); //=> '1st'
* console.log(toOrdinal(2)); //=> '2nd'
* console.log(toOrdinal(3)); //=> '3rd'
* console.log(toOrdinal(110)); //=> '110th'
* ```
* @name .toOrdinal
* @param {Number} `num`
* @return {String}
* @api public
*/
increment.toOrdinal = toOrdinal;
module.exports = increment;