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

deco

v2.0.3

Published

Compose modular decorators to build constructors. Your Node.js code will never have been as organized, reusable, and pluggable.

Downloads

794

Readme

deco.js

NPM

Composable decorators for ES6. You're Node.js code will never have been as organized, reusable, and pluggable.

Summary

  • Provides class-like decorators and factories while avoiding problematic classical concepts like new, super, extends, and instanceof.
  • Compose objects, factory functions, ES5 constructors, and/or ES6 classes with one simple interface.
  • Write code in a style similar to partial classes.
  • Optionally, set default options for each class.
  • Easily create properties that use private data internally.

Usage Overview

The main functionality is provided by the Deco() function. It is used to compose decorators.

Decorators are defined when they are built. They are immutable.

const Deco = require('deco');

const Decorator = Deco({
  ƒ () { return 4 }
});

Decorators can decorate existing objects.

const existing = {
  g () { return 5 }
};

Decorator.call(existing);
expect(existing.ƒ()).to.equal(4);
expect(existing.g()).to.equal(5);

Decorators can be used as factories. Factories are functions that create objects but don't require using the new keyword.

const o = Decorator();
expect(o.ƒ()).to.equal(4);

Custom constructor logic can be supplied.

const Beer = Deco({
  constructor () {
    this.created = Date.now();
  }
});

const pint = Beer();
expect(pint.created).to.be.above(1469655052201);

Decorators can be composed into new decorators:

const Lager = Deco(Beer, {
  fermentation () { return 'bottom' }
});

const drink = Lager();
expect(drink.created).to.be.above(1469655052201);
expect(drink.fermentation()).to.equal('bottom');

Decorators execute the constructors of composed definitions sequentially.

const TastyBeer = Deco(Beer, {
  constructor (a) {
    this.a = a;
    this.tasted = this.created + 86400000;
  }
});

const tasty = TastyBeer(1);
expect(tasty.a).to.equal(1);
expect(tasty.created).to.be.above(1469655052201);
expect(tasty.tasted).to.be.above(1469741452201);

Partials / Loading from File

A directory of decorator files can be composed into a single decorator handily.

const Composed1 = Deco.loadFrom('./test/decorators');
expect(Composed1().flavor).to.equal('bitter');
expect(Composed1().type).to.equal('liqueur');

Specific files can be loaded from the directory in a specified order.

const Composed2 = Deco.loadFrom('./test/decorators', 'd2', 'd1');
expect(Composed2().flavor).to.equal('bitter');
expect(Composed2().type).to.equal('liqueur');

Use Deco.load instead of loadFrom to load definition files from the current directory.

Composition

You can have factories create objects of custom instances by passing in a constructor that returns an object.

const ErrorDecorator = Deco(Error);
expect(ErrorDecorator()).to.be.an.instanceof(Error);

// or...

const OtherDecorator = Deco(() => ({ a: 1 }));
const other = OtherDecorator();
expect(other.a).to.equal(1);

// and even...

const C = class {
  ƒ () { return 1 }
};

const CompatibleDecorator = Deco(C, {
  g () { return 2 }
});

const xyz = CompatibleDecorator();

expect(xyz).to.be.an.instanceof(C);
expect(xyz.ƒ()).to.equal(1);
expect(xyz.g()).to.equal(2);

// or compose several types of definitions in one go:

const AllAtOnce = Deco(C, function () { this.x = 'y' }, { z: 100 });
const all = AllAtOnce();
expect(all).to.be.an.instanceof(C);
expect(all.ƒ()).to.equal(1);
expect(all.x).to.equal('y');
expect(all.z).to.equal(100);

Defaults

Decorators can be associated with a defaults object. Here's an example using the defaults in a constructor.

const DecoratorWithDefaults = Deco({
  defaults: { a: 1, c: 4 }
});

const CheckDecoratorWithDefaults = Deco(DecoratorWithDefaults, {
  constructor (given) {
    const options = this.defaults(given);
    expect(options).to.equal({ a: 1, b: 2, c: 3 });
  }
});

CheckDecoratorWithDefaults({ b: 2, c: 3 });

Decorators have a defaults method which can be used to create a new immutable decorator by merging in new defaults.

const DecoratorNewDefaults = DecoratorWithDefaults.defaults({ a: 2, b: 2 });
const CheckDecoratorNewDefaults = Deco(DecoratorNewDefaults, {
  constructor () {
    expect(this.defaults()).to.equal({ a: 2, b: 2, c: 4 });
  }
});

CheckDecoratorNewDefaults();

Private Data

Deco provides an easy mechanism for creating properties backed with private data.

const hidden = Deco.hidden();

const DecoratorWithSecrets = Deco({
  get knox () {
    return hidden(this).knox
  },
  set knox (a) {
    hidden(this).knox = a + 1;
    return this.knox;
  }
});

const o1 = DecoratorWithSecrets();
expect(o1.knox).to.equal(undefined);
o1.knox = 1;
expect(o1.knox).to.equal(2);

Try deco in your project today!

Contact

© 2016-2024 Kun.io Labs