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

extenderball

v0.0.3

Published

Extenderball: Extendable JS objects

Downloads

15

Readme

Extenderball

Very flexible extendable objects.

The idea is that object properties and methods are defined in "object fragments", where each fragment has its own file. The fragments can then be combined into one object as desired. That object can then be used to create as many instances as required.

Goals:

  • Allow creation of small, self-contained bits of code
  • Reuse generic fragments in multiple "big" objects
  • Replace arbitrary amounts of code depending on current configuration

I created this system as the basis for an online shopping platform. The platform is supposed to work in very different ways depending on the current setup. I wanted to be able to replace parts of the application logic depending on the project's configuration, the current user's settings and any other relevant factors. With Extenderball I can effectively replace single functions in an object.

Note that Extenderball does not contain any logic for managing sets of object fragments. This is not part of this module's scope (yet).

Example

A very simple example for an object fragment (let's say it's in the file objects/example/fragment.js):

var ExampleFragment = function(eb, obj) {
	obj.prototype.exampleProperty = 'Lazy dog';
	obj.prototype.exampleMethod = function() {
		return this.exampleProperty;
	};
};
module.exports = ExampleFragment;

This fragment sets a property and adds a method. To use this fragment:

var EB = require('extenderball');
var MyObject = new EB(['objects/fragment']);
var myInstance = new MyObject();
// Use the instance
console.log(myInstance.exampleMethod());

This is a lot of work for a single fragment. Let's create another fragment objects/another:

var AnotherFragment = function(eb, obj) {
	obj.prototype.anotherProperty = 'Crazy fox';
	obj.prototype.anotherMethod = function() {
		return this.exampleMethod()+', '+this.anotherProperty;
	};
};
module.exports = AnotherFragment;

And then we extend the main code like this:

var EB = require('extenderball');
var MyObject = new EB([
	'objects/fragment',
	'objects/another'
]);
var myInstance = new MyObject();
// Use the instance
console.log(myInstance.exampleMethod());
console.log(myInstance.anotherMethod());

The two object fragments were essentially merged into a single object. This can be done with any number of fragments.

This and other examples can be found in the examples directory.

Usage

Install it via npm install extenderball or download and drop it in the node_modules folder.

Then include it in your project as usual:

var Extenderball = require('extenderball');

Create, extendand and instantiate objects

The Extenderball constructor takes an array as its single parameter. This array contains paths to as many fragments as desired:

var MyObject = new Extenderball([
	'objects/general/logging',
	'objects/products/default',
	'objects/products/computer'
]);

An object can be extended with additional fragments afterwards:

MyObject.extend('objects/products/laptop');

Creating an object instance is done as usual:

var myInstance = new MyObject();

Write Object Fragments

TODO: Must still be documented

  • How to write an object fragment
  • The init method

TODO

  • Check that a fragment is only loaded once
  • Error catching
  • Manage sets of fragments
  • Ability to access "parent" methods when overwriting a method.