Skip to content

Commit

Permalink
clarify 🔬
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed Mar 1, 2019
1 parent 0430236 commit fc25f9a
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,26 @@ This will execute a provided function to every prop in the object recursively. T
```js
import { findAndReplaceIf } from 'find-and-replace-anything'

// function that replaces 'a' with 'b'
function checkFn (foundVal) {
if (foundVal === 'a') return 'b'
return foundVal
// always return original foundVal when no replacement occurs
}

findAndReplaceIf({_1: {_2: {_3: 'a'}}}, checkFn)
findAndReplaceIf({deep: {nested: {prop: 'a'}}}, checkFn)
// returns
{_1: {_2: {_3: 'b'}}}
{deep: {nested: {prop: 'b'}}}

// this is what gets executed in order:
checkFn({deep: {nested: {prop: 'a'}}})
checkFn({nested: {prop: 'a'}})
checkFn({prop: 'a'})
checkFn('a')
// the final execution replaces 'a' with 'b'
// and then returns the entire object

// also works on non-objects
findAndReplace('a', checkFn)
// returns
'b'
Expand Down

0 comments on commit fc25f9a

Please sign in to comment.