Launch Week Day 2: Introducing Reports: An Extensible Reporting Framework for Socket Data.Learn More
Socket
Book a DemoSign in
Socket

nested-replace

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nested-replace

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.

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

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 :)'
//           }
//         ]
//       }
//     }
//   ],
// };

Keywords

object

FAQs

Package last updated on 26 Jan 2021

Did you know?

Socket

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.

Install

Related posts