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

mixable-object

v0.1.1

Published

(Node.js) The way we do code reuse: simply copy things from an object to another.

Downloads

1,294

Readme

Mixable Object

Build Status Coverage Status

(Node.js) The way we do code reuse: simply copy things from an object to another.

Originally part of Carcass.

JavaScript is classified as a prototype-based scripting language...

It just works, without the magical inheritance blah blah.

How to use

We usually prepare the code to be reused into "proto"s.

var proto = {
    doSomething: function() {
        return 'done something';
    }
};

A typical gotcha: proto.something = 'something'; - it usually doesn't make sense to share a simple value (as opposed to a function).

Then there are many ways to reuse it. One of them is simply copy it to an object.

var mixable = require('mixable-object');
// A random object.
var obj = {};
// Make it mixable.
mixable(obj);
// Mixin.
obj.mixin(proto);
// Now it can be used.
// obj.doSomething().should.equal('done something');

However most of the time we want to reuse it with another prototype.

var mixable = require('mixable-object');
// A random class.
function Klass() {}
// It also makes the prototype mixable.
mixable(Klass);
// Mixin.
Klass.prototype.mixin(proto);
// Now it can be used.
// var ins = new Klass();
// ins.doSomething().should.equal('done something');

How it works

The code base is very simple, with just a few lines.

The mixable() function simply attaches a mixin() function to a given object (or if nothing was given, it builds a new object).

The mixin() function simply copies attributes from a source object to this.

Selective mixin: a 2nd or more arguments can be used, i.e. mixin(source, 'keyOne', 'keyTwo', ...) and if so, only the attributes with the keys are copied.

We've seen many similar implementations like Object.assign() from ES6 or another mixin() from es5-ext, while our mixin() has some notable features / differences:

  1. Only enumerable attributes are copied by default unless you do selective mixin (see above).
  2. Things are copied with Object.defineProperty(), so for example we will not copy the value that a getter returns but the getter itself.
  3. The argument of mixin() is the source and the target is this. This is different with many other libraries where they usually have 2 arguments - the target and the source. This allows it to be chained like obj.mixin(lorem).mixin(ipsum). However you can still do mixin.call(target, source) if you want to.

Merge

merge() is a variant of mixin(), can be used like:

var mixable = require('mixable-object');
var obj = {
    merge: mixable.merge
};
obj.merge(something);

Or:

mixable.merge.call(obj, something);

merge() does the same thing with mixin() except that if:

  1. both the source and the target has an attribute with the same name,
  2. and the descriptors of both the attributes are values (not getters),
  3. and both values are objects,

the merge() will copy the child attributes from the source, recursively, results in a deep merge.

Git summary

 project  : mixable-object
 repo age : 1 year, 1 month
 active   : 6 days
 commits  : 20
 files    : 21
 authors  :
    20  Makara Wang  100.0%