Socket
Socket
Sign inDemoInstall

each-props

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

each-props

Process each properties deeply.


Version published
Weekly downloads
1.3M
increased by0.06%
Maintainers
1
Weekly downloads
 
Created
Source

each-props NPM MIT License Build Status Build Status Coverage Status

Process object properties deeply.

Install

$ npm i each-props --save

Usage

  • Load this module :

    const eachProps = require('each-props');
    
  • Apply a function to all (non plain object) properties.

    var obj = { a: 1, b: { c: 'CCC', d: { e: 'EEE' } } };
    
    eachProps(obj, function(value, keyChain, nodeInfo) {
      if (keyChain === 'a') {
        nodeInfo.parent['a'] = value * 2;
      } else if (keyChain === 'b.c') {
        nodeInfo.parent['c'] = value.toLowerCase();
      } else if (keyChain === 'b.d') {
        return true; // stop to dig
      } else if (keyChain === 'b.d.e') {
        nodeInfo.parent['e'] = value.toLowerCase();
      }
    });
    console.log(obj);
    // => { a: 2, b: { c: 'ccc', d: { e: 'EEE' } } };
    

API

eachProps(obj, callback [, opts]) => void

Executes the callback function for all properties.

Arguments :
  • obj [object] : A plain object to be treated.
  • fn [function] : A function to treat the plain object.
  • opts [object] : An object to be able to has options.
API of fn function
  • fn(value, keyChain, nodeInfo) => boolean

    • Arguments :

      • value [any] : The property value.
      • keyChain [string] : A string concatenating the hierarchical keys with dots.
      • nodeInfo [object] : An object which contains properties: name, index, count, depth, parent, sort, and can contains more properties by specifying in opts above.
    • Returns : [boolean] : Stops digging child properties if true.

Properties of nodeInfo :
  • nodeInfo [object]
    • name [string] : The property name of this node.
    • index [number] : The index of the property among the sibling properties.
    • count [number] : The count of the sibling properties.
    • depth [number] : The depth in the property hierarchy.
    • parent [object] : The parent property.
    • sort [function] : A sort function which orders the child properties. This property is inherited from opts, if specified.
    • ... and any properties inherited from opts.
Properties of opts :
  • opts [object]
    • sort [function] : A sort function which orders the same level properties. (optional)
    • ... and any properties you want to pass to each node.

License

Copyright (C) 2016 Takayuki Sato

This program is free software under MIT License. See the file LICENSE in this distribution for more details.

Keywords

FAQs

Package last updated on 04 Aug 2017

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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc