npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

subclassable-object-merger

v1.0.1

Published

An Object.assign() generalization with overridable rules

Downloads

8

Readme

workflow Jest coverage

Rationale

The public npm registry contains a plethora of modules implementing deep (recursive) alternatives to the standard Object.assign().

Some of them are basically single functions with fixed logic hardly suitable for any extending, for instance:

Others offer to set up sophisticated custom rules using some languages other than plain ECMAScript:

The author of this module needed something very close to the basic naive implementation, but easily customizable by the means of standard ES. This is why subclassable-object-merger was created.

Installation

npm install subclassable-object-merger

Usage

const {ObjectMerger} = require ('subclassable-object-merger')
const om = new ObjectMerger (
//{override: [
//  'scalar',
//  'array',
//  'object',
//]}
)

const t1 = {
	name: 'users',
	label: 'System users',
	columns: {
		id: {TYPE: 'int'},
	},
	data: [
		{id: 1, label: 'admin'}
	],
	triggers: null,
}

const t2 = {
	name: 'users',
	label: undefined,
	columns: {
		id: {AUTO_INCREMENT: true},
		label: {TYPE: 'text'},
	},
	pk: 'id',
	data: [
		{id: 2, label: 'employee'}
	],
	triggers: {before_insert: 'RETURN;'},
}

om.merge (t1, t2) /* result:
{
	name: 'users',
	label: 'System users',
	pk: 'id',
	columns: {
		id:    {TYPE: 'int', AUTO_INCREMENT: true},
		label: {TYPE: 'text'},
	},
	data: [
		{id: 1, label: 'admin'},
		{id: 2, label: 'employee'}
	],
	triggers: {before_insert: 'RETURN;'},
})
*/

Constructor

May be invoked without parameters or with a single {override} object.

The override option

When set, override must be an array of type names (see getType () below) to be forcibly overridden in case of conflict.

In particular, setting {override: ['scalar']} will lead to merge ({name: 'specific'}, {name: 'default'}) into {name: 'specific'} instead of throwing an error.

Internals

Instance properties

The only property is sum: the object mapping type names to corresponding type specific adding functions. Simply put:

this.sum = {
  array: (a, b) => a.concat (b),
  object: (a, b) => this.merge (a, b),			
  scalar: (a, b) => a === b ? a : throw Error ()
}

With the override option, a => a is set for types listed therein.

Methods

getType (a)

For a given non-null a, returns:

  • 'array' if Array.isArray (a)
  • 'object' if typeof a === 'object' (no advanced checker like is-plain-obj nor is-plain-object is in use here)
  • 'scalar' otherwise (e. g. for function valued a)

add (a, b, k)

Does the main job here: calls getType for a and b and if the results are the same, merges b into a; otherwise, throws an error.

When a or b is null or undefined, returns the other argument (which may be null or undefined too).

The k parameter is a name of the outer objects properties whose values are a and b. It doesn't affect the result, but may appear in error messages. Descendant classes may use this argument for some special needs.

merge (a, b)

This top level method copies b's content into a by calling add for each of b's keys. Both a and b must be plain Objects.

Events

ObjectMerger inherits from EventEmitter. Though never using this feature on its own, it may be used as a message box by containing processes.