
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
find-replace
Advanced tools
Replace or remove multiple items in an array.
Similar to array.prototype.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.replaceWith
argument, find-replace
will invoke it to compute the replacement value.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:
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' ]
This example explodes combined (-vrf
) into individual flags (-v -r -f
).
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' ]
If you omit the third replaceWith
argument, all found items will be deleted.
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' ]
array
⏏Kind: Exported function
Param | Type | Description |
---|---|---|
array | array | The input array |
findFn | function | A predicate function which, if returns true causes the current item to be operated on. |
[...replaceWith] | any | If not specified, each found value will be removed. If specified, each found value will be replaced with this value. If the replaceWith value is a function, it will be invoked with the found value and its result used as the replace value. If the replaceWith function returns an array, the found value will be replaced with each item in the array (not replaced with the array itself). |
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):
const findReplace = require('find-replace')
Node.js (ECMAScript Module):
import findReplace from 'find-replace'
Modern browser (ECMAScript Module):
import findReplace from './node_modules/find-replace/dist/index.mjs'
Old browser (adds window.findReplace
):
<script nomodule src="./node_modules/find-replace/dist/index.js"></script>
© 2015-25 Lloyd Brookes <75pound@gmail.com>.
Test suite by test-runner. Documented by jsdoc-to-markdown.
FAQs
Replace or remove multiple items in an array.
The npm package find-replace receives a total of 1,925,570 weekly downloads. As such, find-replace popularity was classified as popular.
We found that find-replace demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.