├── .editorconfig
├── .eslintrc.json
├── .gitattributes
├── .github
└── contributing.md
├── .gitignore
├── .travis.yml
├── .verb.md
├── LICENSE
├── README.md
├── index.js
├── package.json
└── test
├── darwin.js
├── linux.js
├── non-standard.js
└── windows.js
/.editorconfig:
--------------------------------------------------------------------------------
1 | # http://editorconfig.org/
2 | root = true
3 |
4 | [*]
5 | charset = utf-8
6 | end_of_line = lf
7 | indent_size = 2
8 | indent_style = space
9 | insert_final_newline = true
10 | trim_trailing_whitespace = true
11 |
12 | [{**/{actual,fixtures,expected,templates}/**,*.md}]
13 | trim_trailing_whitespace = false
14 | insert_final_newline = false
15 |
--------------------------------------------------------------------------------
/.eslintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": [
3 | "eslint:recommended"
4 | ],
5 |
6 | "env": {
7 | "browser": false,
8 | "es6": true,
9 | "node": true,
10 | "mocha": true
11 | },
12 |
13 | "parserOptions":{
14 | "ecmaVersion": 9,
15 | "sourceType": "module",
16 | "ecmaFeatures": {
17 | "modules": true,
18 | "experimentalObjectRestSpread": true
19 | }
20 | },
21 |
22 | "globals": {
23 | "document": false,
24 | "navigator": false,
25 | "window": false
26 | },
27 |
28 | "rules": {
29 | "accessor-pairs": 2,
30 | "arrow-spacing": [2, { "before": true, "after": true }],
31 | "block-spacing": [2, "always"],
32 | "brace-style": [2, "1tbs", { "allowSingleLine": true }],
33 | "comma-dangle": [2, "never"],
34 | "comma-spacing": [2, { "before": false, "after": true }],
35 | "comma-style": [2, "last"],
36 | "constructor-super": 2,
37 | "curly": [2, "multi-line"],
38 | "dot-location": [2, "property"],
39 | "eol-last": 2,
40 | "eqeqeq": [2, "allow-null"],
41 | "generator-star-spacing": [2, { "before": true, "after": true }],
42 | "handle-callback-err": [2, "^(err|error)$" ],
43 | "indent": [2, 2, { "SwitchCase": 1 }],
44 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }],
45 | "keyword-spacing": [2, { "before": true, "after": true }],
46 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }],
47 | "new-parens": 2,
48 | "no-array-constructor": 2,
49 | "no-caller": 2,
50 | "no-class-assign": 2,
51 | "no-cond-assign": 2,
52 | "no-const-assign": 2,
53 | "no-control-regex": 2,
54 | "no-debugger": 2,
55 | "no-delete-var": 2,
56 | "no-dupe-args": 2,
57 | "no-dupe-class-members": 2,
58 | "no-dupe-keys": 2,
59 | "no-duplicate-case": 2,
60 | "no-empty-character-class": 2,
61 | "no-eval": 2,
62 | "no-ex-assign": 2,
63 | "no-extend-native": 2,
64 | "no-extra-bind": 2,
65 | "no-extra-boolean-cast": 2,
66 | "no-extra-parens": [2, "functions"],
67 | "no-fallthrough": 2,
68 | "no-floating-decimal": 2,
69 | "no-func-assign": 2,
70 | "no-implied-eval": 2,
71 | "no-inner-declarations": [2, "functions"],
72 | "no-invalid-regexp": 2,
73 | "no-irregular-whitespace": 2,
74 | "no-iterator": 2,
75 | "no-label-var": 2,
76 | "no-labels": 2,
77 | "no-lone-blocks": 2,
78 | "no-mixed-spaces-and-tabs": 2,
79 | "no-multi-spaces": 2,
80 | "no-multi-str": 2,
81 | "no-multiple-empty-lines": [2, { "max": 1 }],
82 | "no-native-reassign": 0,
83 | "no-negated-in-lhs": 2,
84 | "no-new": 2,
85 | "no-new-func": 2,
86 | "no-new-object": 2,
87 | "no-new-require": 2,
88 | "no-new-wrappers": 2,
89 | "no-obj-calls": 2,
90 | "no-octal": 2,
91 | "no-octal-escape": 2,
92 | "no-proto": 0,
93 | "no-redeclare": 2,
94 | "no-regex-spaces": 2,
95 | "no-return-assign": 2,
96 | "no-self-compare": 2,
97 | "no-sequences": 2,
98 | "no-shadow-restricted-names": 2,
99 | "no-spaced-func": 2,
100 | "no-sparse-arrays": 2,
101 | "no-this-before-super": 2,
102 | "no-throw-literal": 2,
103 | "no-trailing-spaces": 0,
104 | "no-undef": 2,
105 | "no-undef-init": 2,
106 | "no-unexpected-multiline": 2,
107 | "no-unneeded-ternary": [2, { "defaultAssignment": false }],
108 | "no-unreachable": 2,
109 | "no-unused-vars": [2, { "vars": "all", "args": "none" }],
110 | "no-useless-call": 0,
111 | "no-with": 2,
112 | "one-var": [0, { "initialized": "never" }],
113 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
114 | "padded-blocks": [0, "never"],
115 | "quotes": [2, "single", "avoid-escape"],
116 | "radix": 2,
117 | "semi": [2, "always"],
118 | "semi-spacing": [2, { "before": false, "after": true }],
119 | "space-before-blocks": [2, "always"],
120 | "space-before-function-paren": [2, "never"],
121 | "space-in-parens": [2, "never"],
122 | "space-infix-ops": 2,
123 | "space-unary-ops": [2, { "words": true, "nonwords": false }],
124 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
125 | "use-isnan": 2,
126 | "valid-typeof": 2,
127 | "wrap-iife": [2, "any"],
128 | "yoda": [2, "never"]
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | * text=auto
2 |
--------------------------------------------------------------------------------
/.github/contributing.md:
--------------------------------------------------------------------------------
1 | # Contributing to strip-filename-increment
2 |
3 | First and foremost, thank you! We appreciate that you want to contribute to strip-filename-increment, your time is valuable, and your contributions mean a lot to us.
4 |
5 | ## Important!
6 |
7 | By contributing to this project, you:
8 |
9 | * Agree that you have authored 100% of the content
10 | * Agree that you have the necessary rights to the content
11 | * Agree that you have received the necessary permissions from your employer to make the contributions (if applicable)
12 | * Agree that the content you contribute may be provided under the Project license(s)
13 |
14 | ## Getting started
15 |
16 | **What does "contributing" mean?**
17 |
18 | Creating an issue is the simplest form of contributing to a project. But there are many ways to contribute, including the following:
19 |
20 | - Updating or correcting documentation
21 | - Feature requests
22 | - Bug reports
23 |
24 | If you'd like to learn more about contributing in general, the [Guide to Idiomatic Contributing](https://github.com/jonschlinkert/idiomatic-contributing) has a lot of useful information.
25 |
26 | **Showing support for strip-filename-increment**
27 |
28 | Please keep in mind that open source software is built by people like you, who spend their free time creating things the rest the community can use.
29 |
30 | Don't have time to contribute? No worries, here are some other ways to show your support for strip-filename-increment:
31 |
32 | - star the [project](https://github.com/jonschlinkert/strip-filename-increment)
33 | - tweet your support for strip-filename-increment
34 |
35 | ## Issues
36 |
37 | ### Before creating an issue
38 |
39 | Please try to determine if the issue is caused by an underlying library, and if so, create the issue there. Sometimes this is difficult to know. We only ask that you attempt to give a reasonable attempt to find out. Oftentimes the readme will have advice about where to go to create issues.
40 |
41 | Try to follow these guidelines
42 |
43 | - **Avoid creating issues for implementation help**. It's much better for discoverability, SEO, and semantics - to keep the issue tracker focused on bugs and feature requests - to ask implementation-related questions on [stackoverflow.com][so]
44 | - **Investigate the issue**:
45 | - **Check the readme** - oftentimes you will find notes about creating issues, and where to go depending on the type of issue.
46 | - Create the issue in the appropriate repository.
47 |
48 | ### Creating an issue
49 |
50 | Please be as descriptive as possible when creating an issue. Give us the information we need to successfully answer your question or address your issue by answering the following in your issue:
51 |
52 | - **version**: please note the version of strip-filename-increment are you using
53 | - **extensions, plugins, helpers, etc** (if applicable): please list any extensions you're using
54 | - **error messages**: please paste any error messages into the issue, or a [gist](https://gist.github.com/)
55 |
56 | ### Closing issues
57 |
58 | The original poster or the maintainer's of strip-filename-increment may close an issue at any time. Typically, but not exclusively, issues are closed when:
59 |
60 | - The issue is resolved
61 | - The project's maintainers have determined the issue is out of scope
62 | - An issue is clearly a duplicate of another issue, in which case the duplicate issue will be linked.
63 | - A discussion has clearly run its course
64 |
65 |
66 | ## Next steps
67 |
68 | **Tips for creating idiomatic issues**
69 |
70 | Spending just a little extra time to review best practices and brush up on your contributing skills will, at minimum, make your issue easier to read, easier to resolve, and more likely to be found by others who have the same or similar issue in the future. At best, it will open up doors and potential career opportunities by helping you be at your best.
71 |
72 | The following resources were hand-picked to help you be the most effective contributor you can be:
73 |
74 | - The [Guide to Idiomatic Contributing](https://github.com/jonschlinkert/idiomatic-contributing) is a great place for newcomers to start, but there is also information for experienced contributors there.
75 | - Take some time to learn basic markdown. We can't stress this enough. Don't start pasting code into GitHub issues before you've taken a moment to review this [markdown cheatsheet](https://gist.github.com/jonschlinkert/5854601)
76 | - The GitHub guide to [basic markdown](https://help.github.com/articles/markdown-basics/) is another great markdown resource.
77 | - Learn about [GitHub Flavored Markdown](https://help.github.com/articles/github-flavored-markdown/). And if you want to really go above and beyond, read [mastering markdown](https://guides.github.com/features/mastering-markdown/).
78 |
79 | At the very least, please try to:
80 |
81 | - Use backticks to wrap code. This ensures that it retains its formatting and isn't modified when it's rendered by GitHub, and makes the code more readable to others
82 | - When applicable, use syntax highlighting by adding the correct language name after the first "code fence"
83 |
84 |
85 | [so]: http://stackoverflow.com/questions/tagged/strip-filename-increment
86 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # always ignore files
2 | *.DS_Store
3 | *.sublime-*
4 |
5 | # test related, or directories generated by tests
6 | test/actual
7 | actual
8 | coverage
9 | .nyc*
10 |
11 | # npm
12 | node_modules
13 | npm-debug.log
14 |
15 | # yarn
16 | yarn.lock
17 | yarn-error.log
18 |
19 | # misc
20 | _gh_pages
21 | _draft
22 | _drafts
23 | bower_components
24 | vendor
25 | temp
26 | tmp
27 | TODO.md
28 | package-lock.json
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 | os:
3 | - linux
4 | - osx
5 | - windows
6 | language: node_js
7 | node_js:
8 | - node
9 | - '12'
10 | - '10'
11 | - '8'
12 |
--------------------------------------------------------------------------------
/.verb.md:
--------------------------------------------------------------------------------
1 | ## Usage
2 |
3 | ```js
4 | const strip = require('{%= name %}');
5 | ```
6 |
7 | ## API
8 |
9 | {%= apidocs("index.js") %}
10 |
11 | ## Options
12 |
13 | ### removeRawNumbers
14 |
15 | Remove "raw" trailing numbers that might not actually be increments. Use this with caution.
16 |
17 | **Type**: `boolean`
18 |
19 | **Default**: `undefined`
20 |
21 | **Example**:
22 |
23 | ```js
24 | console.log(strip('foo 1')); //=> 'foo 1'
25 | console.log(strip('foo 1', { removeRawNumbers: true })); //=> 'foo'
26 |
27 | console.log(strip('foo (1) 1')); //=> 'foo (1) 1'
28 | console.log(strip('foo (1) 1', { removeRawNumbers: true })); //=> 'foo'
29 |
30 | // This following example is not touched either way,
31 | // since it's definitely not an increment.
32 | console.log(strip('foo [1]')); //=> 'foo [1]'
33 | console.log(strip('foo [1]', { removeRawNumbers: true })); //=> 'foo [1]'
34 | ```
35 |
36 | ## Examples
37 |
38 | ### Windows path increments
39 |
40 | All of the following would return `foo`
41 |
42 | ```js
43 | console.log(strip('foo (1)'));
44 | console.log(strip('foo (2)'));
45 | console.log(strip('foo (22)'));
46 | ```
47 |
48 | All of the following would return `foo.txt`
49 |
50 | ```js
51 | console.log(strip('foo (1).txt'));
52 | console.log(strip('foo (2).txt'));
53 | console.log(strip('foo (22).txt'));
54 | ```
55 |
56 | ### MacOS path increments
57 |
58 | All of the following would return `foo`
59 |
60 | ```js
61 | console.log(strip('foo copy'));
62 | console.log(strip('foo copy 1'));
63 | console.log(strip('foo copy 2'));
64 | console.log(strip('foo copy 21'));
65 | console.log(strip('foo copy 219 copy 219'));
66 | ```
67 |
68 | All of the following would return `foo.txt`
69 |
70 | ```js
71 | console.log(strip('foo copy.txt'));
72 | console.log(strip('foo copy 1.txt'));
73 | console.log(strip('foo copy 2.txt'));
74 | console.log(strip('foo copy 21.txt'));
75 | console.log(strip('foo copy 219 copy 219.txt'));
76 | ```
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015-present, Jon Schlinkert.
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # strip-filename-increment [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=W8YFZ425KND68) [](https://www.npmjs.com/package/strip-filename-increment) [](https://npmjs.org/package/strip-filename-increment) [](https://npmjs.org/package/strip-filename-increment) [](https://travis-ci.org/jonschlinkert/strip-filename-increment)
2 |
3 | > Operating systems commonly add a trailing increment, or the word 'copy', or something similar to duplicate files. This strips those increments. Tested on Windows, MacOS, and Linux.
4 |
5 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
6 |
7 | ## Install
8 |
9 | Install with [npm](https://www.npmjs.com/) (requires [Node.js](https://nodejs.org/en/) >=8):
10 |
11 | ```sh
12 | $ npm install --save strip-filename-increment
13 | ```
14 |
15 | ## Usage
16 |
17 | ```js
18 | const strip = require('strip-filename-increment');
19 | ```
20 |
21 | ## API
22 |
23 | ### [strip](index.js#L34)
24 |
25 | Remove trailing increments from the `dirname` and/or `stem` (basename
26 | without extension) of the given file path or object.
27 |
28 | **Params**
29 |
30 | * `file` **{Sring|Object}**: If the file is an object, it must have a `path` property.
31 | * `options` **{Object}**: See [available options](#options).
32 | * `returns` **{String|Object}**: Returns the same type that was given.
33 |
34 | ### [.increment](index.js#L62)
35 |
36 | Removes trailing increments from the given string.
37 |
38 | **Params**
39 |
40 | * `input` **{String}**
41 | * `options` **{Object}**: See [available options](#options).
42 | * `returns` **{String}**
43 |
44 | **Example**
45 |
46 | ```js
47 | console.log(strip.increment('foo (2)')); => 'foo'
48 | console.log(strip.increment('foo (copy)')); => 'foo'
49 | console.log(strip.increment('foo copy 2')); => 'foo'
50 | ```
51 |
52 | ### [.dirname](index.js#L86)
53 |
54 | Removes trailing increments and returns the `dirname` of the given `filepath`.
55 |
56 | **Params**
57 |
58 | * `filepath` **{String}**
59 | * `options` **{Object}**: See [available options](#options).
60 | * `returns` **{String}**: Returns the `dirname` of the filepath, without increments.
61 |
62 | **Example**
63 |
64 | ```js
65 | console.log(strip.dirname('foo (2)/bar.txt')); => 'foo'
66 | console.log(strip.dirname('foo (copy)/bar.txt')); => 'foo'
67 | console.log(strip.dirname('foo copy 2/bar.txt')); => 'foo'
68 | ```
69 |
70 | ### [.stem](index.js#L107)
71 |
72 | Removes trailing increments and returns the `stem` of the given `filepath`.
73 |
74 | **Params**
75 |
76 | * `filepath` **{String}**
77 | * `options` **{Object}**: See [available options](#options).
78 | * `returns` **{String}**: Returns the `stem` of the filepath, without increments.
79 |
80 | **Example**
81 |
82 | ```js
83 | console.log(strip.stem('foo/bar (2).txt')); //=> 'bar'
84 | console.log(strip.stem('foo/bar (copy).txt')); //=> 'bar'
85 | console.log(strip.stem('foo/bar copy 2.txt')); //=> 'bar'
86 | console.log(strip.stem('foo/bar (2) copy.txt')); //=> 'bar'
87 | console.log(strip.stem('foo/bar (2) - copy.txt')); //=> 'bar'
88 | ```
89 |
90 | ### [.basename](index.js#L128)
91 |
92 | Removes trailing increments and returns the `basename` of the given `filepath`.
93 |
94 | **Params**
95 |
96 | * `filepath` **{String}**
97 | * `options` **{Object}**: See [available options](#options).
98 | * `returns` **{String}**: Returns the `basename` of the filepath, without increments.
99 |
100 | **Example**
101 |
102 | ```js
103 | console.log(strip.basename('foo/bar (2).txt')); //=> 'bar.txt'
104 | console.log(strip.basename('foo/bar (copy).txt')); //=> 'bar.txt'
105 | console.log(strip.basename('foo/bar copy 2.txt')); //=> 'bar.txt'
106 | console.log(strip.basename('foo/bar (2) copy.txt')); //=> 'bar.txt'
107 | console.log(strip.basename('foo/bar (2) - copy.txt')); //=> 'bar.txt'
108 | ```
109 |
110 | ### [.path](index.js#L151)
111 |
112 | Removes trailing increments from the `dirname` and `stem` of the given `filepath`.
113 |
114 | **Params**
115 |
116 | * `filepath` **{String}**
117 | * `options` **{Object}**: See [available options](#options).
118 | * `returns` **{String}**: Returns the `basename` of the filepath, without increments.
119 |
120 | **Example**
121 |
122 | ```js
123 | console.log(strip.path('foo copy/bar (2).txt')); //=> 'foo/bar.txt'
124 | console.log(strip.path('foo (2)/bar (copy).txt')); //=> 'foo/bar.txt'
125 | console.log(strip.path('foo (2)/bar copy 2.txt')); //=> 'foo/bar.txt'
126 | console.log(strip.path('foo copy/bar (2) copy.txt')); //=> 'foo/bar.txt'
127 | console.log(strip.path('foo copy/bar (2) - copy.txt')); //=> 'foo/bar.txt'
128 | ```
129 |
130 | ### [.file](index.js#L181)
131 |
132 | Removes trailing increments from the `dirname` and `stem` properties of the given `file`.
133 |
134 | **Params**
135 |
136 | * `filepath` **{String}**
137 | * `options` **{Object}**: See [available options](#options).
138 | * `returns` **{String}**: Returns the `basename` of the filepath, without increments.
139 |
140 | **Example**
141 |
142 | ```js
143 | console.log(strip({ path: 'foo copy/bar (2).txt' }));
144 | //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
145 | console.log(strip({ path: 'foo (2)/bar (copy).txt' }));
146 | //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
147 | console.log(strip({ path: 'foo (2)/bar copy 2.txt' }));
148 | //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
149 | console.log(strip({ path: 'foo copy/bar (2) copy.txt' }));
150 | //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
151 | console.log(strip({ path: 'foo copy/bar (2) - copy.txt' }));
152 | //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
153 | ```
154 |
155 | ## Options
156 |
157 | ### removeRawNumbers
158 |
159 | Remove "raw" trailing numbers that might not actually be increments. Use this with caution.
160 |
161 | **Type**: `boolean`
162 |
163 | **Default**: `undefined`
164 |
165 | **Example**:
166 |
167 | ```js
168 | console.log(strip('foo 1')); //=> 'foo 1'
169 | console.log(strip('foo 1', { removeRawNumbers: true })); //=> 'foo'
170 |
171 | console.log(strip('foo (1) 1')); //=> 'foo (1) 1'
172 | console.log(strip('foo (1) 1', { removeRawNumbers: true })); //=> 'foo'
173 |
174 | // This following example is not touched either way,
175 | // since it's definitely not an increment.
176 | console.log(strip('foo [1]')); //=> 'foo [1]'
177 | console.log(strip('foo [1]', { removeRawNumbers: true })); //=> 'foo [1]'
178 | ```
179 |
180 | ## Examples
181 |
182 | ### Windows path increments
183 |
184 | All of the following would return `foo`
185 |
186 | ```js
187 | console.log(strip('foo (1)'));
188 | console.log(strip('foo (2)'));
189 | console.log(strip('foo (22)'));
190 | ```
191 |
192 | All of the following would return `foo.txt`
193 |
194 | ```js
195 | console.log(strip('foo (1).txt'));
196 | console.log(strip('foo (2).txt'));
197 | console.log(strip('foo (22).txt'));
198 | ```
199 |
200 | ### MacOS path increments
201 |
202 | All of the following would return `foo`
203 |
204 | ```js
205 | console.log(strip('foo copy'));
206 | console.log(strip('foo copy 1'));
207 | console.log(strip('foo copy 2'));
208 | console.log(strip('foo copy 21'));
209 | console.log(strip('foo copy 219 copy 219'));
210 | ```
211 |
212 | All of the following would return `foo.txt`
213 |
214 | ```js
215 | console.log(strip('foo copy.txt'));
216 | console.log(strip('foo copy 1.txt'));
217 | console.log(strip('foo copy 2.txt'));
218 | console.log(strip('foo copy 21.txt'));
219 | console.log(strip('foo copy 219 copy 219.txt'));
220 | ```
221 |
222 | ## About
223 |
224 |
225 | Contributing
226 |
227 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
228 |
229 | Please read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards.
230 |
231 |
232 |
233 |
234 | Running Tests
235 |
236 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
237 |
238 | ```sh
239 | $ npm install && npm test
240 | ```
241 |
242 |
243 |
244 |
245 | Building docs
246 |
247 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
248 |
249 | To generate the readme, run the following command:
250 |
251 | ```sh
252 | $ npm install -g verbose/verb#dev verb-generate-readme && verb
253 | ```
254 |
255 |
256 |
257 | ### Author
258 |
259 | **Jon Schlinkert**
260 |
261 | * [GitHub Profile](https://github.com/jonschlinkert)
262 | * [Twitter Profile](https://twitter.com/jonschlinkert)
263 | * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
264 |
265 | ### License
266 |
267 | Copyright © 2019, [Jon Schlinkert](https://github.com/jonschlinkert).
268 | Released under the [MIT License](LICENSE).
269 |
270 | ***
271 |
272 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on September 04, 2019._
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * file-name
3 | *
4 | * Copyright (c) 2015-present, Jon Schlinkert.
5 | * Licensed under the MIT License.
6 | */
7 |
8 | 'use strict';
9 |
10 | const path = require('path');
11 | const isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val);
12 |
13 | const constants = {
14 | REGEX_DARWIN: /( copy( [0-9]+)?)+$/i,
15 | REGEX_DEFAULT: /(( copy)?( \([0-9]+\)|[0-9]+)?)+$/i,
16 | REGEX_WIN32: /( \([0-9]+\))+$/i,
17 | REGEX_NON_STANDARD: /( \.\(incomplete\)| \([0-9]+\)|[- ]+)+$/i,
18 | REGEX_LINUX: /( \(((another|[0-9]+(th|st|nd|rd)) )?copy\))+$/i,
19 | REGEX_RAW_NUMBERS: '| [0-9]+',
20 | REGEX_SOURCE: ' \\((?:(another|[0-9]+(th|st|nd|rd)) )?copy\\)|copy( [0-9]+)?|\\.\\(incomplete\\)| \\([0-9]+\\)|[- ]+'
21 | };
22 |
23 | /**
24 | * Remove trailing increments from the `dirname` and/or `stem` (basename
25 | * without extension) of the given file path or object.
26 | *
27 | * @name strip
28 | * @param {Sring|Object} `file` If the file is an object, it must have a `path` property.
29 | * @param {Object} `options` See [available options](#options).
30 | * @return {String|Object} Returns the same type that was given.
31 | * @api public
32 | */
33 |
34 | const strip = (file, options) => {
35 | if (!file) return file;
36 | if (isObject(file) && file.path) {
37 | return strip.file(file, options);
38 | }
39 |
40 | let filepath = strip.increment(file, options);
41 | let extname = path.extname(filepath);
42 | let dirname = strip.increment(path.dirname(filepath), options);
43 | let stem = strip.increment(path.basename(filepath, extname), options);
44 | return path.join(dirname, stem + extname);
45 | };
46 |
47 | /**
48 | * Removes trailing increments from the given string.
49 | *
50 | * ```js
51 | * console.log(strip.increment('foo (2)')); => 'foo'
52 | * console.log(strip.increment('foo (copy)')); => 'foo'
53 | * console.log(strip.increment('foo copy 2')); => 'foo'
54 | * ```
55 | * @name .increment
56 | * @param {String} `input`
57 | * @param {Object} `options` See [available options](#options).
58 | * @return {String}
59 | * @api public
60 | */
61 |
62 | strip.increment = (input, options = {}) => {
63 | if (typeof input === 'string' && input !== '') {
64 | let suffix = options.removeRawNumbers === true ? constants.REGEX_RAW_NUMBERS : '';
65 | let source = constants.REGEX_SOURCE + suffix;
66 | return input.replace(new RegExp(`(${source})+$`, 'i'), '');
67 | }
68 | return input;
69 | };
70 |
71 | /**
72 | * Removes trailing increments and returns the `dirname` of the given `filepath`.
73 | *
74 | * ```js
75 | * console.log(strip.dirname('foo (2)/bar.txt')); => 'foo'
76 | * console.log(strip.dirname('foo (copy)/bar.txt')); => 'foo'
77 | * console.log(strip.dirname('foo copy 2/bar.txt')); => 'foo'
78 | * ```
79 | * @name .dirname
80 | * @param {String} `filepath`
81 | * @param {Object} `options` See [available options](#options).
82 | * @return {String} Returns the `dirname` of the filepath, without increments.
83 | * @api public
84 | */
85 |
86 | strip.dirname = (filepath, options) => {
87 | return strip.increment(path.dirname(filepath), options);
88 | };
89 |
90 | /**
91 | * Removes trailing increments and returns the `stem` of the given `filepath`.
92 | *
93 | * ```js
94 | * console.log(strip.stem('foo/bar (2).txt')); //=> 'bar'
95 | * console.log(strip.stem('foo/bar (copy).txt')); //=> 'bar'
96 | * console.log(strip.stem('foo/bar copy 2.txt')); //=> 'bar'
97 | * console.log(strip.stem('foo/bar (2) copy.txt')); //=> 'bar'
98 | * console.log(strip.stem('foo/bar (2) - copy.txt')); //=> 'bar'
99 | * ```
100 | * @name .stem
101 | * @param {String} `filepath`
102 | * @param {Object} `options` See [available options](#options).
103 | * @return {String} Returns the `stem` of the filepath, without increments.
104 | * @api public
105 | */
106 |
107 | strip.stem = (filepath, options) => {
108 | return strip.increment(path.basename(filepath, path.extname(filepath)), options);
109 | };
110 |
111 | /**
112 | * Removes trailing increments and returns the `basename` of the given `filepath`.
113 | *
114 | * ```js
115 | * console.log(strip.basename('foo/bar (2).txt')); //=> 'bar.txt'
116 | * console.log(strip.basename('foo/bar (copy).txt')); //=> 'bar.txt'
117 | * console.log(strip.basename('foo/bar copy 2.txt')); //=> 'bar.txt'
118 | * console.log(strip.basename('foo/bar (2) copy.txt')); //=> 'bar.txt'
119 | * console.log(strip.basename('foo/bar (2) - copy.txt')); //=> 'bar.txt'
120 | * ```
121 | * @name .basename
122 | * @param {String} `filepath`
123 | * @param {Object} `options` See [available options](#options).
124 | * @return {String} Returns the `basename` of the filepath, without increments.
125 | * @api public
126 | */
127 |
128 | strip.basename = (filepath, options) => {
129 | let extname = path.extname(filepath);
130 | let stem = path.basename(filepath, extname);
131 | return strip.increment(stem, options) + extname;
132 | };
133 |
134 | /**
135 | * Removes trailing increments from the `dirname` and `stem` of the given `filepath`.
136 | *
137 | * ```js
138 | * console.log(strip.path('foo copy/bar (2).txt')); //=> 'foo/bar.txt'
139 | * console.log(strip.path('foo (2)/bar (copy).txt')); //=> 'foo/bar.txt'
140 | * console.log(strip.path('foo (2)/bar copy 2.txt')); //=> 'foo/bar.txt'
141 | * console.log(strip.path('foo copy/bar (2) copy.txt')); //=> 'foo/bar.txt'
142 | * console.log(strip.path('foo copy/bar (2) - copy.txt')); //=> 'foo/bar.txt'
143 | * ```
144 | * @name .path
145 | * @param {String} `filepath`
146 | * @param {Object} `options` See [available options](#options).
147 | * @return {String} Returns the `basename` of the filepath, without increments.
148 | * @api public
149 | */
150 |
151 | strip.path = (filepath, options) => {
152 | let extname = path.extname(filepath);
153 | let stem = strip.increment(path.basename(filepath, extname), options);
154 | let dirname = strip.increment(path.dirname(filepath), options);
155 | return path.join(dirname, stem + extname);
156 | };
157 |
158 | /**
159 | * Removes trailing increments from the `dirname` and `stem` properties
160 | * of the given `file`.
161 | *
162 | * ```js
163 | * console.log(strip({ path: 'foo copy/bar (2).txt' }));
164 | * //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
165 | * console.log(strip({ path: 'foo (2)/bar (copy).txt' }));
166 | * //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
167 | * console.log(strip({ path: 'foo (2)/bar copy 2.txt' }));
168 | * //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
169 | * console.log(strip({ path: 'foo copy/bar (2) copy.txt' }));
170 | * //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
171 | * console.log(strip({ path: 'foo copy/bar (2) - copy.txt' }));
172 | * //=> { path: 'foo/bar.txt', dir: 'foo', base: 'bar.txt', name: 'bar', ext: '.txt' }
173 | * ```
174 | * @name .file
175 | * @param {String} `filepath`
176 | * @param {Object} `options` See [available options](#options).
177 | * @return {String} Returns the `basename` of the filepath, without increments.
178 | * @api public
179 | */
180 |
181 | strip.file = (file, options = {}) => {
182 | if (!isObject(file)) return file;
183 | if (!file.path) return file;
184 |
185 | if (file.dirname && !file.dir) file.dir = file.dirname;
186 | if (file.basename && !file.base) file.base = file.basename;
187 | if (file.extname && !file.ext) file.ext = file.extname;
188 | if (file.stem && !file.name) file.name = file.stem;
189 |
190 | if (file.dir === void 0) file.dir = path.dirname(file.path);
191 | if (file.ext === void 0) file.ext = path.extname(file.path);
192 | if (file.base === void 0) file.base = path.basename(file.path);
193 | if (file.name === void 0) file.name = path.basename(file.path, file.ext);
194 |
195 | file.name = strip.increment(file.name, options);
196 | file.dir = strip.increment(file.dir, options);
197 | file.base = file.name + file.ext;
198 |
199 | file.path = path.join(file.dir, file.base);
200 | return file;
201 | };
202 |
203 | module.exports = strip;
204 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "strip-filename-increment",
3 | "description": "Operating systems commonly add a trailing increment, or the word 'copy', or something similar to duplicate files. This strips those increments. Tested on Windows, MacOS, and Linux.",
4 | "version": "2.0.1",
5 | "homepage": "https://github.com/jonschlinkert/strip-filename-increment",
6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)",
7 | "repository": "jonschlinkert/strip-filename-increment",
8 | "bugs": {
9 | "url": "https://github.com/jonschlinkert/strip-filename-increment/issues"
10 | },
11 | "license": "MIT",
12 | "files": [
13 | "index.js"
14 | ],
15 | "main": "index.js",
16 | "engines": {
17 | "node": ">=8"
18 | },
19 | "scripts": {
20 | "test": "mocha"
21 | },
22 | "devDependencies": {
23 | "gulp-format-md": "^2.0.0",
24 | "mocha": "^6.2.0"
25 | },
26 | "keywords": [
27 | "filename",
28 | "increment",
29 | "strip"
30 | ],
31 | "verb": {
32 | "toc": false,
33 | "layout": "default",
34 | "tasks": [
35 | "readme"
36 | ],
37 | "plugins": [
38 | "gulp-format-md"
39 | ],
40 | "lint": {
41 | "reflinks": true
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/test/darwin.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | require('mocha');
4 | const assert = require('assert').strict;
5 | const strip = require('..');
6 | const posix = str => str.replace(/\\/g, '/');
7 |
8 | describe('darwin', () => {
9 | describe('file name (stem)', () => {
10 | it('should remove mac-OS-style increments from a file name', () => {
11 | assert.equal(strip('foo copy.txt'), 'foo.txt');
12 | assert.equal(strip('foo copy 1.txt'), 'foo.txt');
13 | assert.equal(strip('foo copy 2.txt'), 'foo.txt');
14 | assert.equal(strip('foo copy 21.txt'), 'foo.txt');
15 | assert.equal(strip('foo copy 219 copy 219.txt'), 'foo.txt');
16 | assert.equal(strip('foo copy 219 (2).txt'), 'foo.txt');
17 | });
18 |
19 | it('should remove mac-OS-style increments from a folder name', () => {
20 | assert.equal(strip('foo copy'), 'foo');
21 | assert.equal(strip('foo copy 1'), 'foo');
22 | assert.equal(strip('foo copy 2'), 'foo');
23 | assert.equal(strip('foo copy 21'), 'foo');
24 | assert.equal(strip('foo copy 219 copy 219'), 'foo');
25 | assert.equal(strip('foo copy 219 (2)'), 'foo');
26 | assert.equal(strip('foo Copy'), 'foo');
27 | assert.equal(strip('foo Copy 1'), 'foo');
28 | assert.equal(strip('foo Copy 2'), 'foo');
29 | assert.equal(strip('foo Copy 21'), 'foo');
30 | assert.equal(strip('foo Copy 219 copy 219'), 'foo');
31 | assert.equal(strip('foo Copy 219 (2)'), 'foo');
32 | });
33 |
34 | it('should remove mac-OS-style increments from folder and file name', () => {
35 | assert.equal(posix(strip('bar copy/foo copy 1.txt')), 'bar/foo.txt');
36 | assert.equal(posix(strip('bar copy/foo copy 2.txt')), 'bar/foo.txt');
37 | assert.equal(posix(strip('bar copy/foo copy 21.txt')), 'bar/foo.txt');
38 | assert.equal(posix(strip('bar copy/foo copy 219 (2).txt')), 'bar/foo.txt');
39 | assert.equal(posix(strip('bar copy/foo copy 219 copy 219.txt')), 'bar/foo.txt');
40 | assert.equal(posix(strip('bar copy/foo copy.txt')), 'bar/foo.txt');
41 | });
42 | });
43 |
44 | describe('basename (stem + file extension)', () => {
45 | it('should remove mac-OS-style increments from a basename', () => {
46 | assert.equal(strip('foo copy.txt'), 'foo.txt');
47 | assert.equal(strip('foo copy 1.txt'), 'foo.txt');
48 | assert.equal(strip('foo copy 2.txt'), 'foo.txt');
49 | assert.equal(strip('foo copy 21.txt'), 'foo.txt');
50 | assert.equal(strip('foo copy 219 copy 219.txt'), 'foo.txt');
51 | assert.equal(strip('foo copy 219 (2).txt'), 'foo.txt');
52 | });
53 |
54 | it('should remove mac-OS-style increments from a basename', () => {
55 | assert.equal(strip('foo.(incomplete).txt'), 'foo.txt');
56 | assert.equal(strip('foo copy 219.(incomplete).txt'), 'foo.txt');
57 | assert.equal(strip('foo copy 219.(incomplete).(incomplete).(incomplete).txt'), 'foo.txt');
58 | assert.equal(strip('foo.(incomplete).(incomplete).txt'), 'foo.txt');
59 | });
60 | });
61 | });
62 |
--------------------------------------------------------------------------------
/test/linux.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | require('mocha');
4 | const assert = require('assert').strict;
5 | const strip = require('..');
6 |
7 | describe('linux', () => {
8 | describe('basename (stem + extension)', () => {
9 | it('should remove linux-style increments from a file name', () => {
10 | assert.equal(strip('foo (copy).txt', { platform: 'linux' }), 'foo.txt');
11 | assert.equal(strip('foo (another copy).txt', { platform: 'linux' }), 'foo.txt');
12 | assert.equal(strip('foo (3rd copy).txt', { platform: 'linux' }), 'foo.txt');
13 | assert.equal(strip('foo (4th copy).txt', { platform: 'linux' }), 'foo.txt');
14 | assert.equal(strip('foo (5th copy).txt', { platform: 'linux' }), 'foo.txt');
15 | assert.equal(strip('foo (111th copy).txt', { platform: 'linux' }), 'foo.txt');
16 | });
17 | });
18 |
19 | describe('folder name', () => {
20 | it('should remove linux-style increments from a folder name', () => {
21 | assert.equal(strip('foo (copy)', { platform: 'linux' }), 'foo');
22 | assert.equal(strip('foo (another copy)', { platform: 'linux' }), 'foo');
23 | assert.equal(strip('foo (3rd copy)', { platform: 'linux' }), 'foo');
24 | assert.equal(strip('foo (4th copy)', { platform: 'linux' }), 'foo');
25 | assert.equal(strip('foo (5th copy)', { platform: 'linux' }), 'foo');
26 | assert.equal(strip('foo (111th copy)', { platform: 'linux' }), 'foo');
27 | });
28 |
29 | it('should not remove non-incremental numbers from a folder name', () => {
30 | assert.equal(strip('foo 1 (copy)', { platform: 'linux' }), 'foo 1');
31 | assert.equal(strip('foo 1 (another copy)', { platform: 'linux' }), 'foo 1');
32 | assert.equal(strip('foo 1 (3rd copy)', { platform: 'linux' }), 'foo 1');
33 | assert.equal(strip('foo 1 (4th copy)', { platform: 'linux' }), 'foo 1');
34 | assert.equal(strip('foo 1 (5th copy)', { platform: 'linux' }), 'foo 1');
35 | assert.equal(strip('foo 1 (111th copy)', { platform: 'linux' }), 'foo 1');
36 | });
37 | });
38 | });
39 |
--------------------------------------------------------------------------------
/test/non-standard.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | require('mocha');
4 | const assert = require('assert').strict;
5 | const strip = require('..');
6 | const posix = str => str.replace(/\\/g, '/');
7 |
8 | describe('non-standard', () => {
9 | describe('foldres or files without extensions', () => {
10 | it('should not remove raw numbers from file names by default', () => {
11 | assert.equal(strip('foo 1'), 'foo 1');
12 | assert.equal(strip('foo 2'), 'foo 2');
13 | });
14 |
15 | it('should remove raw numbers from file names when specified on options', () => {
16 | assert.equal(strip('foo 1', { removeRawNumbers: true }), 'foo');
17 | assert.equal(strip('foo 2', { removeRawNumbers: true }), 'foo');
18 | });
19 |
20 | it('should remove (incomplete) from a file name', () => {
21 | assert.equal(strip('foo.(incomplete)'), 'foo');
22 | assert.equal(strip('foo copy 219.(incomplete)'), 'foo');
23 | assert.equal(strip('foo copy 219.(incomplete).(incomplete).(incomplete)'), 'foo');
24 | assert.equal(strip('foo.(incomplete).(incomplete)'), 'foo');
25 | });
26 |
27 | it('should remove (incomplete) from a file name with extension', () => {
28 | assert.equal(strip('foo.(incomplete).txt'), 'foo.txt');
29 | assert.equal(strip('foo copy 219.(incomplete).txt'), 'foo.txt');
30 | assert.equal(strip('foo copy 219.(incomplete).(incomplete).(incomplete).txt'), 'foo.txt');
31 | assert.equal(strip('foo.(incomplete).(incomplete).txt'), 'foo.txt');
32 | });
33 |
34 | it('should not remove a non-increment from a file or folder name', () => {
35 | assert.equal(strip('foo [1]'), 'foo [1]');
36 | assert.equal(strip('foo [1].txt'), 'foo [1].txt');
37 | assert.equal(posix(strip('bar [1]/foo [1].txt')), 'bar [1]/foo [1].txt');
38 | assert.equal(posix(strip('bar[1]/foo[1].txt')), 'bar[1]/foo[1].txt');
39 | });
40 | });
41 |
42 | describe('basename (stem + file extension)', () => {
43 | it('should not remove a non-increment from a basename', () => {
44 | assert.equal(strip('foo [1].txt'), 'foo [1].txt');
45 | });
46 | });
47 | });
48 |
--------------------------------------------------------------------------------
/test/windows.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | require('mocha');
4 | const path = require('path');
5 | const assert = require('assert').strict;
6 | const strip = require('..');
7 |
8 | describe('windows', () => {
9 | describe('folders or files without extensions', () => {
10 | it('should remove windows-style increments from a file name', () => {
11 | assert.equal(strip('foo (1)'), 'foo');
12 | assert.equal(strip('foo (2)'), 'foo');
13 | assert.equal(strip('foo (22)'), 'foo');
14 | });
15 |
16 | it('should not remove non-increments', () => {
17 | assert.equal(strip('foo 1'), 'foo 1');
18 | assert.equal(strip('foo (1) 1'), 'foo (1) 1');
19 | assert.equal(strip('foo [1]'), 'foo [1]');
20 | });
21 |
22 | it('should not remove non-increments', () => {
23 | assert.equal(strip('foo 1', { removeRawNumbers: true }), 'foo');
24 | assert.equal(strip('foo (1) 1', { removeRawNumbers: true }), 'foo');
25 | assert.equal(strip('foo [1]', { removeRawNumbers: true }), 'foo [1]');
26 | });
27 |
28 | it('should remove windows-style increments from absolute paths', () => {
29 | assert.equal(strip(path.resolve('foo (1)')), path.resolve('foo'));
30 | assert.equal(strip(path.resolve('foo (2)')), path.resolve('foo'));
31 | assert.equal(strip(path.resolve('foo (22)')), path.resolve('foo'));
32 | });
33 |
34 | it('should remove dash-separated windows-style increments', () => {
35 | assert.equal(strip('foo (3) - Copy'), 'foo');
36 | assert.equal(strip('foo (31) - Copy - Copy'), 'foo');
37 | });
38 | });
39 |
40 | describe('basename (stem + extension)', () => {
41 | it('should remove windows-style increments from a basename', () => {
42 | assert.equal(strip('foo (1).txt'), 'foo.txt');
43 | assert.equal(strip('foo (2).txt'), 'foo.txt');
44 | assert.equal(strip('foo (22).txt'), 'foo.txt');
45 | assert.equal(strip('foo copy (22).txt'), 'foo.txt');
46 | assert.equal(strip('foo Copy (22).txt'), 'foo.txt');
47 | });
48 | });
49 | });
50 |
--------------------------------------------------------------------------------