2 <a href="https://github.com/node-base/base">
3 <img height="250" width="250" src="https://raw.githubusercontent.com/node-base/base/master/docs/logo.png">
7 # base [![NPM version](https://img.shields.io/npm/v/base.svg?style=flat)](https://www.npmjs.com/package/base) [![NPM monthly downloads](https://img.shields.io/npm/dm/base.svg?style=flat)](https://npmjs.org/package/base) [![NPM total downloads](https://img.shields.io/npm/dt/base.svg?style=flat)](https://npmjs.org/package/base) [![Linux Build Status](https://img.shields.io/travis/node-base/base.svg?style=flat&label=Travis)](https://travis-ci.org/node-base/base)
9 > base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting with a handful of common methods, like `set`, `get`, `del` and `use`.
13 Install with [npm](https://www.npmjs.com/):
16 $ npm install --save base
21 Base is a framework for rapidly creating high quality node.js applications, using plugins like building blocks.
23 ### Guiding principles
25 The core team follows these principles to help guide API decisions:
27 * **Compact API surface**: The smaller the API surface, the easier the library will be to learn and use.
28 * **Easy to extend**: Implementors can use any npm package, and write plugins in pure JavaScript. If you're building complex apps, Base simplifies inheritance.
29 * **Easy to test**: No special setup should be required to unit test `Base` or base plugins
31 ### Minimal API surface
33 [The API](#api) was designed to provide only the minimum necessary functionality for creating a useful application, with or without [plugins](#plugins).
37 Base itself ships with only a handful of [useful methods](#api), such as:
39 * `.set`: for setting values on the instance
40 * `.get`: for getting values from the instance
41 * `.has`: to check if a property exists on the instance
42 * `.define`: for setting non-enumerable values on the instance
43 * `.use`: for adding plugins
47 When deciding on method to add or remove, we try to answer these questions:
49 1. Will all or most Base applications need this method?
50 2. Will this method encourage practices or enforce conventions that are beneficial to implementors?
51 3. Can or should this be done in a plugin instead?
57 It couldn't be easier to extend Base with any features or custom functionality you can think of.
59 Base plugins are just functions that take an instance of `Base`:
62 var base = new Base();
64 function plugin(base) {
65 // do plugin stuff, in pure JavaScript
73 Easily inherit Base using `.extend`:
76 var Base = require('base');
83 var app = new MyApp();
89 **Inherit or instantiate with a namespace**
91 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:
94 var Base = require('base');
95 // get and set values on the `base.cache` object
96 var base = Base.namespace('cache');
99 app.set('foo', 'bar');
100 console.log(app.cache.foo);
109 var Base = require('base');
110 var app = new Base();
111 app.set('foo', 'bar');
112 console.log(app.foo);
116 ### [Base](index.js#L44)
118 Create an instance of `Base` with the given `config` and `options`.
122 * `config` **{Object}**: If supplied, this object is passed to [cache-base](https://github.com/jonschlinkert/cache-base) to merge onto the the instance upon instantiation.
123 * `options` **{Object}**: If supplied, this object is used to initialize the `base.options` object.
128 // initialize with `config` and `options`
129 var app = new Base({isApp: true}, {abc: true});
130 app.set('foo', 'bar');
132 // values defined with the given `config` object will be on the root of the instance
133 console.log(app.baz); //=> undefined
134 console.log(app.foo); //=> 'bar'
136 console.log(app.get('isApp')); //=> true
137 console.log(app.get('foo')); //=> 'bar'
139 // values defined with the given `options` object will be on `app.options
140 console.log(app.options.abc); //=> true
143 ### [.is](index.js#L107)
145 Set the given `name` on `app._name` and `app.is*` properties. Used for doing lookups in plugins.
149 * `name` **{String}**
150 * `returns` **{Boolean}**
156 console.log(app._name);
158 console.log(app.isFoo);
161 console.log(app.isFoo);
163 console.log(app.isBar);
165 console.log(app._name);
169 ### [.isRegistered](index.js#L145)
171 Returns true if a plugin has already been registered on an instance.
173 Plugin implementors are encouraged to use this first thing in a plugin
174 to prevent the plugin from being called more than once on the same
179 * `name` **{String}**: The plugin name.
180 * `register` **{Boolean}**: If the plugin if not already registered, to record it as being registered pass `true` as the second argument.
181 * `returns` **{Boolean}**: Returns true if a plugin is already registered.
185 * `emits`: `plugin` Emits the name of the plugin being registered. Useful for unit tests, to ensure plugins are only registered once.
190 var base = new Base();
191 base.use(function(app) {
192 if (app.isRegistered('myPlugin')) return;
196 // to also record the plugin as being registered
197 base.use(function(app) {
198 if (app.isRegistered('myPlugin', true)) return;
203 ### [.use](index.js#L175)
205 Define a plugin function to be called immediately upon init. Plugins are chainable and expose the following arguments to the plugin function:
207 * `app`: the current instance of `Base`
208 * `base`: the [first ancestor instance](#base) of `Base`
212 * `fn` **{Function}**: plugin function to call
213 * `returns` **{Object}**: Returns the item instance for chaining.
224 ### [.define](index.js#L197)
226 The `.define` method is used for adding non-enumerable property on the instance. Dot-notation is **not supported** with `define`.
230 * `key` **{String}**: The name of the property to define.
232 * `returns` **{Object}**: Returns the instance for chaining.
237 // arbitrary `render` function using lodash `template`
238 app.define('render', function(str, locals) {
239 return _.template(str)(locals);
243 ### [.mixin](index.js#L222)
245 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.
250 * `val` **{Object|Array}**
251 * `returns` **{Object}**: Returns the `base` instance for chaining.
256 app.mixin('foo', function() {
261 ### [.base](index.js#L268)
263 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.
268 // create an instance of `Base`, this is our first ("base") instance
269 var first = new Base();
270 first.foo = 'bar'; // arbitrary property, to make it easier to see what's happening later
272 // create another instance
273 var second = new Base();
274 // create a reference to the first instance (`first`)
275 second.parent = first;
277 // create another instance
278 var third = new Base();
279 // create a reference to the previous instance (`second`)
280 // repeat this pattern every time a "child" instance is created
281 third.parent = second;
283 // we can always access the first instance using the `base` property
284 console.log(first.base.foo);
286 console.log(second.base.foo);
288 console.log(third.base.foo);
290 // and now you know how to get to third base ;)
293 ### [#use](index.js#L293)
295 Static method for adding global plugin functions that will be added to an instance when created.
299 * `fn` **{Function}**: Plugin function to use on each instance.
300 * `returns` **{Object}**: Returns the `Base` constructor for chaining
305 Base.use(function(app) {
308 var app = new Base();
309 console.log(app.foo);
313 ### [#extend](index.js#L337)
315 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](https://github.com/jonschlinkert/static-extend) for more details.
319 * `Ctor` **{Function}**: constructor to extend
320 * `methods` **{Object}**: Optional prototype properties to mix in.
321 * `returns` **{Object}**: Returns the `Base` constructor for chaining
326 var extend = cu.extend(Parent);
327 Parent.extend(Child);
330 Parent.extend(Child, {
336 ### [#mixin](index.js#L379)
338 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)`).
342 * `fn` **{Function}**: Function to call
343 * `returns` **{Object}**: Returns the `Base` constructor for chaining
348 Base.mixin(function(proto) {
349 proto.foo = function(msg) {
355 ### [#mixins](index.js#L401)
357 Static method for running global mixin functions against a child constructor. Mixins must be registered before calling this method.
361 * `Child` **{Function}**: Constructor function of a child class
362 * `returns` **{Object}**: Returns the `Base` constructor for chaining
371 ### [#inherit](index.js#L420)
373 Similar to `util.inherit`, but copies all static properties, prototype properties, and getters/setters from `Provider` to `Receiver`. See [class-utils](https://github.com/jonschlinkert/class-utils#inherit) for more details.
377 * `Receiver` **{Function}**: Receiving (child) constructor
378 * `Provider` **{Function}**: Providing (parent) constructor
379 * `returns` **{Object}**: Returns the `Base` constructor for chaining
384 Base.inherit(Foo, Bar);
389 The following node.js applications were built with `Base`:
391 * [assemble](https://github.com/assemble/assemble)
392 * [verb](https://github.com/verbose/verb)
393 * [generate](https://github.com/generate/generate)
394 * [scaffold](https://github.com/jonschlinkert/scaffold)
395 * [boilerplate](https://github.com/jonschlinkert/boilerplate)
400 Statements : 98.91% ( 91/92 )
401 Branches : 92.86% ( 26/28 )
402 Functions : 100% ( 17/17 )
403 Lines : 98.9% ( 90/91 )
410 * fixes https://github.com/micromatch/micromatch/issues/99
416 * Static `.use` and `.run` methods are now non-enumerable
422 * `.is` no longer takes a function, a string must be passed
423 * all remaining `.debug` code has been removed
424 * `app._namespace` was removed (related to `debug`)
425 * `.plugin`, `.use`, and `.define` no longer emit events
426 * `.assertPlugin` was removed
427 * `.lazy` was removed
433 * [base-cwd](https://www.npmjs.com/package/base-cwd): Base plugin that adds a getter/setter for the current working directory. | [homepage](https://github.com/node-base/base-cwd "Base plugin that adds a getter/setter for the current working directory.")
434 * [base-data](https://www.npmjs.com/package/base-data): adds a `data` method to base-methods. | [homepage](https://github.com/node-base/base-data "adds a `data` method to base-methods.")
435 * [base-fs](https://www.npmjs.com/package/base-fs): base-methods plugin that adds vinyl-fs methods to your 'base' application for working with the file… [more](https://github.com/node-base/base-fs) | [homepage](https://github.com/node-base/base-fs "base-methods plugin that adds vinyl-fs methods to your 'base' application for working with the file system, like src, dest, copy and symlink.")
436 * [base-generators](https://www.npmjs.com/package/base-generators): Adds project-generator support to your `base` application. | [homepage](https://github.com/node-base/base-generators "Adds project-generator support to your `base` application.")
437 * [base-option](https://www.npmjs.com/package/base-option): Adds a few options methods to base, like `option`, `enable` and `disable`. See the readme… [more](https://github.com/node-base/base-option) | [homepage](https://github.com/node-base/base-option "Adds a few options methods to base, like `option`, `enable` and `disable`. See the readme for the full API.")
438 * [base-pipeline](https://www.npmjs.com/package/base-pipeline): base-methods plugin that adds pipeline and plugin methods for dynamically composing streaming plugin pipelines. | [homepage](https://github.com/node-base/base-pipeline "base-methods plugin that adds pipeline and plugin methods for dynamically composing streaming plugin pipelines.")
439 * [base-pkg](https://www.npmjs.com/package/base-pkg): Plugin for adding a `pkg` method that exposes pkg-store to your base application. | [homepage](https://github.com/node-base/base-pkg "Plugin for adding a `pkg` method that exposes pkg-store to your base application.")
440 * [base-plugins](https://www.npmjs.com/package/base-plugins): Adds 'smart plugin' support to your base application. | [homepage](https://github.com/node-base/base-plugins "Adds 'smart plugin' support to your base application.")
441 * [base-questions](https://www.npmjs.com/package/base-questions): Plugin for base-methods that adds methods for prompting the user and storing the answers on… [more](https://github.com/node-base/base-questions) | [homepage](https://github.com/node-base/base-questions "Plugin for base-methods that adds methods for prompting the user and storing the answers on a project-by-project basis.")
442 * [base-store](https://www.npmjs.com/package/base-store): Plugin for getting and persisting config values with your base-methods application. Adds a 'store' object… [more](https://github.com/node-base/base-store) | [homepage](https://github.com/node-base/base-store "Plugin for getting and persisting config values with your base-methods application. Adds a 'store' object that exposes all of the methods from the data-store library. Also now supports sub-stores!")
443 * [base-task](https://www.npmjs.com/package/base-task): base plugin that provides a very thin wrapper around [https://github.com/doowb/composer](https://github.com/doowb/composer) for adding task methods to… [more](https://github.com/node-base/base-task) | [homepage](https://github.com/node-base/base-task "base plugin that provides a very thin wrapper around <https://github.com/doowb/composer> for adding task methods to your application.")
447 Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
451 | **Commits** | **Contributor** |
453 | 141 | [jonschlinkert](https://github.com/jonschlinkert) |
454 | 30 | [doowb](https://github.com/doowb) |
455 | 3 | [charlike](https://github.com/charlike) |
456 | 1 | [criticalmash](https://github.com/criticalmash) |
457 | 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
461 _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
463 To generate the readme, run the following command:
466 $ npm install -g verbose/verb#dev verb-generate-readme && verb
471 Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
474 $ npm install && npm test
481 * [github/jonschlinkert](https://github.com/jonschlinkert)
482 * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
486 Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
487 Released under the [MIT License](LICENSE).
491 _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 07, 2017._