@frsource/frs-replace 4.1.1 → 5.0.0
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/LICENSE +21 -201
- package/README.md +181 -236
- package/bin/cli.mjs +213 -0
- package/dist/async.d.ts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/sync.d.ts +3 -0
- package/dist/types.d.ts +27 -0
- package/dist/utils.d.ts +3 -0
- package/package.json +62 -36
- package/async.js +0 -1
- package/bin/cli.js +0 -148
- package/index.js +0 -4
- package/src/async.js +0 -104
- package/src/sync.js +0 -88
- package/src/utils.js +0 -22
- package/sync.js +0 -1
package/README.md
CHANGED
|
@@ -1,314 +1,259 @@
|
|
|
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
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://www.npmjs.com/package/@frsource/frs-replace">
|
|
3
|
+
<img src="https://img.shields.io/npm/v/@frsource/frs-replace" alt="NPM version badge">
|
|
4
|
+
</a>
|
|
5
|
+
<a href="https://bundlejs.com/?q=%40frsource%2Ffrs-replace">
|
|
6
|
+
<img src="https://deno.bundlejs.com/badge?q=@frsource/frs-replace" alt="GZIP size calculated by bundlejs.com">
|
|
7
|
+
</a>
|
|
8
|
+
<a href="https://github.com/semantic-release/semantic-release">
|
|
9
|
+
<img src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg" alt="semantic-release badge">
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/FRSOURCE/frs-replace/blob/master/LICENSE">
|
|
12
|
+
<img src="https://img.shields.io/github/license/FRSOURCE/frs-replace" alt="license MIT badge">
|
|
13
|
+
</a>
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
<h1 align="center">@frsource/frs-replace - replace content directly in your files/streams with ease (CLI included)! 📝</h1>
|
|
17
|
+
|
|
18
|
+
<p align="center">
|
|
19
|
+
<a href="https://github.com/FRSOURCE/autoresize-textarea/issues">File an Issue</a>
|
|
20
|
+
·
|
|
21
|
+
<a href="#questions">Question or an idea?</a>
|
|
22
|
+
·
|
|
23
|
+
<a href="#chart_with_upwards_trend-benchmarks">Benchmarks</a>
|
|
24
|
+
<br>
|
|
25
|
+
</p>
|
|
26
|
+
|
|
27
|
+
<p align="center"><strong>The fastest</strong> (<a href="#chart_with_upwards_trend-benchmarks">see benchmarks</a>) CLI & Node wrapper around <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace">javascript replace</a> which allows in-file replacing (with possibility to save changed file as a copy), <a href="https://en.wikipedia.org/wiki/Glob_(programming)">globbing</a>, <a href="https://en.wikipedia.org/wiki/Pipeline_(Unix)">piping</a> and many more!</p>
|
|
28
|
+
|
|
29
|
+
<p align="center">
|
|
30
|
+
<a href="#keyboard-cli">Getting Started (CLI)</a>
|
|
31
|
+
·
|
|
32
|
+
<a href="#mag_right-examples-and-recipes---cli">Examples and recipes (CLI)</a>
|
|
33
|
+
<br>
|
|
34
|
+
<a href="#books-node-api">Getting Started (Node)</a>
|
|
35
|
+
·
|
|
36
|
+
<a href="#mag_right-examples-and-recipes---node">Examples and recipes (Node)</a>
|
|
37
|
+
<br>
|
|
38
|
+
</p>
|
|
28
39
|
|
|
29
40
|
## :scroll: Installation
|
|
30
41
|
|
|
31
42
|
```bash
|
|
32
|
-
|
|
43
|
+
npm install @frsource/frs-replace
|
|
33
44
|
```
|
|
34
45
|
|
|
35
|
-
or
|
|
46
|
+
or execute it right away:
|
|
36
47
|
|
|
37
48
|
```bash
|
|
38
|
-
|
|
49
|
+
npx @frsource/frs-replace
|
|
39
50
|
```
|
|
40
51
|
|
|
41
|
-
|
|
42
|
-
[zipped from `@frsource/frs-replace` releases](https://github.com/FRSource/frs-replace/releases)
|
|
43
|
-
|
|
44
|
-
## :books: Node API
|
|
45
|
-
|
|
46
|
-
@frsource/frs-replace package provides 2 methods: for synchronous or for asynchronous (with promise and ES6 `async`/`await` syntax support) usage:
|
|
52
|
+
## :keyboard: CLI
|
|
47
53
|
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
// or
|
|
51
|
-
import * as FRSReplace from '@frsource/frs-replace';
|
|
52
|
-
|
|
53
|
-
FRSReplace.sync({/* options */})
|
|
54
|
-
FRSReplace.async({/* options */})
|
|
55
|
-
|
|
56
|
-
// you might also want to import methods separately (e.g. import only one of them):
|
|
57
|
-
const FRSReplaceSync = require('@frsource/frs-replace/sync');
|
|
58
|
-
const FRSReplaceAsync = require('@frsource/frs-replace/async');
|
|
59
|
-
// or
|
|
60
|
-
import { sync, async } from '@frsource/frs-replace';
|
|
54
|
+
```bash
|
|
55
|
+
frs-replace <regex> <replacement> [flags]
|
|
61
56
|
```
|
|
62
57
|
|
|
63
|
-
|
|
64
|
-
> Note: remember that you need to provide some input for @frsource/frs-replace to work, so **one of parameters: input or content is required**
|
|
65
|
-
|
|
66
|
-
| Option | Type | Default | Description |
|
|
67
|
-
| --- | --- | --- | --- |
|
|
68
|
-
| input | string or string[] | *undefined* | Path to files or [fast-glob](https://github.com/mrmlnc/fast-glob) pattern pointing to files to be read & replaced from. If multiple files specified results will be joined using `outputJoinString` option's value) |
|
|
69
|
-
| inputReadOptions | string or object | utf8 | Options which are passed directly to the [readFileSync method](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) when reading input file |
|
|
70
|
-
| inputGlobOptions | object | *undefined* | Options which are passed directly to the [fast-glob package](https://github.com/mrmlnc/fast-glob#options-1) when resolving glob patterns |
|
|
71
|
-
| content | string | *undefined* | Content to be replaced (takes precedence over file input) |
|
|
72
|
-
| strategy | "join" or "flatten" or "preserve-structure" | "join" | Output file generation strategy. *"join"* - joins all input files and outputs them as a single file using path passed as: *"output"*. *"preserve-structure"* - saves all files to the *"output"* directory keeping relative directory structure.*"flatten"* - same as *"preserve-structure"* but flattens the directory structure |
|
|
73
|
-
| needle | string or [RegExp Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Syntax)| *-* | Used as a first argument of [javascript replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Syntax) |
|
|
74
|
-
| replacement | string | *-* | Passed as a second argument to [javascript replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Syntax) |
|
|
75
|
-
| output | string | *undefined* | Path of an output file |
|
|
76
|
-
| outputWriteOptions | string or object | "utf8" | Passed as options argument of [write's .sync](https://www.npmjs.com/package/write#sync) |
|
|
77
|
-
| outputJoinString | string | \n | String used when joining multiple files, passed directly to [javascript join](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join#Syntax) |
|
|
78
|
-
|
|
79
|
-
## :keyboard: CLI API
|
|
58
|
+
Basic usage:
|
|
80
59
|
|
|
81
60
|
```bash
|
|
82
|
-
frs-replace
|
|
61
|
+
frs-replace something anything -i foo.js -o foo_replaced.js
|
|
62
|
+
|
|
63
|
+
# all of the occurences of "something" in `foo.js` will be replaced with "anything" and saved to `foo_replaced.js`
|
|
83
64
|
```
|
|
84
65
|
|
|
85
|
-
|
|
66
|
+
More examples [below](#mag_right-examples-and-recipes---cli).
|
|
67
|
+
|
|
68
|
+
### Arguments
|
|
86
69
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
70
|
+
| Argument | Type | Description |
|
|
71
|
+
| --------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------- |
|
|
72
|
+
| \<regex\> | string | First parameter to [RegExp constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Syntax) |
|
|
73
|
+
| \<replacement\> | string | String or path to replacement function file (see ‑‑replace‑fn switch for details) |
|
|
91
74
|
|
|
92
|
-
###
|
|
75
|
+
### Flags
|
|
93
76
|
|
|
94
77
|
> Note: Every boolean option can be negated with use of `--no-` prefix, e.g. `--stdout` or `--no-stdout` turn stdout output on or off, respectively.
|
|
95
78
|
|
|
96
79
|
> Note: Object types can be set using [dot notation](https://github.com/yargs/yargs-parser#dot-notation). So, e.g. if you want to pass `utf8` value under `i-read-opts` `encoding` field you should write `--i-read-opts.encoding utf8`.
|
|
97
80
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
## :mag_right:
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
### 1. Replace all `a` occurences with `b` from given `foo.js` file and return result / write result to the console
|
|
120
|
-
|
|
121
|
-
<details><summary>Click to expand</summary>
|
|
122
|
-
|
|
123
|
-
#### 1.1 API
|
|
124
|
-
|
|
125
|
-
```javascript
|
|
126
|
-
const FRSReplace = require('@frsource/frs-replace')
|
|
127
|
-
|
|
128
|
-
/* synchronously */
|
|
129
|
-
const resultSync = FRSReplace.sync({
|
|
130
|
-
input : 'foo.js',
|
|
131
|
-
regex : new RegExp('a', 'g'),
|
|
132
|
-
replacement : 'b',
|
|
133
|
-
output : 'foo_replaced.js'
|
|
134
|
-
})
|
|
135
|
-
// work with result here
|
|
136
|
-
|
|
137
|
-
/* asynchronously */
|
|
138
|
-
FRSReplace.async({
|
|
139
|
-
input : 'foo.js',
|
|
140
|
-
regex : new RegExp('a', 'g'),
|
|
141
|
-
replacement : 'b'
|
|
142
|
-
})
|
|
143
|
-
.then(resultAsync => {
|
|
144
|
-
// work with result here */
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
/* asynchronously ES6 syntax (must be runned inside async function) */
|
|
148
|
-
const resultAsync = await FRSReplace.async({
|
|
149
|
-
input : 'foo.js',
|
|
150
|
-
regex : new RegExp('a', 'g'),
|
|
151
|
-
replacement : 'b'
|
|
152
|
-
})
|
|
153
|
-
// work with result here */
|
|
154
|
-
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
#### 1.2 CLI
|
|
81
|
+
| Option | Type | Default | Description |
|
|
82
|
+
| ---------------------------------------- | ------------------------------------------- | ------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
83
|
+
| ‑i, ‑‑input | string or string[] | _-_ | Path to files or [fast-glob](https://github.com/mrmlnc/fast-glob) pattern pointing to files to be read & replaced from. If multiple files specified results will be joined using `outputJoinString` option's value) |
|
|
84
|
+
| ‑‑i-read-opts | string or object | utf8 | Options which are passed directly to the [readFileSync method](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) when reading input file |
|
|
85
|
+
| ‑‑i-glob-opts | object | _undefined_ | Options which are passed directly to the [fast-glob package](https://github.com/mrmlnc/fast-glob#options-1) when resolving glob patterns |
|
|
86
|
+
| ‑‑stdin | boolean | _true_ | Wait for stdin input (should be set to _false_ when used in non-interactive terminals) |
|
|
87
|
+
| ‑o, ‑‑output | string | _-_ | Output file name/path (replaces the file if it already exists and creates any intermediate directories if they don't already exist) |
|
|
88
|
+
| ‑‑o-write-opts | string or object | utf8 | Passed as options argument of [write's .sync](https://www.npmjs.com/package/write#sync) |
|
|
89
|
+
| ‑‑o-join-str | string | \n | Used when joining multiple files, passed directly to [javascript join](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join#Syntax) |
|
|
90
|
+
| ‑c, ‑‑content | string | _-_ | Content to be replaced (takes precedence over stream & file input) |
|
|
91
|
+
| ‑s, ‑‑strategy | "join" or "flatten" or "preserve-structure" | "join" | Output file generation strategy. _"join"_ - joins all input files and outputs them as a single file using path passed as: _"output"_. _"preserve-structure"_ - saves all files to the _"output"_ directory keeping relative directory structure._"flatten"_ - same as _"preserve-structure"_ but flattens the directory structure |
|
|
92
|
+
| ‑f, ‑‑flags | string, combination of _gim_ flags | g | [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Syntax) flags |
|
|
93
|
+
| ‑‑stdout | boolean | _true_ if piped input present, _false_ otherwise | Force sending output on stdout |
|
|
94
|
+
| ‑r, ‑‑replace‑fn | boolean | false | Treat replacement argument as path to file containing replacement function |
|
|
95
|
+
| ‑h, ‑‑help | boolean | _-_ | Show help |
|
|
96
|
+
| ‑v, ‑‑version | boolean | _-_ | Show version number |
|
|
97
|
+
|
|
98
|
+
## :mag_right: Examples and recipes - CLI
|
|
99
|
+
|
|
100
|
+
##### 1. Replace all `a` occurrences with `b` from the `foo.js` file and return the result (using CLI)
|
|
158
101
|
|
|
159
102
|
```bash
|
|
160
103
|
frs-replace a b -i foo.js --stdout
|
|
161
104
|
```
|
|
162
105
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
### 2. Replace all `a` occurences with `b` from given `foo.js` and save result to the `foo_replaced.js`
|
|
166
|
-
|
|
167
|
-
<details><summary>Click to expand</summary>
|
|
168
|
-
|
|
169
|
-
#### 2.1 API
|
|
170
|
-
|
|
171
|
-
```javascript
|
|
172
|
-
const result = require('@frsource/frs-replace').sync({
|
|
173
|
-
input : 'foo.js',
|
|
174
|
-
regex : new RegExp('a', 'g'),
|
|
175
|
-
replacement : 'b',
|
|
176
|
-
output : 'foo_replaced.js'
|
|
177
|
-
})
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
#### 2.2 CLI
|
|
106
|
+
##### 2. Replace all `a` occurrences with `b` from `foo.js` and save the result to the `foo_replaced.js` (using CLI)
|
|
181
107
|
|
|
182
108
|
```bash
|
|
183
109
|
frs-replace a b -i foo.js -o foo_replaced.js
|
|
184
110
|
```
|
|
185
111
|
|
|
186
|
-
|
|
112
|
+
##### 3. Replace all `a` occurrences with `b` from an array of files and save the result to the `foo_replaced.js` using default `\n` as a result-joining string (using CLI)
|
|
187
113
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
114
|
+
```bash
|
|
115
|
+
frs-replace a b -i foo.js -i foo2.js -o foo_replaced.js
|
|
116
|
+
```
|
|
191
117
|
|
|
192
|
-
|
|
118
|
+
##### 4. Replace all `a` occurrences with `b` from all `.js` files in the `foo` directory and save the result to the `foo_replaced.js` using `\n/////\n` as a result-joining string (using CLI)
|
|
193
119
|
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
input : ['foo.js', 'foo2.js'],
|
|
197
|
-
regex : new RegExp('a', 'g'),
|
|
198
|
-
replacement : 'b',
|
|
199
|
-
output : 'foo_replaced.js'
|
|
200
|
-
})
|
|
120
|
+
```bash
|
|
121
|
+
frs-replace a b -i foo/*.js -o foo_replaced.js --o-join-str "\n/////\n"
|
|
201
122
|
```
|
|
202
123
|
|
|
203
|
-
|
|
124
|
+
##### 5. Replace all `a` occurrences with `b` in a content string `abcd` and save the result to the `foo_replaced.js` (using CLI)
|
|
204
125
|
|
|
205
126
|
```bash
|
|
206
|
-
frs-replace a b
|
|
127
|
+
frs-replace a b --content abcd -o foo_replaced.js
|
|
207
128
|
```
|
|
208
129
|
|
|
209
|
-
|
|
130
|
+
##### 6. Replace all `a` occurrences with `b` from a piped stream and save it to the output file (using CLI)
|
|
210
131
|
|
|
211
132
|
```bash
|
|
212
|
-
frs-replace a b
|
|
133
|
+
<read-file> | frs-replace a b > <output-file-path>
|
|
213
134
|
```
|
|
214
135
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
</details>
|
|
136
|
+
##### 7. Replace all `a` occurrences with `b` from a piped stream and pass it through the `stdout` stream to the `<next-command>` (using CLI)
|
|
218
137
|
|
|
219
|
-
|
|
138
|
+
```bash
|
|
139
|
+
<read-file> | frs-replace a b | <next-command>
|
|
140
|
+
```
|
|
220
141
|
|
|
221
|
-
|
|
142
|
+
## :books: Node API
|
|
222
143
|
|
|
223
|
-
|
|
144
|
+
@frsource/frs-replace package is shipped in 2 flavors: synchronous and asynchronous:
|
|
224
145
|
|
|
225
146
|
```javascript
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
147
|
+
import { sync, async } from '@frsource/frs-replace';
|
|
148
|
+
|
|
149
|
+
sync({
|
|
150
|
+
/* options */
|
|
151
|
+
});
|
|
152
|
+
await async({
|
|
153
|
+
/* options */
|
|
154
|
+
});
|
|
233
155
|
```
|
|
234
156
|
|
|
235
|
-
|
|
157
|
+
Where `/* options */` is an object containing:
|
|
236
158
|
|
|
237
|
-
|
|
238
|
-
frs-replace a b -i foo/*.js -o foo_replaced.js --o-join-str "\n/////\n"
|
|
239
|
-
```
|
|
159
|
+
> Note: one of parameters: **input** or **content** is required
|
|
240
160
|
|
|
241
|
-
|
|
161
|
+
| Option | Type | Default | Description |
|
|
162
|
+
| ------------------ | ------------------------------------------------------------------------------------------------------------------------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
163
|
+
| input | string or string[] | _undefined_ | Path to files or [fast-glob](https://github.com/mrmlnc/fast-glob) pattern pointing to files to be read & replaced from. If multiple files specified results will be joined using `outputJoinString` option's value) |
|
|
164
|
+
| inputReadOptions | string or object | utf8 | Options which are passed directly to the [readFileSync method](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) when reading input file |
|
|
165
|
+
| inputGlobOptions | object | _undefined_ | Options which are passed directly to the [fast-glob package](https://github.com/mrmlnc/fast-glob#options-1) when resolving glob patterns |
|
|
166
|
+
| content | string | _undefined_ | Content to be replaced (takes precedence over file input) |
|
|
167
|
+
| strategy | "join" or "flatten" or "preserve-structure" | "join" | Output file generation strategy. _"join"_ - joins all input files and outputs them as a single file using path passed as: _"output"_. _"preserve-structure"_ - saves all files to the _"output"_ directory keeping relative directory structure._"flatten"_ - same as _"preserve-structure"_ but flattens the directory structure |
|
|
168
|
+
| needle | string or [RegExp Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Syntax) | _-_ | Used as a first argument of [javascript replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Syntax) |
|
|
169
|
+
| replacement | string | _-_ | Passed as a second argument to [javascript replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Syntax) |
|
|
170
|
+
| output | string | _undefined_ | Path of an output file |
|
|
171
|
+
| outputWriteOptions | string or object | "utf8" | Passed as options argument of [write's .sync](https://www.npmjs.com/package/write#sync) |
|
|
172
|
+
| outputJoinString | string | \n | String used when joining multiple files, passed directly to [javascript join](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join#Syntax) |
|
|
242
173
|
|
|
243
|
-
|
|
174
|
+
## :mag_right: Examples and recipes - Node
|
|
244
175
|
|
|
245
|
-
|
|
176
|
+
> Note: While all of examples are using synchronous API method in all cases your can use `async` as well.
|
|
246
177
|
|
|
247
|
-
|
|
178
|
+
##### 1. Replace all `a` occurrences with `b` from the `foo.js` file and return the result
|
|
248
179
|
|
|
249
180
|
```javascript
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
181
|
+
import { sync } from '@frsource/frs-replace';
|
|
182
|
+
|
|
183
|
+
const result = sync({
|
|
184
|
+
input: 'foo.js',
|
|
185
|
+
regex: new RegExp('a', 'g'),
|
|
186
|
+
replacement: 'b',
|
|
187
|
+
output: 'foo_replaced.js',
|
|
188
|
+
});
|
|
256
189
|
```
|
|
257
190
|
|
|
258
|
-
|
|
191
|
+
##### 2. Replace all `a` occurrences with `b` from `foo.js` and save the result to the `foo_replaced.js`
|
|
259
192
|
|
|
260
|
-
```
|
|
261
|
-
|
|
193
|
+
```javascript
|
|
194
|
+
import { sync } from '@frsource/frs-replace';
|
|
195
|
+
const result = sync({
|
|
196
|
+
input: 'foo.js',
|
|
197
|
+
regex: new RegExp('a', 'g'),
|
|
198
|
+
replacement: 'b',
|
|
199
|
+
output: 'foo_replaced.js',
|
|
200
|
+
});
|
|
262
201
|
```
|
|
263
202
|
|
|
264
|
-
|
|
203
|
+
##### 3. Replace all `a` occurrences with `b` from an array of files and save the result to the `foo_replaced.js` using default `\n` as a result-joining string
|
|
265
204
|
|
|
266
|
-
|
|
205
|
+
```javascript
|
|
206
|
+
import { sync } from '@frsource/frs-replace';
|
|
207
|
+
const result = sync({
|
|
208
|
+
input: ['foo.js', 'foo2.js'],
|
|
209
|
+
regex: new RegExp('a', 'g'),
|
|
210
|
+
replacement: 'b',
|
|
211
|
+
output: 'foo_replaced.js',
|
|
212
|
+
});
|
|
213
|
+
```
|
|
267
214
|
|
|
268
|
-
|
|
215
|
+
##### 4. Replace all `a` occurrences with `b` from all `.js` files in the `foo` directory and save the result to the `foo_replaced.js` using `\n/////\n` as a result-joining string
|
|
269
216
|
|
|
270
|
-
```
|
|
271
|
-
|
|
217
|
+
```javascript
|
|
218
|
+
import { sync } from '@frsource/frs-replace';
|
|
219
|
+
const result = sync({
|
|
220
|
+
input: 'foo/*.js',
|
|
221
|
+
regex: new RegExp('a', 'g'),
|
|
222
|
+
replacement: 'b',
|
|
223
|
+
outputJoinString: '\n/////\n',
|
|
224
|
+
output: 'foo_replaced.js',
|
|
225
|
+
});
|
|
272
226
|
```
|
|
273
227
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
#### 7.1 CLI
|
|
228
|
+
##### 5. Replace all `a` occurrences with `b` in a content string `abcd` and save the result to the `foo_replaced.js`
|
|
277
229
|
|
|
278
|
-
```
|
|
279
|
-
|
|
230
|
+
```javascript
|
|
231
|
+
import { sync } from '@frsource/frs-replace';
|
|
232
|
+
const result = sync({
|
|
233
|
+
content: 'abcd',
|
|
234
|
+
regex: new RegExp('a', 'g'),
|
|
235
|
+
replacement: 'b',
|
|
236
|
+
output: 'foo_replaced.js',
|
|
237
|
+
});
|
|
280
238
|
```
|
|
281
239
|
|
|
282
|
-
|
|
240
|
+
## :chart_with_upwards_trend: Benchmarks
|
|
241
|
+
|
|
242
|
+
> Tested on Node v20.11.1.
|
|
283
243
|
|
|
284
|
-
|
|
244
|
+
### input as glob pattern [40 files]
|
|
285
245
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
246
|
+
| Rank | Library | Average latency [ms] | Difference percentage (comparing to best average latency) |
|
|
247
|
+
| ---- | ----------------------------- | -------------------- | ----------------------------------------------------------------------------- |
|
|
248
|
+
| 1 | @frsource/frs-replace (sync) | 0.45 ± 1.03% | +0.00% |
|
|
249
|
+
| 2 | replace-in-file (async) | 0.70 ± 1.24% | +55.69% |
|
|
250
|
+
| 3 | @frsource/frs-replace (async) | 1.81 ± 1.22% | +304.17% |
|
|
251
|
+
| 4 | replace-in-file (async) | 2.88 ± 1.18% | +540.38% |
|
|
289
252
|
|
|
290
|
-
|
|
253
|
+
### input & replacement as strings
|
|
291
254
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
|
297
|
-
| --- | --- | --- |
|
|
298
|
-
| frs-replace async | 0.01959564 | 57.5020% |
|
|
299
|
-
| **frs-replace sync** | 0.01244152 | 0.0000% |
|
|
300
|
-
| replace-in-file | 0.02223758 | 78.7368% |
|
|
301
|
-
| replace async | *N/A* | *N/A* |
|
|
302
|
-
| replace sync | 0.06111267 | 391.1992% |
|
|
303
|
-
| replace-string | *N/A* | *N/A* |
|
|
304
|
-
|
|
305
|
-
### input & replacement as strings [1000 iterations x 100 repetitions]
|
|
306
|
-
|
|
307
|
-
| Library (best bolded) | Execution time [s] | Difference percentage (comparing to best time) |
|
|
308
|
-
| --- | --- | --- |
|
|
309
|
-
| frs-replace async | 0.00020093 | 516.9883% |
|
|
310
|
-
| **frs-replace sync** | 0.00003257 | 0.0000% |
|
|
311
|
-
| replace-in-file | *N/A* | *N/A* |
|
|
312
|
-
| replace async | *N/A* | *N/A* |
|
|
313
|
-
| replace sync | *N/A* | *N/A* |
|
|
314
|
-
| replace-string | 0.00003438 | 5.5692% |
|
|
255
|
+
| Rank | Library | Average latency [ms] | Difference percentage (comparing to best average latency) |
|
|
256
|
+
| ---- | ----------------------------- | -------------------- | ----------------------------------------------------------------------------- |
|
|
257
|
+
| 1 | @frsource/frs-replace (sync) | 0.01 ± 0.28% | +0.00% |
|
|
258
|
+
| 2 | @frsource/frs-replace (async) | 0.01 ± 0.35% | +12.83% |
|
|
259
|
+
| 3 | replaceString | 0.08 ± 1.12% | +932.99% |
|