Socket
Socket
Sign inDemoInstall

merge-descriptors

Package Overview
Dependencies
0
Maintainers
6
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    merge-descriptors

Merge objects using their property descriptors


Version published
Weekly downloads
27M
decreased by-1.3%
Maintainers
6
Install size
4.27 kB
Created
Weekly downloads
 

Package description

What is merge-descriptors?

The merge-descriptors npm package is a utility that allows you to copy enumerable properties from one or more source objects to a destination object, optionally defining property descriptors. It is commonly used to mix properties and their descriptors from one object into another, preserving more detailed configuration than a simple object spread or Object.assign.

What are merge-descriptors's main functionalities?

Merge properties with descriptors

This feature allows you to merge properties from the source object to the target object, including their getters and setters. The example shows how a property with a getter is copied to the target object, preserving its behavior.

const mergeDescriptors = require('merge-descriptors');
let target = {};
let source = { get foo() { return 'bar'; } };
mergeDescriptors(target, source);
console.log(target.foo); // Outputs: 'bar'

Merge properties without overwriting

This feature demonstrates how to merge properties without overwriting existing properties in the target object. The example merges a getter for 'foo' from the source into the target, but since 'foo' already exists in the target and the overwrite flag is set to false, the original value is preserved.

const mergeDescriptors = require('merge-descriptors');
let target = { foo: 'initial' };
let source = { get foo() { return 'bar'; } };
mergeDescriptors(target, source, false);
console.log(target.foo); // Outputs: 'initial'

Other packages similar to merge-descriptors

Readme

Source

merge-descriptors

Merge objects using their property descriptors

Install

npm install merge-descriptors

Usage

import mergeDescriptors from 'merge-descriptors';

const thing = {
	get name() {
		return 'John'
	}
}

const animal = {};

mergeDescriptors(animal, thing);

console.log(animal.name);
//=> 'John'

API

merge(destination, source, overwrite?)

Merges "own" properties from a source to a destination object, including non-enumerable and accessor-defined properties. It retains original values and descriptors, ensuring the destination receives a complete and accurate copy of the source's properties.

Returns the modified destination object.

destination

Type: object

The object to receive properties.

source

Type: object

The object providing properties.

overwrite

Type: boolean
Default: true

A boolean to control overwriting of existing properties.

Keywords

FAQs

Last updated on 16 Nov 2023

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