Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including decoratable with all npm packages installed. Try it out:

var decoratable = require("decoratable")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

decoratable v0.0.2

define decoratable function. like connect middleware.

decoratable

define decoratable function. like connect middleware.

example

var decoratable = require('decoratable');

/**
 * define class.
 */
function SomeClass(values) {
  this._values = values;
}

/**
 * get value.
 */
SomeClass.prototype.get = decoratable(function(key) {
  return this._values[key];
});

/**
 * decorate default value.
 */
SomeClass.prototype.get.use(function(key, next) {
  var v = next(key);
  if ((key === 'created' || key === 'updated') && v === undefined) {
    return Date.now();
  }
  return v;
});

/**
 * instantiate.
 */
var ins = new SomeClass({ created: 1234567890 });
ins.get('created'); //=> 1234567890
ins.get('updated'); //=> Date.now() results.
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free