Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

copy-props

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

copy-props

Copy properties deeply between two objects.

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
467K
decreased by-61.47%
Maintainers
1
Weekly downloads
 
Created
Source

copy-props Build Status Build Status

Copy properties deeply between two objects.

NPM

Install

$ npm i copy-props

Usage

  • Load this module :

    const copyProps = require('copy-props');
    
  • Copy src to dst simply (and return dst) :

    var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc' };
    var dst = { a: 2, b: { b1: 'xxx', b2: 'yyy' } };
    
    copyProps(src, dst);
    // => { a: 1, b: { b1: 'bbb', b2: 'yyy' }, c: 'ccc' }
    
  • Copy src to dst with property mapping (and return dst) :

    var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc', d: 'ddd' };
    var dst = { f: { a: 2, b1: 'xxx', b2: 'yyy' }, e: 'zzz' };
    
    copyProps(src, dst, {
      a: 'f.a',
      'b.b1': 'f.b.b1',
      'b.b2': 'f.b.b2',
      'c': 'f.c',
    });
    // => { f: { a: 1, b1: 'bbb', b2: 'yyy', c: 'ccc' }, e: 'zzz' }
    
  • Copy src to dst with convert function (and return dst) :

    var src = { a: 1, b: { b1: 'bbb' } };
    var dst = { a: 0 };
    
    copyProps(src, dst, function(value, keyChain) {
      if (keyChain === 'a') {
        return value * 2;
      }
      if (keyChain === 'b.b1') {
        return value.toUpperCase();
      }
    });
    // => { a: 2, b: { b1: 'BBB' } }
    

API

copyProps(src, dst [, fromToProps] [, converter]) => object

Copy properties of src to dst deeply. If map is given, it is able to copy between different properties. If converter is given, it is able to convert the terminal values.

  • Arguments:

    • src [object] : a source object of copy.
    • dst [object] : a destinate object of copy.
    • fromToProps [object] : an object mapping properties between src and dst.
    • converter [function] : a function to convert terminal values in src.
  • Return [object] : dst object after copying.

Format of fromToProps

fromToProps is a non-nested key-value object. And the keys are property key chains of src and the values are property key chains of dst. The key chain is a string which is concatenated property keys on each level with dots, like 'aaa.bbb.ccc'.

The following example copys the value of src.aaa.bbb.ccc to dst.xxx.yyy.

copyProps(src, dst, {
  'aaa.bbb.ccc' : 'xxx.yyy'
})
API of converter

converter(value, keyChain) => any

converter is a function to convert terminal values of propeerties of src.

  • Arguments:

    • value [any] : a property value to be converted.
    • keyChain [string] : a string of property keys concatenated with dots.
  • Return: [any] : converted value.

License

MIT

Keywords

FAQs

Package last updated on 18 Sep 2016

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