Git Product home page Git Product logo

babak-gholamzadeh / nested-replace Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 5 KB

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.

Home Page: https://www.npmjs.com/package/nested-replace

License: MIT License

JavaScript 100.00%
javascript nodejs object string replace regular-expression regex nested array

nested-replace's Introduction

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

nested-replace's People

Contributors

babak-gholamzadeh avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.