Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#10 better parallelization #19

Merged
merged 3 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
dist: trusty
sudo: required
language: node_js
node_js:
- "node"
- "lts/*"
cache: yarn
matrix:
include:
- name: "Standard linting"
script: yarn standard
node_js: "lts/*"
- name: "Unit tests & coverage"
script: yarn test
node_js:
- "node"
- "lts/*"
- name: "Benchmark test - glob"
script: travis_wait yarn test:benchmark:glob
node_js:
- "node"
- "lts/*"
- name: "Benchmark test - string"
script: yarn test:benchmark:string
node_js:
- "node"
- "lts/*"
113 changes: 67 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# FRS-replace

[![NPM version](https://img.shields.io/npm/v/frs-replace.svg?style=flat)](https://www.npmjs.com/package/frs-replace)
[![Build Status](https://travis-ci.org/FRSource/FRS-replace.svg?branch=master)](https://travis-ci.org/FRSource/FRS-replace)
[![Coverage Status](https://coveralls.io/repos/github/FRSource/FRS-replace/badge.svg?branch=master)](https://coveralls.io/github/FRSource/FRS-replace?branch=master)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) [![Greenkeeper badge](https://badges.greenkeeper.io/FRSource/FRS-replace.svg)](https://greenkeeper.io/)
[![codebeat badge](https://codebeat.co/badges/5496a006-a13d-48cc-baeb-37c79a1f6444)](https://codebeat.co/projects/github-com-frsource-frs-replace-master)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

# FRS-replace

CLI & Node wrapper around [javascript replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) which allows on-the-fly replacing (with or without changing input files), [globbing](https://en.wikipedia.org/wiki/Glob_(programming)), [piping](https://en.wikipedia.org/wiki/Pipeline_(Unix)) and many more!
The fastest ([see benchmarks](#benchmarks)) CLI & Node wrapper around [javascript replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) which allows on-the-fly replacing (with or without changing input files), [globbing](https://en.wikipedia.org/wiki/Glob_(programming)), [piping](https://en.wikipedia.org/wiki/Pipeline_(Unix)) and many more!

* [Installation](#installation)
* [Node API usage](#node-api-usage)
Expand All @@ -16,17 +16,20 @@ CLI & Node wrapper around [javascript replace](https://developer.mozilla.org/en-
* [Benchmarks](#benchmarks)

## Installation
##### yarn
```
$ yarn add frs-replace
```

##### npm
yarn

```bash
yarn add frs-replace
```
$ npm install frs-replace

npm

```bash
npm install frs-replace
```

##### download
download
[zipped from FRS-replace Releases](https://github.com/FRSource/FRS-replace/releases)

## Node API usage
Expand Down Expand Up @@ -61,14 +64,16 @@ Where `/* options */` is an object containing:
FRS-replace <regex> <replacement> [options]
```

#### Positionals:
### Positionals

| Option | Type | Description |
| --- | --- | --- |
| \<regex\> | string | First parameter to [RegExp constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Syntax) |
| \<replacement\> | string | String or path to replacement function file (see &#8209;&#8209;replace&#8209;fn switch for details) |

#### Options:
> 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.
### Options

> 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](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`.

Expand All @@ -91,9 +96,9 @@ FRS-replace <regex> <replacement> [options]

> Note: while most of examples is using synchronous API method, in all cases `.async` is applicable as well.

#### 1. Replace all `a` occurences with `b` from given `foo.js` and returns result / writes result to console :
### 1. Replace all `a` occurences with `b` from given `foo.js` and returns result / writes result to console

###### API
#### 1.1 API

```javascript
const FRSReplace = require('FRS-replace')
Expand Down Expand Up @@ -127,14 +132,16 @@ const resultAsync = await FRSReplace.async({

```

###### CLI
#### 1.2 CLI

```bash
FRS-replace a b -i foo.js --stdout
```

#### 2. Replace all `a` occurences with `b` from given `foo.js` and save result to `foo_replaced.js` :
### 2. Replace all `a` occurences with `b` from given `foo.js` and save result to `foo_replaced.js`

#### 2.1 API

###### API
```javascript
const result = require('FRS-replace').sync({
input : 'foo.js',
Expand All @@ -144,14 +151,16 @@ const result = require('FRS-replace').sync({
})
```

###### CLI
#### 2.2 CLI

```bash
FRS-replace a b -i foo.js -o foo_replaced.js
```

#### 3. Replace all `a` occurences with `b` from given array of files and save result to `foo_replaced.js` using default `\n` as result-joining string :
### 3. Replace all `a` occurences with `b` from given array of files and save result to `foo_replaced.js` using default `\n` as result-joining string

#### 3.1 API

###### API
```javascript
const result = require('FRS-replace').sync({
input : ['foo.js', 'foo2.js'],
Expand All @@ -161,22 +170,24 @@ const result = require('FRS-replace').sync({
})
```

###### CLI
#### 3.2 CLI

```bash
FRS-replace a b -i foo.js foo2.js -o foo_replaced.js --i-join-str "\n/////\n"
```

*or*
or

```bash
FRS-replace a b -i foo.js -i foo2.js -o foo_replaced.js --i-join-str "\n/////\n"
```

> Note: Arrays can be passed under single flag-entry as a space-separated list *or* under same flag repeated multiple times (all values will be concatenated into single array using, details - [yargs array notation](https://github.com/yargs/yargs-parser#dot-notation)).

#### 4. Replace all `a` occurences with `b` from all `.js` files in `foo` directory and save result to `foo_replaced.js` using `\n/////\n` as result-joining string :
### 4. Replace all `a` occurences with `b` from all `.js` files in `foo` directory and save result to `foo_replaced.js` using `\n/////\n` as result-joining string

#### 4.1 API

###### API
```javascript
const result = require('FRS-replace').sync({
input : 'foo/*.js',
Expand All @@ -187,14 +198,16 @@ const result = require('FRS-replace').sync({
})
```

###### CLI
#### 4.2 CLI

```bash
FRS-replace a b -i foo/*.js -o foo_replaced.js --i-join-str "\n/////\n"
```

#### 5. Replace all `a` occurences with `b` in given content string `abcd` and save result to `foo_replaced.js`
### 5. Replace all `a` occurences with `b` in given content string `abcd` and save result to `foo_replaced.js`

#### 5.1 API

###### API
```javascript
const result = require('FRS-replace').sync({
content : 'abcd',
Expand All @@ -204,48 +217,56 @@ const result = require('FRS-replace').sync({
})
```

###### CLI
#### 5.2 CLI

```bash
FRS-replace a b --content abcd -o foo_replaced.js
```

#### 6. Replace all `a` occurences with `b` from piped stream and save it to output file:
### 6. Replace all `a` occurences with `b` from piped stream and save it to output file

#### 6.1 CLI

###### CLI
```bash
<read-file> | FRS-replace a b > <output-file-path>
```

#### 7. Replaces all `a` occurences with `b` from piped stream and pass it through `stdout` stream to next command

###### CLI
### 7. Replaces all `a` occurences with `b` from piped stream and pass it through `stdout` stream to next command

#### 7.1 CLI

```bash
<read-file> | FRS-replace a b | <next-command>
```

#### 8. Both pipe & options styles can be mixed together, here - getting input from `i` argument and passing output down the stream to next command
### 8. Both pipe & options styles can be mixed together, here - getting input from `i` argument and passing output down the stream to next command

#### 8.1 CLI

###### CLI
```bash
FRS-replace a b -i foo.js | <next-command>
```

## Benchmarks
#### input as glob pattern [1000 iterations x 100 repetitions]
## Benchmarks (Node v10.16.0)

### input as glob pattern [40 files x 1000 iterations x 100 repetitions]

| Library (best&nbsp;bolded) | Execution time [s] | Difference percentage (comparing&nbsp;to&nbsp;best&nbsp;time) |
| --- | --- | --- |
| **FRS-replace async** | 0.36640944 | 0.0000% |
| FRS-replace sync | 0.39553770 | 7.9496% |
| replace-in-file | 1.78587186 | 387.3979% |
| FRS-replace async | 0.01761663 | 103.7503% |
| **FRS-replace sync** | 0.00864619 | 0.0000% |
| replace-in-file | 0.02154322 | 149.1644% |
| replace async | *N/A* | *N/A* |
| replace sync | 0.44655926 | 21.8744% |
| replace sync | 0.05026399 | 481.3428% |
| replace-string | *N/A* | *N/A* |
#### input & replacement as strings [1000 iterations x 100 repetitions]

### input & replacement as strings [1000 iterations x 100 repetitions]

| Library (best&nbsp;bolded) | Execution time [s] | Difference percentage (comparing&nbsp;to&nbsp;best&nbsp;time) |
| --- | --- | --- |
| FRS-replace async | 0.01015828 | 59.0095% |
| FRS-replace sync | 0.00657347 | 2.8957% |
| FRS-replace async | 0.00011756 | 215.1822% |
| **FRS-replace sync** | 0.00003730 | 0.0000% |
| replace-in-file | *N/A* | *N/A* |
| replace async | *N/A* | *N/A* |
| replace sync | *N/A* | *N/A* |
| **replace-string** | 0.00638847 | 0.0000% |
| replace-string | 0.00004905 | 31.5049% |
Loading