Skip to content

Commit f74b011

Browse files
committed
refactor: renames match to find
1 parent 00be5a3 commit f74b011

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Rename
22

3-
🖉 Script for recursively renaming files and folders
3+
🖉 Script for recursively renaming files and folders
44

55
## Usage
66

@@ -13,26 +13,27 @@
1313
### Rename all '.js' files to '.ts' files in the current directory
1414

1515
```sh
16-
rename --match '(.+).js' --replace '$1.ts'
16+
rename --find '(.+).js' --replace '$1.ts'
1717
```
1818

1919
### Rename all '.js' files to '.ts' files recursively, starting with the current directory
2020

2121
```sh
22-
rename --match '(.+).js' --replace '$1.ts' --recursive
22+
rename --find '(.+).js' --replace '$1.ts' --recursive
2323
```
2424

25-
*Be careful when using this command however it can easily be undone by it's inverse*
25+
_Be careful when using this command however it can easily be undone by it's inverse_
2626

2727
```sh
28-
rename --match '(.+).ts' --replace '$1.js' --recursive
28+
rename --find '(.+).ts' --replace '$1.js' --recursive
2929
```
30+
3031
## Transformers
3132

3233
### Available transformers
3334

3435
```ts
35-
"toUpperCase" | "toLowerCase" | "trim" | "trimLeft" | "trimRight"
36+
'toUpperCase' | 'toLowerCase' | 'trim' | 'trimLeft' | 'trimRight';
3637
```
3738

3839
### Examples
@@ -44,7 +45,7 @@ rename --transformations trim
4445
```
4546

4647
```sh
47-
rename --match '(.+)(\.ts)' --replace '$1$2' --transformations trim,slice
48+
rename --find '(.+)(\.ts)' --replace '$1$2' --transformations trim,slice
4849
```
4950

5051
#### Lower casing file names

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"test:watch": "jest --watch",
99
"build": "tsc",
1010
"lint": "tslint",
11-
"rename": "cd playground && ../src/rename -m '(.+).js' -r '$1.ts' && cd .."
11+
"rename": "cd playground && ../src/rename -f '(.+).js' -r '$1.ts' && cd .."
1212
},
1313
"bin": {
1414
"rename": "./dist/rename.js"

src/rename.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import * as fs from 'fs-extra';
44
import { transform } from './utils';
55

66
commander
7-
.option('-m, --match <pattern>', 'Pattern to match against', '.+')
8-
.option('-f, --flags <flags>', 'Pattern to match against', '')
7+
.option('-f, --find <pattern>', 'Pattern to find against', '.+')
8+
.option('-f, --flags <flags>', 'Pattern to find against', '')
99
.option('-r, --replace <pattern>', 'Replacement pattern', '$&')
1010
.option(
1111
'-t, --transformations <array>',
@@ -15,7 +15,7 @@ commander
1515
.option('-R, --recursive', 'Perform recursive rename')
1616
.parse(process.argv);
1717

18-
const { match, flags, replace, transformations, recursive } = commander;
18+
const { find, flags, replace, transformations, recursive } = commander;
1919

2020
const transformationsArray = JSON.parse(transformations);
2121

@@ -25,7 +25,7 @@ const rename = (currentDirectory: string) => (node: string) =>
2525
fs.rename(
2626
`${currentDirectory}/${node}`,
2727
`${currentDirectory}/${node.replace(
28-
new RegExp(match, flags),
28+
new RegExp(find, flags),
2929
transform(replace)(transformationsArray),
3030
)}`,
3131
);

src/rename.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
import * as commander from 'commander';
44
import * as fs from 'fs-extra'; // fs throws when node types are unavailable in the current directory
5-
import { parseTransformations } from './utils';
6-
import { transform } from './utils';
5+
import { parseTransformations, transform } from './utils';
76

87
commander
9-
.option('-m, --match <pattern>', 'Pattern to match against', '.+')
10-
.option('-f, --flags <flags>', 'Pattern to match against', '')
8+
.option('-f, --find <pattern>', 'Pattern to find against', '.+')
9+
.option('--flags <flags>', 'Pattern to find against', '')
1110
.option('-r, --replace <pattern>', 'Replacement pattern', '$&')
1211
.option(
1312
'-t, --transformations <array>',
@@ -17,15 +16,15 @@ commander
1716
.option('-R, --recursive', 'Perform recursive rename')
1817
.parse(process.argv);
1918

20-
const { match, flags, replace, transformations, recursive } = commander;
19+
const { find, flags, replace, transformations, recursive } = commander;
2120

2221
const currentDirectory = process.cwd();
2322

2423
const rename = (currentDirectory: string) => (node: string) =>
2524
fs.rename(
2625
`${currentDirectory}/${node}`,
2726
`${currentDirectory}/${node.replace(
28-
new RegExp(match, flags),
27+
new RegExp(find, flags),
2928
transform(replace)(transformations),
3029
)}`,
3130
);

src/utils/transform.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ import { Transformations } from '../models';
33

44
const transform = (replacement: string) => (
55
transformations: Transformations,
6-
) => (match: string, ...config: string[]) => {
6+
) => (find: string, ...config: string[]) => {
77
const [wholeString] = config.slice(-1);
88

99
const matches = config.slice(0, -2);
1010

1111
const transformedMatches = zip(matches, transformations).map(
12-
([match, transformation]) => match[transformation](),
12+
([find, transformation]) => find[transformation](),
1313
);
1414

1515
const replacementResult = replacement
1616
.replace(/\$0/g, wholeString)
17-
.replace(/\$&/g, match)
17+
.replace(/\$&/g, find)
1818
.replace(/\$(\d)/g, (_, groupNumber) => {
19-
const match = transformedMatches[groupNumber - 1];
19+
const find = transformedMatches[groupNumber - 1];
2020

21-
if (match) {
22-
return match;
21+
if (find) {
22+
return find;
2323
} else {
2424
throw new Error(`'${groupNumber}' does not represent a captured group number from 1 to '${
2525
matches.length

0 commit comments

Comments
 (0)