co

copy-object

Copy, clone and merge, full or partially, javascript objects.

Showing:

Popularity

Downloads/wk

244

GitHub Stars

0

Maintenance

Last Commit

6yrs ago

Contributors

0

Package

Dependencies

0

Size (min+gzip)

0.3KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

copy-object Build Status


Copy, clone and merge, full or partially, javascript objects.

Installation

Easily via npm.

npm install copy-object

Usage

var result = copyObject(sourceObject[, destinationObject, keyList])

destinationObject is, if not provided, an empty Object keyList is an Array of keys to copy, if not provided, all keys will be copied.

Examples

Behavior

var copyObject = require('copy-object'),
    sourceObj = { a: 1, b: 2, c: 3 };

/**
 * Source object is not bitwise equally it's copy
 */
copyObject(sourceObj) === sourceObj
// false

/**
 * Previously defined objects will equal bitwise after copying it.
 */
var destObj = {};
copyObject(sourceObj, destObj) === destObj
// true

destObj
// { a: 1, b: 2, c: 3}

/**
 * Copies only selected keys.
 */
copyObject(sourceObj, {}, ['a'])
// { a: 1 }

/**
 * Copies only selected keys.
 */
var existingObject = { d: 4, e: 5 }
copyObject(sourceObj, existingObject)
// { a: 1, b: 2, c: 3, d: 4, e: 5 }

Some real life examples:

var EventEmitter = require('events').EventEmitter;

/**
 * Example class, inherits EventEmitter
 */
function MyClass() {
    this.a = 1;
    this.b = 2;
    this.c = 3;
    this._private = 4;
    this._someOther = 5;
}

// Inherit EventEmitter's prototype
MyClass.prototype = copyObject(EventEmitter.prototype, MyClass.prototype);

/**
 * Returns object with selected attributes.
 */
MyClass.prototype.toObject = function () {
    return copyObject(this, {}, ['a', 'b', 'c']);
};

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100