Socket
Socket
Sign inDemoInstall

object-tree

Package Overview
Dependencies
1
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    object-tree

lookup js objects values from a flat structure


Version published
Weekly downloads
22
decreased by-12%
Maintainers
2
Install size
972 kB
Created
Weekly downloads
 

Readme

Source

Build Status

npm install --save object-tree

value lookup

string

var data = {
  food: {
    caramel: {
      tastes: {
        chocolate: 1.234
      }
    }
  }
}

var ot = new ObjectTree()

var actual = ot.lookup('food.caramel.tastes.chocolate', data)
console.log(actual) // 1.234

string with custom separator

var data = {
  food: {
    caramel: {
      tastes: {
        chocolate: 1.234
      }
    }
  }
}

var ot = new ObjectTree({seperator: '::'})

var actual = ot.lookup('food::caramel::tastes::chocolate', data)
console.log(actual) // 1.234

array

var data = {
  food: {
    caramel: {
      tastes: {
        chocolate: 1.234
      }
    }
  }
}

var ot = new ObjectTree()

var actual = ot.lookup(['food','caramel','tastes','chocolate','smells','code'], data)
console.log(actual) // 1.234

filter

var data = {
  food: {
    caramel: {
      tastes: {
        chocolate: 1.234
      }
    }
  }
}

var ot = new ObjectTree()

var actual = ot.lookup({'food':'caramel','tastes':'chocolate'}, data)
console.log(actual) // 1.234

filter with wildcard

var data = {
  food: {
    caramel: {
      tastes: {
        '*': 444,
        chocolate: 1.234
      }
    }
  }
}

var ot = new ObjectTree({
  wildcard: '*'
})

var actual = ot.lookup({'food':'caramel','tastes':'flower'}, data)
console.log(actual) // 444

template only

var ot = new ObjectTree()

var actual = ot.lookupTemplate('{food.caramel.tastes.chocolate}', {
    food: {
      'caramel': {
        tastes: {
          'chocolate': 1.234
        }
      }
    }
  }
)

assert.equal(actual, 1.234)

template with custom delimiter

var ot = new ObjectTree({
  template: {
    left: '<',
    right: '>'
  }
})

var actual = ot.lookupTemplate('<food.caramel.tastes.chocolate>', {
    food: {
      'caramel': {
        tastes: {
          'chocolate': 1.234
        }
      }
    }
  }
)

assert.equal(actual, 1.234)

reverse lookup (generate a tree)

generate filters from the tree

var tree = {
  the: {
    barn: {
      '*': 0,
      is: {
        free: 1
      }
    },
    food: {
      is: {
        tasty: 2
      }
    }
  }
}

var ot = new ObjectTree({wildcard: '*'})

var eventBus = ot.generateFilters(tree)

eventBus.on('filter', function(value, filter, attrList) {
  will be called 3 times:
  // value=0, filter={the: 'barn'}, attrList=['the', 'barn']
  // value=1, filter={the: 'barn', is: 'free'}, attrList=['the', 'barn', 'is', 'free']
  // value=2, filter={the: 'food', is: 'tasty'}, attrList=['the', 'food', 'is', 'tasty']
})
eventBus.on('end', function() {
  // no more 'filter' events
})

set value

var obj = {attr1: false, attr2: true}

ot.set('attr2.nested1.nested2', Date.now(), obj)) // true

set returns true if the object was modified

Keywords

FAQs

Last updated on 09 Jun 2014

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc