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

nested-replace

v1.0.0

Published

Find a specified string or a regular expression (in a string, array or object value) and returns a new value where the specified string are replaced.

Downloads

4

Readme

Nested Replace

Find a specified string or a regular expression in a string, or a nested array/object value) and returns a new value where the specified string are replaced.

An Example usage of this package could be when you need to replace a value in entire a big json

Installation

$ npm install nested-replace

Usage

const nestedReplace = require('nested-replace');

// The usage of this function is similar to the native String replace() method
// The only difference is the first parameter which is the input value that would be processed for replacement

// String value as input
const str = 'this is a string value';
const newStr = nestedReplace(str, 'is', 'XX');
// -> newStr = 'thXX is a string value'

// You can use Regex if you need (i.e. for replace all)
const newStr = nestedReplace(str, /is/g, 'XX');
// -> newStr = 'thXX XX a string value'

// And you also use a callback as third parameter (like native replace())
// to access the found value
const str = '0123456789';
const newStr = nestedReplace(/[0-9]/g, match => match % 2 ? 'x' : 'y')
// -> newStr = 'yxyxyxyxyx'

And finally the most interesting part is that you can do all of this kind of things with any kind of nested array and/or object

const input = {
  a: 'this is a string value',
  b: [
    'this is a string value inside an array',
    [
      'this is a string value inside a nested array'
    ],
    {
      c: {
        d: [
          {
            e: 'this is a string value inside a nested object which is inside a nested array which is inside a nested object which is inside another nested object :)'
          }
        ]
      }
    }
  ],
};

const newInput = nestedReplace(input, /is/g, 'XX');
//  -> newValue = {
//   a: 'thXX XX a string value',
//   b: [
//     'thXX XX a string value inside an array',
//     [
//       'thXX XX a string value inside a nested array'
//     ],
//     {
//       c: {
//         d: [
//           {
//             e: 'thXX XX a string value inside a nested object which XX inside a nested array which XX inside a nested object which XX inside another nested object :)'
//           }
//         ]
//       }
//     }
//   ],
// };

Note: This function always returns a new value and does not modify the input value

If you do not use regex for replace all, it only replaces the first found value in each string value in entire object

For example:

const newInput = nestedReplace(input, 'is', 'XX');
// -> newInput = {
//   a: 'thXX is a string value',
//   b: [
//     'thXX is a string value inside an array',
//     [
//       'thXX is a string value inside a nested array'
//     ],
//     {
//       c: {
//         d: [
//           {
//             e: 'thXX is a string value inside a nested object which is inside a nested array which is inside a nested object which is inside another nested object :)'
//           }
//         ]
//       }
//     }
//   ],
// };