Socket
Socket
Sign inDemoInstall

copy-descriptor

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    copy-descriptor

Copy a descriptor from object A to object B


Version published
Weekly downloads
12M
decreased by-1.68%
Maintainers
1
Install size
7.90 kB
Created
Weekly downloads
 

Package description

What is copy-descriptor?

The copy-descriptor npm package is designed for copying property descriptors from one object to another. It allows for precise control over how properties are copied, including their enumerability, configurability, writability, and whether they are getters/setters. This can be particularly useful when you want to replicate the behavior of an object's properties without altering the original object.

What are copy-descriptor's main functionalities?

Copying a single property descriptor

This feature allows you to copy a single property descriptor from the source object to the target object. The code sample demonstrates copying the getter for the 'foo' property from the source object to the target object, then logging the descriptor of the 'foo' property on the target to show that it has been copied.

const copyDescriptor = require('copy-descriptor');
let target = {};
let source = { get foo() { return 'bar'; } };
copyDescriptor(target, source, 'foo');
console.log(Object.getOwnPropertyDescriptor(target, 'foo'));

Copying multiple property descriptors

This feature enables the copying of multiple property descriptors from the source object to the target object in a single call. The code sample shows how to copy both the 'foo' getter and the 'bar' value property from the source to the target, then logs the descriptors to demonstrate that both properties have been successfully copied.

const copyDescriptor = require('copy-descriptor');
let target = {};
let source = { get foo() { return 'bar'; }, bar: 'baz' };
copyDescriptor(target, source, ['foo', 'bar']);
console.log(Object.getOwnPropertyDescriptor(target, 'foo'));
console.log(Object.getOwnPropertyDescriptor(target, 'bar'));

Other packages similar to copy-descriptor

Readme

Source

copy-descriptor NPM version Build Status

Copy a descriptor from object A to object B

(TOC generated by verb using markdown-toc)

Install

Install with npm:

$ npm i copy-descriptor --save

Usage

var copy = require('copy-descriptor');

API

copy

Copy a descriptor from one object to another.

Params

  • receiver {Object}: The target object
  • provider {Object}: The provider object
  • from {String}: The key to copy on provider.
  • to {String}: Optionally specify a new key name to use.
  • returns {Object}

Example

function App() {
  this.cache = {};
}
App.prototype.set = function(key, val) {
  this.cache[key] = val;
  return this;
};
Object.defineProperty(App.prototype, 'count', {
  get: function() {
    return Object.keys(this.cache).length;
  }
});

copy(App.prototype, 'count', 'len');

// create an instance
var app = new App();

app.set('a', true);
app.set('b', true);
app.set('c', true);

console.log(app.count);
//=> 3
console.log(app.len);
//=> 3

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Author

Jon Schlinkert

License

Copyright © 2015 Jon Schlinkert Released under the MIT license.


This file was generated by verb on December 28, 2015.

Keywords

FAQs

Last updated on 28 Dec 2015

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