https://github.com/75lb/find-replace
Replace or remove multiple items in an array
https://github.com/75lb/find-replace
Last synced: over 1 year ago
JSON representation
Replace or remove multiple items in an array
- Host: GitHub
- URL: https://github.com/75lb/find-replace
- Owner: 75lb
- License: mit
- Created: 2015-07-01T22:14:56.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2024-09-17T00:14:17.000Z (almost 2 years ago)
- Last Synced: 2024-10-12T08:10:52.853Z (almost 2 years ago)
- Language: JavaScript
- Homepage:
- Size: 256 KB
- Stars: 5
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.hbs
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.org/package/find-replace)
[](https://www.npmjs.org/package/find-replace)
[](https://github.com/75lb/find-replace/network/dependents?dependent_type=REPOSITORY)
[](https://github.com/75lb/find-replace/network/dependents?dependent_type=PACKAGE)
[](https://github.com/75lb/find-replace/actions/workflows/node.js.yml)
[](https://github.com/feross/standard)
# find-replace
Replace or remove multiple items in an array.
Similar to [array.prototype.splice()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) with the following differences:
* `splice` only operates on one item at a time requiring you to know its index. `find-replace` will operate on every item satisfying the find function.
* If a function is passed as a `replaceWith` argument, `find-replace` will invoke it to compute the replacement value.
## Synopsis
```js
import findReplace from 'find-replace'
const colours = ['red', 'white', 'blue', 'white']
const result = findReplace(
colours,
colour => colour === 'white',
'gold'
)
console.log(result)
// [ 'red', 'gold', 'blue', 'gold' ]
```
If the `replaceWith` value is a function, it will be invoked with the found item and its result used as the replace value. For example:
```js
const colours = ['red', 'white', 'blue', 'white']
const result = findReplace(
colours,
colour => colour === 'red',
colour => colour.split('')
)
console.log(result)
// [ 'r', 'e', 'd', 'white', 'blue', 'white' ]
```
## Real world examples
### Replace with an array of strings
This example explodes combined (`-vrf`) into individual flags (`-v -r -f`).
```js
import findReplace from 'find-replace'
const argv = ['-vrf', 'file1.js', 'file2.js']
const combinedShortOptionRe = /^-[^\d-]{2,}$/
const result = findReplace(
argv,
arg => combinedShortOptionRe.test(arg),
arg => {
return arg
.slice(1) /* remove initial hypen */
.split('')
.map(letter => '-' + letter)
}
)
console.log(result)
```
Output:
```
$ node example/argv.mjs
[ '-v', '-r', '-f', 'file1.js', 'file2.js' ]
```
### Delete found items
If you omit the third `replaceWith` argument, all found items will be deleted.
```js
import findReplace from 'find-replace'
const fruits = ['apple', 'pear', 'nectarine', 'pineapple', 'peach']
const bad = ['pear', 'pineapple']
const result = findReplace(
fruits,
fruit => bad.includes(fruit)
)
console.log(result)
```
Output:
```
$ node example/delete.mjs
[ 'apple', 'nectarine', 'peach' ]
```
# API Reference
{{>main}}
# Load anywhere
This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.
Node.js (CommonJS):
```js
const findReplace = require('find-replace')
```
Node.js (ECMAScript Module):
```js
import findReplace from 'find-replace'
```
Modern browser (ECMAScript Module):
```js
import findReplace from './node_modules/find-replace/dist/index.mjs'
```
Old browser (adds `window.findReplace`):
```html
```
* * *
© 2015-25 Lloyd Brookes \<75pound@gmail.com\>.
Test suite by [test-runner](https://github.com/test-runner-js/test-runner). Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).