base-methods
Advanced tools
Comparing version 0.2.6 to 0.2.7
313
index.js
'use strict'; | ||
var util = require('util'); | ||
var Emitter = require('component-emitter'); | ||
var cu = require('class-utils'); | ||
var utils = require('./utils'); | ||
function base(name) { | ||
var Emitter = require('component-emitter'); | ||
var cu = require('class-utils'); | ||
var utils = require('./utils'); | ||
/** | ||
* Create an instance of `Base` with optional `options`. | ||
* | ||
* ```js | ||
* var app = new Base(); | ||
* app.set('foo', 'bar'); | ||
* console.log(app.get('foo')); | ||
* //=> 'bar' | ||
* ``` | ||
* | ||
* @param {Object} `options` | ||
* @api public | ||
*/ | ||
function Base(options) { | ||
if (!(this instanceof Base)) { | ||
return new Base(options); | ||
} | ||
Emitter.call(this); | ||
this.define('_callbacks', this._callbacks); | ||
if (typeof options === 'object') { | ||
this.visit('set', options); | ||
} | ||
} | ||
Base.prototype = Emitter({ | ||
constructor: Base, | ||
/** | ||
* Assign `value` to `key`. Also emits `set` with | ||
* the key and value. | ||
* Create an instance of `Base` with optional `options`. | ||
* | ||
* ```js | ||
* app.on('set', function(key, val) { | ||
* // do something when `set` is emitted | ||
* }); | ||
* | ||
* app.set(key, value); | ||
* | ||
* // also takes an object or array | ||
* app.set({name: 'Halle'}); | ||
* app.set([{foo: 'bar'}, {baz: 'quux'}]); | ||
* console.log(app); | ||
* //=> {name: 'Halle', foo: 'bar', baz: 'quux'} | ||
* var app = new Base(); | ||
* app.set('foo', 'bar'); | ||
* console.log(app.get('foo')); | ||
* //=> 'bar' | ||
* ``` | ||
* | ||
* @name .set | ||
* @param {String} `key` | ||
* @param {*} `value` | ||
* @return {Object} Returns the instance for chaining. | ||
* @param {Object} `options` | ||
* @api public | ||
*/ | ||
set: function (key, val) { | ||
if (typeof key === 'object') { | ||
this.visit('set', key); | ||
} else { | ||
utils.set(this, key, val); | ||
this.emit('set', key, val); | ||
function Base(options) { | ||
if (!(this instanceof Base)) { | ||
return new Base(options); | ||
} | ||
return this; | ||
}, | ||
Emitter.call(this); | ||
if (name) this[name] = {}; | ||
this.define('_callbacks', this._callbacks); | ||
if (typeof options === 'object') { | ||
this.visit('set', options); | ||
} | ||
} | ||
/** | ||
* Return the stored value of `key`. Dot notation may be used | ||
* to get [nested property values][get-value]. | ||
* | ||
* ```js | ||
* app.set('foo', 'bar'); | ||
* app.get('foo'); | ||
* // => "bar" | ||
* ``` | ||
* | ||
* @name .get | ||
* @param {*} `key` | ||
* @param {Boolean} `escape` | ||
* @return {*} | ||
* @api public | ||
*/ | ||
get: function (key) { | ||
return utils.get(this, key); | ||
}, | ||
Base.prototype = Emitter({ | ||
constructor: Base, | ||
/** | ||
* Delete `key` from the instance. Also emits `del` with | ||
* the key of the deleted item. | ||
* | ||
* ```js | ||
* app.del(); // delete all | ||
* // or | ||
* app.del('foo'); | ||
* // or | ||
* app.del(['foo', 'bar']); | ||
* ``` | ||
* @name .del | ||
* @param {String} `key` | ||
* @return {Object} Returns the instance for chaining. | ||
* @api public | ||
*/ | ||
/** | ||
* Assign `value` to `key`. Also emits `set` with | ||
* the key and value. | ||
* | ||
* ```js | ||
* app.on('set', function(key, val) { | ||
* // do something when `set` is emitted | ||
* }); | ||
* | ||
* app.set(key, value); | ||
* | ||
* // also takes an object or array | ||
* app.set({name: 'Halle'}); | ||
* app.set([{foo: 'bar'}, {baz: 'quux'}]); | ||
* console.log(app); | ||
* //=> {name: 'Halle', foo: 'bar', baz: 'quux'} | ||
* ``` | ||
* | ||
* @name .set | ||
* @param {String} `key` | ||
* @param {*} `value` | ||
* @return {Object} Returns the instance for chaining. | ||
* @api public | ||
*/ | ||
del: function (key) { | ||
if (Array.isArray(key)) { | ||
this.visit('del', key); | ||
} else { | ||
utils.del(this, key); | ||
this.emit('del', key); | ||
set: function (key, val) { | ||
if (typeof key === 'object') { | ||
this.visit('set', key); | ||
} else { | ||
if (name) { | ||
utils.set(this[name], key, val); | ||
} else { | ||
utils.set(this, key, val); | ||
} | ||
this.emit('set', key, val); | ||
} | ||
return this; | ||
}, | ||
/** | ||
* Return the stored value of `key`. Dot notation may be used | ||
* to get [nested property values][get-value]. | ||
* | ||
* ```js | ||
* app.set('foo', 'bar'); | ||
* app.get('foo'); | ||
* // => "bar" | ||
* ``` | ||
* | ||
* @name .get | ||
* @param {*} `key` | ||
* @param {Boolean} `escape` | ||
* @return {*} | ||
* @api public | ||
*/ | ||
get: function (key) { | ||
if (name) { | ||
return utils.get(this[name], key); | ||
} | ||
return utils.get(this, key); | ||
}, | ||
/** | ||
* Delete `key` from the instance. Also emits `del` with | ||
* the key of the deleted item. | ||
* | ||
* ```js | ||
* app.del(); // delete all | ||
* // or | ||
* app.del('foo'); | ||
* // or | ||
* app.del(['foo', 'bar']); | ||
* ``` | ||
* @name .del | ||
* @param {String} `key` | ||
* @return {Object} Returns the instance for chaining. | ||
* @api public | ||
*/ | ||
del: function (key) { | ||
if (Array.isArray(key)) { | ||
this.visit('del', key); | ||
} else { | ||
if (name) { | ||
utils.del(this[name], key); | ||
} else { | ||
utils.del(this, key); | ||
} | ||
this.emit('del', key); | ||
} | ||
return this; | ||
}, | ||
/** | ||
* Define a non-enumerable property on the instance. | ||
* | ||
* ```js | ||
* // arbitrary `render` function using lodash `template` | ||
* define('render', function(str, locals) { | ||
* return _.template(str)(locals); | ||
* }); | ||
* ``` | ||
* @name .define | ||
* @param {String} `key` | ||
* @param {any} `value` | ||
* @return {Object} Returns the instance for chaining. | ||
* @api public | ||
*/ | ||
define: function (key, value) { | ||
if (name) { | ||
utils.define(this[name], key, value); | ||
} else { | ||
utils.define(this, key, value); | ||
} | ||
return this; | ||
}, | ||
/** | ||
* Visit `method` over the items in the given object, or map | ||
* visit over the objects in an array. | ||
* | ||
* @name .visit | ||
* @param {String} `method` | ||
* @param {Object|Array} `val` | ||
* @return {Object} Returns the instance for chaining. | ||
* @api public | ||
*/ | ||
visit: function (method, val) { | ||
utils.visit(this, method, val); | ||
return this; | ||
} | ||
return this; | ||
}, | ||
}); | ||
/** | ||
* Define a non-enumerable property on the instance. | ||
* Static method for inheriting both the prototype and | ||
* static methods of the `Base` class. See [class-utils][] | ||
* for more details. | ||
* | ||
* ```js | ||
* // arbitrary `render` function using lodash `template` | ||
* define('render', function(str, locals) { | ||
* return _.template(str)(locals); | ||
* }); | ||
* ``` | ||
* @name .define | ||
* @param {String} `key` | ||
* @param {any} `value` | ||
* @return {Object} Returns the instance for chaining. | ||
* @api public | ||
*/ | ||
define: function (key, value) { | ||
utils.define(this, key, value); | ||
return this; | ||
}, | ||
Base.extend = cu.extend(Base); | ||
/** | ||
* Visit `method` over the items in the given object, or map | ||
* visit over the objects in an array. | ||
* Similar to `util.inherit`, but copies all static properties, | ||
* prototype properties, and descriptors from `Provider` to `Receiver`. | ||
* [class-utils][] for more details. | ||
* | ||
* @name .visit | ||
* @param {String} `method` | ||
* @param {Object|Array} `val` | ||
* @return {Object} Returns the instance for chaining. | ||
* @api public | ||
*/ | ||
visit: function (method, val) { | ||
utils.visit(this, method, val); | ||
return this; | ||
} | ||
}); | ||
Base.inherit = cu.inherit; | ||
return Base; | ||
} | ||
/** | ||
* Static method for inheriting both the prototype and | ||
* static methods of the `Base` class. See [class-utils][] | ||
* for more details. | ||
* | ||
* @api public | ||
* Expose `base-methods` | ||
*/ | ||
Base.extend = cu.extend(Base); | ||
module.exports = base(); | ||
/** | ||
* Similar to `util.inherit`, but copies all static properties, | ||
* prototype properties, and descriptors from `Provider` to `Receiver`. | ||
* [class-utils][] for more details. | ||
* | ||
* @api public | ||
* Allow users to define a namespace | ||
*/ | ||
Base.inherit = cu.inherit; | ||
/** | ||
* Expose `Base` | ||
*/ | ||
module.exports = Base; | ||
module.exports.create = base; |
{ | ||
"name": "base-methods", | ||
"description": "Starter for creating a node.js application with a handful of common methods, like `set`, `get`, and `del`.", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"homepage": "https://github.com/jonschlinkert/base-methods", | ||
@@ -68,2 +68,2 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
} | ||
} | ||
} |
@@ -19,2 +19,32 @@ # base-methods [![NPM version](https://badge.fury.io/js/base-methods.svg)](http://badge.fury.io/js/base-methods) | ||
**inherit** | ||
```js | ||
function Foo() { | ||
Base.call(this); | ||
} | ||
Base.extend(Foo); | ||
``` | ||
**instantiate** | ||
```js | ||
var base = new Base(); | ||
base.set('foo', 'bar'); | ||
console.log(base.foo); | ||
//=> 'bar' | ||
``` | ||
**Inherit or instantiate with a namespace** | ||
A `.create()` method is exposed on the exported function to allow you to create a custom namespace for setting/getting on the instance. | ||
```js | ||
var Base = require('base-methods').create('cache'); | ||
var base = new Base(); | ||
base.set('foo', 'bar'); | ||
console.log(base.cache.foo); | ||
//=> 'bar' | ||
``` | ||
## API | ||
@@ -39,3 +69,3 @@ | ||
### [.set](index.js#L61) | ||
### [.set](index.js#L63) | ||
@@ -66,3 +96,3 @@ Assign `value` to `key`. Also emits `set` with the key and value. | ||
### [.get](index.js#L88) | ||
### [.get](index.js#L94) | ||
@@ -85,3 +115,3 @@ Return the stored value of `key`. Dot notation may be used to get [nested property values][get-value]. | ||
### [.del](index.js#L109) | ||
### [.del](index.js#L118) | ||
@@ -105,3 +135,3 @@ Delete `key` from the instance. Also emits `del` with the key of the deleted item. | ||
### [.define](index.js#L135) | ||
### [.define](index.js#L148) | ||
@@ -125,3 +155,3 @@ Define a non-enumerable property on the instance. | ||
### [.visit](index.js#L151) | ||
### [.visit](index.js#L168) | ||
@@ -137,3 +167,3 @@ Visit `method` over the items in the given object, or map | ||
### [.extend](index.js#L165) | ||
### [.extend](index.js#L182) | ||
@@ -144,3 +174,3 @@ Static method for inheriting both the prototype and | ||
### [.inherit](index.js#L175) | ||
### [.inherit](index.js#L192) | ||
@@ -185,3 +215,3 @@ Similar to `util.inherit`, but copies all static properties, | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 15, 2015._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 30, 2015._ | ||
@@ -195,2 +225,2 @@ [class-utils]: https://github.com/jonschlinkert/class-utils | ||
[set-value]: https://github.com/jonschlinkert/set-value | ||
[unset-value]: https://github.com/jonschlinkert/unset-value | ||
[unset-value]: https://github.com/jonschlinkert/unset-value |
12456
191
216