base-methods
Advanced tools
Comparing version 0.2.5 to 0.2.6
38
index.js
@@ -5,2 +5,3 @@ 'use strict'; | ||
var Emitter = require('component-emitter'); | ||
var cu = require('class-utils'); | ||
var utils = require('./utils'); | ||
@@ -110,3 +111,3 @@ | ||
del: function (key) { | ||
if (typeof key === 'object') { | ||
if (Array.isArray(key)) { | ||
this.visit('del', key); | ||
@@ -160,40 +161,19 @@ } else { | ||
* Static method for inheriting both the prototype and | ||
* static methods of the `Base` class. | ||
* static methods of the `Base` class. See [class-utils][] | ||
* for more details. | ||
* | ||
* ```js | ||
* function MyApp(options) { | ||
* Base.call(this, options); | ||
* } | ||
* Base.extend(MyApp); | ||
* | ||
* | ||
* // Optionally pass another object to extend onto `MyApp` | ||
* function MyApp(options) { | ||
* Base.call(this, options); | ||
* Foo.call(this, options); | ||
* } | ||
* Base.extend(MyApp, Foo.prototype); | ||
* ``` | ||
* | ||
* @param {Function} `Ctor` The constructor to extend. | ||
* @param {Object} `proto` Optionally pass an object of methods to extend the prototype. | ||
* @api public | ||
*/ | ||
Base.extend = utils.extend(Base); | ||
Base.extend = cu.extend(Base); | ||
/** | ||
* Similar to `util.inherit`, but copies all properties | ||
* and descriptors from `Provider` to `Receiver` | ||
* Similar to `util.inherit`, but copies all static properties, | ||
* prototype properties, and descriptors from `Provider` to `Receiver`. | ||
* [class-utils][] for more details. | ||
* | ||
* ```js | ||
* Base.inherit(MyClass, OtherClass); | ||
* ``` | ||
* | ||
* @param {Function} `Receiver` Constructor to extend | ||
* @param {Function} `Provider` | ||
* @api public | ||
*/ | ||
Base.inherit = utils.inherit; | ||
Base.inherit = cu.inherit; | ||
@@ -200,0 +180,0 @@ /** |
{ | ||
"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.5", | ||
"version": "0.2.6", | ||
"homepage": "https://github.com/jonschlinkert/base-methods", | ||
@@ -23,6 +23,7 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
}, | ||
"browser": { | ||
"browser": { | ||
"./utils.js": "./utils-browser.js" | ||
}, | ||
"dependencies": { | ||
"class-utils": "^0.1.1", | ||
"collection-visit": "^0.2.0", | ||
@@ -37,2 +38,7 @@ "component-emitter": "^1.2.0", | ||
"devDependencies": { | ||
"gulp": "^3.9.0", | ||
"gulp-istanbul": "^0.10.0", | ||
"gulp-jshint": "^1.11.2", | ||
"gulp-mocha": "^2.1.3", | ||
"jshint-stylish": "^2.0.1", | ||
"mocha": "*", | ||
@@ -52,3 +58,14 @@ "should": "*" | ||
"visit" | ||
] | ||
} | ||
], | ||
"verb": { | ||
"related": { | ||
"list": [ | ||
"define-property", | ||
"set-value", | ||
"get-value", | ||
"unset-value", | ||
"class-utils" | ||
] | ||
} | ||
} | ||
} |
@@ -21,3 +21,3 @@ # base-methods [![NPM version](https://badge.fury.io/js/base-methods.svg)](http://badge.fury.io/js/base-methods) | ||
### [Base](index.js#L21) | ||
### [Base](index.js#L22) | ||
@@ -39,3 +39,3 @@ Create an instance of `Base` with optional `options`. | ||
### [.set](index.js#L60) | ||
### [.set](index.js#L61) | ||
@@ -66,3 +66,3 @@ Assign `value` to `key`. Also emits `set` with the key and value. | ||
### [.get](index.js#L87) | ||
### [.get](index.js#L88) | ||
@@ -85,3 +85,3 @@ Return the stored value of `key`. Dot notation may be used to get [nested property values][get-value]. | ||
### [.del](index.js#L108) | ||
### [.del](index.js#L109) | ||
@@ -105,3 +105,3 @@ Delete `key` from the instance. Also emits `del` with the key of the deleted item. | ||
### [.define](index.js#L134) | ||
### [.define](index.js#L135) | ||
@@ -125,3 +125,3 @@ Define a non-enumerable property on the instance. | ||
### [.visit](index.js#L150) | ||
### [.visit](index.js#L151) | ||
@@ -137,49 +137,19 @@ Visit `method` over the items in the given object, or map | ||
### [.extend](index.js#L180) | ||
### [.extend](index.js#L165) | ||
Static method for inheriting both the prototype and static methods of the `Base` class. | ||
Static method for inheriting both the prototype and | ||
static methods of the `Base` class. See [class-utils][] | ||
for more details. | ||
**Params** | ||
### [.inherit](index.js#L175) | ||
* `Ctor` **{Function}**: The constructor to extend. | ||
* `proto` **{Object}**: Optionally pass an object of methods to extend the prototype. | ||
Similar to `util.inherit`, but copies all static properties, | ||
prototype properties, and descriptors from `Provider` to `Receiver`. | ||
[class-utils][] for more details. | ||
**Example** | ||
```js | ||
function MyApp(options) { | ||
Base.call(this, options); | ||
} | ||
Base.extend(MyApp); | ||
// Optionally pass another object to extend onto `MyApp` | ||
function MyApp(options) { | ||
Base.call(this, options); | ||
Foo.call(this, options); | ||
} | ||
Base.extend(MyApp, Foo.prototype); | ||
``` | ||
### [.inherit](index.js#L209) | ||
Similar to `util.inherit`, but copies all properties and descriptors from `Provider` to `Receiver` | ||
**Params** | ||
* `Receiver` **{Function}**: Constructor to extend | ||
* `Provider` **{Function}** | ||
**Example** | ||
```js | ||
Base.inherit(MyClass, OtherClass); | ||
``` | ||
## Related projects | ||
* [collection-visit](https://www.npmjs.com/package/collection-visit): Visit a method over the items in an object, or map visit over the objects… [more](https://www.npmjs.com/package/collection-visit) | [homepage](https://github.com/jonschlinkert/collection-visit) | ||
* [component-emitter](https://www.npmjs.com/package/component-emitter): Event emitter | [homepage](https://github.com/component/emitter) | ||
* [class-utils](https://www.npmjs.com/package/class-utils): Utils for working with JavaScript classes and prototype methods. | [homepage](https://github.com/jonschlinkert/class-utils) | ||
* [define-property](https://www.npmjs.com/package/define-property): Define a non-enumerable property on an object. | [homepage](https://github.com/jonschlinkert/define-property) | ||
* [get-value](https://www.npmjs.com/package/get-value): Use property paths (` a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value) | ||
* [lazy-cache](https://www.npmjs.com/package/lazy-cache): Cache requires to be lazy-loaded when needed. | [homepage](https://github.com/jonschlinkert/lazy-cache) | ||
* [set-value](https://www.npmjs.com/package/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value) | ||
@@ -214,4 +184,5 @@ * [unset-value](https://www.npmjs.com/package/unset-value): Delete nested properties from an object using dot notation. | [homepage](https://github.com/jonschlinkert/unset-value) | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 10, 2015._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 15, 2015._ | ||
[class-utils]: https://github.com/jonschlinkert/class-utils | ||
[collection-visit]: https://github.com/jonschlinkert/collection-visit | ||
@@ -218,0 +189,0 @@ [component-emitter]: https://github.com/component/emitter |
87
utils.js
@@ -0,3 +1,3 @@ | ||
'use strict'; | ||
var util = require('util'); | ||
var lazy = require('lazy-cache')(require); | ||
@@ -10,87 +10,2 @@ lazy('set-value', 'set'); | ||
var utils = lazy; | ||
/** | ||
* Returns true if an array have the given element | ||
* @return {Boolean} | ||
*/ | ||
utils.has = function has(keys, key) { | ||
return keys.indexOf(key) > -1; | ||
}; | ||
/** | ||
* Return true if the given value is an object. | ||
* @return {Boolean} | ||
*/ | ||
utils.isObject = function isObject(val) { | ||
return val && (typeof val === 'function' || typeof val === 'object') | ||
&& !Array.isArray(val); | ||
}; | ||
utils.getDescriptor = function getDescriptor(provider, key) { | ||
return Object.getOwnPropertyDescriptor(provider, key); | ||
}; | ||
utils.copyDescriptor = function copyDescriptor(receiver, provider, key) { | ||
var val = utils.getDescriptor(provider, key); | ||
if (val) utils.define(receiver, key, val); | ||
}; | ||
utils.copy = function copy(receiver, provider, omit) { | ||
var props = Object.getOwnPropertyNames(provider); | ||
var keys = Object.keys(provider); | ||
var len = props.length, key; | ||
while (len--) { | ||
key = props[len]; | ||
if (utils.has(keys, key)) { | ||
utils.define(receiver, key, provider[key]); | ||
} else if (!(key in receiver) && !utils.has(omit, key)) { | ||
utils.copyDescriptor(receiver, provider, key); | ||
} | ||
} | ||
}; | ||
utils.inherit = function inherit(receiver, provider, omit) { | ||
if (!utils.isObject(receiver)) { | ||
throw new TypeError('expected receiver to be an object.'); | ||
} | ||
if (!utils.isObject(provider)) { | ||
throw new TypeError('expected provider to be an object.'); | ||
} | ||
var keys = []; | ||
for (var key in provider) { | ||
keys.push(key); | ||
utils.define(receiver, key, provider[key]); | ||
} | ||
if (receiver.prototype && provider.prototype) { | ||
utils.copy(receiver.prototype, provider.prototype, keys); | ||
utils.define(receiver, '__super__', provider.prototype); | ||
} | ||
}; | ||
utils.extend = function extend (Parent) { | ||
return function (Ctor, proto) { | ||
util.inherits(Ctor, Parent); | ||
for (var key in Parent) { | ||
Ctor[key] = Parent[key]; | ||
} | ||
if (typeof proto === 'object') { | ||
var obj = Object.create(proto); | ||
for (var k in obj) { | ||
Ctor.prototype[k] = obj[k]; | ||
} | ||
} | ||
Ctor.extend = utils.extend(Ctor); | ||
}; | ||
}; | ||
module.exports = lazy; |
11158
8
7
169
187
+ Addedclass-utils@^0.1.1
+ Addedclass-utils@0.1.2(transitive)