base NPM version NPM downloads Build Status

What is Base?

Base is a framework for rapidly creating high quality node.js applications, using plugins like building blocks.

Guiding principles

The core team follows these principles to help guide API decisions:

Minimal API surface

The API was designed to provide only the minimum necessary functionality for creating a useful application, with or without plugins.

Base core

Base itself ships with only a handful of useful methods, such as:

Be generic

When deciding on method to add or remove, we try to answer these questions:

  1. Will all or most Base applications need this method?
  2. Will this method encourage practices or enforce conventions that are beneficial to implementors?
  3. Can or should this be done in a plugin instead?

Composability

Plugin system

It couldn't be easier to extend Base with any features or custom functionality you can think of.

Base plugins are just functions that take an instance of Base:

var base = new Base();

function plugin(base) {
  // do plugin stuff, in pure JavaScript
}
// use the plugin
base.use(plugin);

Inheritance

Easily inherit Base using .extend:

var Base = require('base');

function MyApp() {
  Base.call(this);
}
Base.extend(MyApp);

var app = new MyApp();
app.set('a', 'b');
app.get('a');
//=> 'b';

Inherit or instantiate with a namespace

By default, the .get, .set and .has methods set and get values from the root of the base instance. You can customize this using the .namespace method exposed on the exported function. For example:

var Base = require('base');
// get and set values on the `base.cache` object
var base = Base.namespace('cache');

var app = base();
app.set('foo', 'bar');
console.log(app.cache.foo);
//=> 'bar'

Install

Install with npm:

$ npm install --save base

API

Usage

var Base = require('base');
var app = new Base();
app.set('foo', 'bar');
console.log(app.foo);
//=> 'bar'

Base

Create an instance of Base with the given config and options.

Params

Example

// initialize with `config` and `options`
var app = new Base({isApp: true}, {abc: true});
app.set('foo', 'bar');

// values defined with the given `config` object will be on the root of the instance
console.log(app.baz); //=> undefined
console.log(app.foo); //=> 'bar'
// or use `.get`
console.log(app.get('isApp')); //=> true
console.log(app.get('foo')); //=> 'bar'

// values defined with the given `options` object will be on `app.options
console.log(app.options.abc); //=> true

.is

Set the given name on app._name and app.is* properties. Used for doing lookups in plugins.

Params

Example

app.is('foo');
console.log(app._name);
//=> 'foo'
console.log(app.isFoo);
//=> true
app.is('bar');
console.log(app.isFoo);
//=> true
console.log(app.isBar);
//=> true
console.log(app._name);
//=> 'bar'

.isRegistered

Returns true if a plugin has already been registered on an instance.

Plugin implementors are encouraged to use this first thing in a plugin to prevent the plugin from being called more than once on the same instance.

Params

Events

Example

var base = new Base();
base.use(function(app) {
  if (app.isRegistered('myPlugin')) return;
  // do stuff to `app`
});

// to also record the plugin as being registered
base.use(function(app) {
  if (app.isRegistered('myPlugin', true)) return;
  // do stuff to `app`
});

.use

Define a plugin function to be called immediately upon init. Plugins are chainable and expose the following arguments to the plugin function:

Params

Example

var app = new Base()
  .use(foo)
  .use(bar)
  .use(baz)

.define

The .define method is used for adding non-enumerable property on the instance. Dot-notation is not supported with define.

Params

Example

// arbitrary `render` function using lodash `template`
app.define('render', function(str, locals) {
  return _.template(str)(locals);
});

.mixin

Mix property key onto the Base prototype. If base is inherited using Base.extend this method will be overridden by a new mixin method that will only add properties to the prototype of the inheriting application.

Params

Example

app.mixin('foo', function() {
  // do stuff
});

.base

Getter/setter used when creating nested instances of Base, for storing a reference to the first ancestor instance. This works by setting an instance of Base on the parent property of a "child" instance. The base property defaults to the current instance if no parent property is defined.

Example

// create an instance of `Base`, this is our first ("base") instance
var first = new Base();
first.foo = 'bar'; // arbitrary property, to make it easier to see what's happening later

// create another instance
var second = new Base();
// create a reference to the first instance (`first`)
second.parent = first;

// create another instance
var third = new Base();
// create a reference to the previous instance (`second`)
// repeat this pattern every time a "child" instance is created
third.parent = second;

// we can always access the first instance using the `base` property
console.log(first.base.foo);
//=> 'bar'
console.log(second.base.foo);
//=> 'bar'
console.log(third.base.foo);
//=> 'bar'
// and now you know how to get to third base ;)

#use

Static method for adding global plugin functions that will be added to an instance when created.

Params

Example

Base.use(function(app) {
  app.foo = 'bar';
});
var app = new Base();
console.log(app.foo);
//=> 'bar'

#extend

Static method for inheriting the prototype and static methods of the Base class. This method greatly simplifies the process of creating inheritance-based applications. See static-extend for more details.

Params

Example

var extend = cu.extend(Parent);
Parent.extend(Child);

// optional methods
Parent.extend(Child, {
  foo: function() {},
  bar: function() {}
});

#mixin

Used for adding methods to the Base prototype, and/or to the prototype of child instances. When a mixin function returns a function, the returned function is pushed onto the .mixins array, making it available to be used on inheriting classes whenever Base.mixins() is called (e.g. Base.mixins(Child)).

Params

Example

Base.mixin(function(proto) {
  proto.foo = function(msg) {
    return 'foo ' + msg;
  };
});

#mixins

Static method for running global mixin functions against a child constructor. Mixins must be registered before calling this method.

Params

Example

Base.extend(Child);
Base.mixins(Child);

#inherit

Similar to util.inherit, but copies all static properties, prototype properties, and getters/setters from Provider to Receiver. See class-utils for more details.

Params

Example

Base.inherit(Foo, Bar);

In the wild

The following node.js applications were built with Base:

Test coverage

Statements   : 98.95% ( 94/95 )
Branches     : 92.31% ( 24/26 )
Functions    : 100% ( 17/17 )
Lines        : 98.94% ( 93/94 )

History

v0.11.0 - major breaking changes!

v0.9.0 - major breaking changes!

Related projects

There are a number of different plugins available for extending base. Let us know if you create your own!

Contributing

This document was generated by verb-readme-generator (a verb generator), please don't edit directly. Any changes to the readme must be made in .verb.md. See Building Docs.

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Or visit the verb-readme-generator project to submit bug reports or pull requests for the readme layout template.

Building docs

(This document was generated by verb-readme-generator (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)

Generate readme and API documentation with verb:

$ npm install -g verb verb-readme-generator && verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2016, Jon Schlinkert. Released under the MIT license.


This file was generated by verb, v0.9.0, on June 23, 2016.