Git Product home page Git Product logo

scrubber's Introduction

srcubber

Find and replace values in objects. Easily walk unkown structures and operate on its values. In-place updates by just returning whatever you want the new value to be.

install

npm install scrubber

usage

Find and replace string ObjectIDs

var query = {
  $or: [
    {
      _id: '4fc487cc0afe090000000048'
    },
    {
      name: 'foo',
      _id: {$in: [1, 2, 3, '4fc487cc0afe090000000048']}
    },
    {
      number: {$gt: 7}
    }
  ]
}

scrub(query, function (obj, key, parent, type) {
  // find any value that might be an ObjectID
  if(type == 'string' && obj.length === 24) {
    try {
      // try and create one
      var id = ObjectID.createFromHexString(obj)
    } catch() { }
    
    // replace the value with id
    return id
  }
})

Sum all the numbers in an object.

sum = {
  a: [1]
  b: {
    c: [2],
    d: {
      e: [3],
      f: {
        g: [4]
      }
    }
  }
}

total = 0

scrub(sum, function(obj, key, parent, type) {
  if(type == number) {
    total += obj
  }
})

console.log(tota); // 10

Its also kinda cool that it can replace any other way of iterating. But don't use it unless you need to walk an unknown structure.

function count(obj) {
  var c = 0;
  scrub(obj, function() { c++ });
  return c;
}

count([1,2,3]) // 3

count({a: 1, b: 2, c:, 3}) // 3

var crazy = {
  a: {
    b: {
      c: {
        d: {
          e: [1, 2, 3, 4, {
            f: 5,
            g: {
              h: {i: 6, j: k: 7, l: 8, m: 9}
            }
          }]
        }
      }
    }
  }
};

count(crazy) // 9

scrubber's People

Contributors

ritch avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar

Forkers

maninga

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.