5.1.51 • Published 7 months ago

@frsource/frs-replace v5.1.51

Weekly downloads
9
License
MIT
Repository
github
Last release
7 months ago

:scroll: Installation

npm install @frsource/frs-replace

or execute it right away:

npx @frsource/frs-replace

:keyboard: CLI

frs-replace <regex> <replacement> [flags]

Basic usage:

frs-replace something anything -i foo.js -o foo_replaced.js

# all of the occurences of "something" in `foo.js` will be replaced with "anything" and saved to `foo_replaced.js`

More examples below.

Arguments

ArgumentTypeDescription
\<regex>stringFirst parameter to RegExp constructor
\<replacement>stringString or path to replacement function file (see replacefn switch for details)

Flags

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.

Note: Object types can be set using 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.

OptionTypeDefaultDescription
i, inputstring or string[]-Path to files or fast-glob pattern pointing to files to be read & replaced from. If multiple files specified results will be joined using outputJoinString option's value)
i-read-optsstring or objectutf8Options which are passed directly to the readFileSync method when reading input file
i-glob-optsobjectundefinedOptions which are passed directly to the fast-glob package when resolving glob patterns
stdinbooleantrueWait for stdin input (should be set to false when used in non-interactive terminals)
o, outputstring-Output file name/path (replaces the file if it already exists and creates any intermediate directories if they don't already exist)
o-write-optsstring or objectutf8Passed as options argument of write's .sync
o-join-strstring\nUsed when joining multiple files, passed directly to javascript join
c, contentstring-Content to be replaced (takes precedence over stream & file input)
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
f, flagsstring, combination of gim flagsgRegExp flags
stdoutbooleantrue if piped input present, false otherwiseForce sending output on stdout
r, replacefnbooleanfalseTreat replacement argument as path to file containing replacement function
h, helpboolean-Show help
v, versionboolean-Show version number

:mag_right: Examples and recipes - CLI

1. Replace all a occurrences with b from the foo.js file and return the result (using CLI)
frs-replace a b -i foo.js --stdout
2. Replace all a occurrences with b from foo.js and save the result to the foo_replaced.js (using CLI)
frs-replace a b -i foo.js -o foo_replaced.js
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)
frs-replace a b -i foo.js -i foo2.js -o foo_replaced.js
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)
frs-replace a b -i foo/*.js -o foo_replaced.js --o-join-str "\n/////\n"
5. Replace all a occurrences with b in a content string abcd and save the result to the foo_replaced.js (using CLI)
frs-replace a b --content abcd -o foo_replaced.js
6. Replace all a occurrences with b from a piped stream and save it to the output file (using CLI)
<read-file> | frs-replace a b > <output-file-path>
7. Replace all a occurrences with b from a piped stream and pass it through the stdout stream to the <next-command> (using CLI)
<read-file> | frs-replace a b | <next-command>

:books: Node API

@frsource/frs-replace package is shipped in 2 flavors: synchronous and asynchronous:

import { sync, async } from '@frsource/frs-replace';

sync({
  /* options */
});
await async({
  /* options */
});

Where /* options */ is an object containing:

Note: one of parameters: input or content is required

OptionTypeDefaultDescription
inputstring or string[]undefinedPath to files or fast-glob pattern pointing to files to be read & replaced from. If multiple files specified results will be joined using outputJoinString option's value)
inputReadOptionsstring or objectutf8Options which are passed directly to the readFileSync method when reading input file
inputGlobOptionsobjectundefinedOptions which are passed directly to the fast-glob package when resolving glob patterns
contentstringundefinedContent to be replaced (takes precedence over file input)
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
needlestring or RegExp Object-Used as a first argument of javascript replace
replacementstring-Passed as a second argument to javascript replace
outputstringundefinedPath of an output file
outputWriteOptionsstring or object"utf8"Passed as options argument of write's .sync
outputJoinStringstring\nString used when joining multiple files, passed directly to javascript join

:mag_right: Examples and recipes - Node

Note: While all of examples are using synchronous API method in all cases your can use async as well.

1. Replace all a occurrences with b from the foo.js file and return the result
import { sync } from '@frsource/frs-replace';

const result = sync({
  input: 'foo.js',
  regex: new RegExp('a', 'g'),
  replacement: 'b',
  output: 'foo_replaced.js',
});
2. Replace all a occurrences with b from foo.js and save the result to the foo_replaced.js
import { sync } from '@frsource/frs-replace';
const result = sync({
  input: 'foo.js',
  regex: new RegExp('a', 'g'),
  replacement: 'b',
  output: 'foo_replaced.js',
});
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
import { sync } from '@frsource/frs-replace';
const result = sync({
  input: ['foo.js', 'foo2.js'],
  regex: new RegExp('a', 'g'),
  replacement: 'b',
  output: 'foo_replaced.js',
});
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
import { sync } from '@frsource/frs-replace';
const result = sync({
  input: 'foo/*.js',
  regex: new RegExp('a', 'g'),
  replacement: 'b',
  outputJoinString: '\n/////\n',
  output: 'foo_replaced.js',
});
5. Replace all a occurrences with b in a content string abcd and save the result to the foo_replaced.js
import { sync } from '@frsource/frs-replace';
const result = sync({
  content: 'abcd',
  regex: new RegExp('a', 'g'),
  replacement: 'b',
  output: 'foo_replaced.js',
});

:chart_with_upwards_trend: Benchmarks

Tested on Node v22.16.0.

input as glob pattern 40 files

RankLibraryAverage latency msDifference percentage (comparing to best average latency)
1@frsource/frs-replace (sync)0.40 ± 1.11%+0.00%
2replace-in-file (sync)0.71 ± 1.28%+77.42%
3@frsource/frs-replace (async)1.72 ± 1.48%+329.86%
4replace-in-file (async)2.82 ± 1.55%+607.31%

input & replacement as strings

RankLibraryAverage latency msDifference percentage (comparing to best average latency)
1@frsource/frs-replace (sync)0.01 ± 0.41%+0.00%
2@frsource/frs-replace (async)0.01 ± 0.27%+9.95%
3replaceString0.15 ± 1.15%+1787.20%
5.1.5

12 months ago

5.1.4

1 year ago

5.1.3

1 year ago

5.1.2

1 year ago

5.1.1

1 year ago

5.1.0

1 year ago

5.1.49

7 months ago

5.1.48

7 months ago

5.1.47

8 months ago

5.1.46

8 months ago

5.1.45

8 months ago

5.1.44

8 months ago

5.1.43

8 months ago

5.1.42

8 months ago

5.1.41

8 months ago

5.1.40

8 months ago

5.1.39

8 months ago

5.1.38

8 months ago

5.1.37

8 months ago

5.1.36

9 months ago

5.1.35

9 months ago

5.1.34

9 months ago

5.1.33

9 months ago

5.1.32

9 months ago

5.1.31

9 months ago

5.1.30

9 months ago

5.0.4

1 year ago

5.0.3

1 year ago

5.0.2

1 year ago

5.0.1

1 year ago

5.0.0

1 year ago

5.1.29

10 months ago

5.1.28

10 months ago

5.1.27

10 months ago

5.1.26

10 months ago

5.1.25

10 months ago

5.1.24

10 months ago

5.1.23

10 months ago

5.1.22

10 months ago

5.1.21

10 months ago

5.1.20

10 months ago

5.1.19

10 months ago

5.1.18

11 months ago

5.1.17

11 months ago

5.1.16

11 months ago

5.1.15

11 months ago

5.1.14

11 months ago

5.1.13

11 months ago

5.1.12

11 months ago

5.1.11

11 months ago

5.1.10

12 months ago

5.1.9

12 months ago

5.1.51

7 months ago

5.1.8

12 months ago

5.1.50

7 months ago

5.1.7

12 months ago

5.1.6

12 months ago

4.1.1

3 years ago

4.1.0

3 years ago

4.0.0

3 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.1.2

5 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.2.0

6 years ago