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

extendable-instantiation

v1.0.1

Published

Extend objects in a simple, reliable way

Downloads

5

Readme

extendable-instantiation

Extend objects in a simple, reliable way

Installation

$ npm i -s extendable-instantiation

Get started

Demonstration

This is a simple demonstrations of the default configurations:

class X {

	static get DEFAULT_OPTIONS() {
		return {
			optionOne: "one",
			optionTwo: "two",
			optionThree: "three",
		};
	}

	static get DEFAULT_OPTIONS__SETTINGS() {
		return {
			configurationOne: "one",
			configurationTwo: "two",
			configurationThree: "three"
		};
	}

	static get DEFAULT_OPTIONS__SETTINGS__PREFERENCES() {
		return {
			preference1: "one",
			preference2: "two",
			preference3: "three",
		}
	}

	constructor(options = {}) {
		require(__dirname + "/../index.js")(this, this.constructor, options);
	}

}

const x = new X({
	options: {
		optionOne: "one hundred",
		settings: {
			configurationOne: "one thousand"
		}
	}
});

expect(x).to.deep.equal({
	options: {
		optionOne: "one hundred",
		optionTwo: "two",
		optionThree: "three",
		settings: {
			configurationOne: "one thousand",
			configurationTwo: "two",
			configurationThree: "three",
			preferences: {
				preference1: "one",
				preference2: "two",
				preference3: "three",
			}
		}
	}
});

Explanation

We pass 3 objects to the function:

  • base:Object: the object to redefine.
  • default:Object: the object from which we pick default values.
  • options:Object: the object from which we pick the optional values.

Moreover, we have 2 parameters more to pass, optionally:

  • mapper:Object: object whose keys are properties of the default object, and whose values are properties of the options object.
  • passDefaults:Boolean: whether or not to apply the default algorythm to map default and options. Defaults to true.

The default mapper algorythm will pick the properties from default that start with DEFAULT_, and map them to the same property in options, but passed as camelCase instead. Also, the __ can be used as array accessor to the next-level value, allowing property nesting just by the names of the variables in the default object.

This is how we get the demonstration work: with the default algorythm under the hood, doing some stuff for us.

Customization

The mapper and the passDefaults parameters can be used to customize the way we mix defaults and options objects.

Basically, the mapper is an object whose keys are picked from the defaults, and whose values are picked from the options objects.

Said this, one can nest access by the splitter token, which by default is /.

{
	"CUSTOM_OPTIONS": "options/custom",
	"CUSTOM_OPTIONS/specialProperty": "options/custom/specialProperty",
}

The idea

Extending objects is a powerful mechanism, but the way of doing it can result controversial.

This is just a flexible proposal which can be very adaptive, and saves a lot of time and repetitive code.

API

Instantiation(this:Object, defaults:Object, options:Object, mapper:Object|Boolean, passDefault:Boolean)

Name: Instantiation

Type: Function

Parameter:

  • this:Object. Required. Object to be extended or fixed.

  • defaults:Object. Required. Object that contains default values (among others).

  • options:Object. Required. Object that contains optional values (among others).

  • mapper:Object|Boolean. Optional. Object that explains how to map default and optional values.

  • passDefault:Boolean. Optional. Whether or not to extend the basic object with the default algorythm of extension. Default: true.

Throws:

  • Error when a property in mapper is not found, either in defaults (on keys) or in options (on values)

  • Error when a property in mapper values could not be set to this.

Returns: this:Object. The updated object.

Description:

Instantiation.setProperty(base:Object, selector:String, value:Any, splitter:String)

Name: Instantiation.setProperty

Type: Function

Parameter:

  • base:Object. Required. Object to alter.

  • selector:String. Required. Property to alter. Property splitter determines the splitter token.

  • value:Any. Required. Value to set.

  • splitter:String. Optional. Token to split the selector into parts. By default: /

Throws:

  • Error when a property should be accessible as object, and it is not.

  • Error when a property should exist in object, and it does not.

Returns: undefined

Description: Sets the property selected (by selector) to the value passed (by value) in base object (base). The selector can be customized (by splitter).

Instantiation.getProperty(base:Object, selector:String, defaultValue:Any, splitter:String)

Name: Instantiation.getProperty

Type: Function

Parameter:

  • base:Object. Required. Object from which to get the data.

  • selector:String. Required. Property to get.

  • defaultValue:Any. Required. Default value, when a property is not found.

  • splitter:String. Optional. Token to split the selector into parts. By default: /

Throws:

  • Error when a property should be accessible as object, and it is not.

Returns: selection:Any

Description: Gets the property selected (by selector) from the base object (base) or the value passed (by defaultValue). The selector can be customized (by splitter).

License

This project is under WTFPL or What The Fuck Public License, which means do whatever you want with it.

Issues

Plese, address your issues and suggestions at the Issues form of the Github project.