npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

find-and-replace-anything

v4.0.2

Published

Replace one val with another or all occurrences in an object recursively. A simple & small integration.

Downloads

6,597

Readme

Find and replace anything 🎣

npm i find-and-replace-anything

Replace one val with another or all occurrences in an object recursively. A simple & small integration.

There are two methods you can import and use:

  • findAndReplace find a replace with b (recursively on an object)
  • findAndReplaceIf execute a function on every prop in an object recursively, and replace the prop with what the function returns

Meet the family (more tiny utils with TS support)

find and replace

This will find a value inside an object and replace it with another:

  • findAndReplace(object, find, replace)
import { findAndReplace } from 'find-and-replace-anything'

findAndReplace({deep: {nested: {prop: 'a'}}}, 'a', 'b')
  // returns
  {deep: {nested: {prop: 'b'}}}

findAndReplace('works on "exact" strings as well', 'a', 'b')
  // returns
  'works on "exact" strings as well'

findAndReplace('a', 'a', 'b')
  // returns
  'b'

// works with other types as well:
findAndReplace({nr: 1}, 1, 100)
  // returns
  {nr: 100}

find and replace IF

This will execute a provided function to every prop in the object recursively. The "check" function provided will receive the prop's value as param:

  • findAndReplaceIf(object, checkFn) checkFn receives each propVal of the object recursively
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({deep: {nested: {prop: 'a'}}}, checkFn)
  // returns
  {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'

A note on plain objects vs classes

only for findAndReplace()

Please note that it will also recursively look inside special objects like JavaScript classes etc. So make sure you test the behaviour properly in those cases! (especially when your classes have read-only properties etc.)

class MyClass {
  constructor () {
    this.prop = 1
  }
}
const target = {
  prop: 1,
  class: new MyClass()
}
findAndReplace(target, 1, 2)
  // this will replace 1 with 2 in the class as well and returns:
  {prop: 2, class: {prop: 2}}

If you need it to only recursively go through plain JavaScript object and avoid going in custom classes etc. you can pass a 4th parameter like so:

findAndReplace(target, 1, 2, {onlyPlainObjects: true})
  // this will replace 1 with 2 only in the plain object and returns:
  {prop: 2, class: {prop: 1}}

Also be careful with circular references! It will cause this library to crash.

Source code

It's literally just this:

/**
 * @param {*} target Target can be anything
 * @param {*} find val to find
 * @param {*} replaceWith val to replace
 * @returns the target with replaced values
 */
function findAndReplaceRecursively (target, find, replaceWith) {
  if (!isObject(target)) {
    if (target === find) return replaceWith
    return target
  }
  return Object.keys(target)
    .reduce((carry, key) => {
      const val = target[key]
      carry[key] = findAndReplaceRecursively(val, find, replaceWith)
      return carry
    }, {})
}