base-methods
Advanced tools
Comparing version 0.2.3 to 0.2.4
17
index.js
@@ -176,2 +176,3 @@ 'use strict'; | ||
* @param {Function} `Ctor` The constructor to extend. | ||
* @param {Object} `proto` Optionally pass an object of methods to extend the prototype. | ||
* @api public | ||
@@ -182,2 +183,3 @@ */ | ||
util.inherits(Ctor, Base); | ||
for (var key in Base) { | ||
@@ -197,2 +199,17 @@ Ctor[key] = Base[key]; | ||
/** | ||
* Similar to `util.inherit`, but copies all properties | ||
* and descriptors from `Provider` to `Receiver` | ||
* | ||
* ```js | ||
* Base.inherit(MyClass, OtherClass); | ||
* ``` | ||
* | ||
* @param {Function} `Receiver` Constructor to extend | ||
* @param {Function} `Provider` | ||
* @api public | ||
*/ | ||
Base.inherit = utils.inherit; | ||
/** | ||
* Expose `Base` | ||
@@ -199,0 +216,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.3", | ||
"version": "0.2.4", | ||
"homepage": "https://github.com/jonschlinkert/base-methods", | ||
@@ -23,2 +23,5 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
}, | ||
"browser": { | ||
"./utils.js": "./utils-browser.js" | ||
}, | ||
"dependencies": { | ||
@@ -25,0 +28,0 @@ "collection-visit": "^0.2.0", |
@@ -131,3 +131,3 @@ # base-methods [![NPM version](https://badge.fury.io/js/base-methods.svg)](http://badge.fury.io/js/base-methods) | ||
### [.extend](index.js#L179) | ||
### [.extend](index.js#L180) | ||
@@ -139,2 +139,3 @@ Static method for inheriting both the prototype and static methods of the `Base` class. | ||
* `Ctor` **{Function}**: The constructor to extend. | ||
* `proto` **{Object}**: Optionally pass an object of methods to extend the prototype. | ||
@@ -157,2 +158,17 @@ **Example** | ||
### [.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 | ||
@@ -194,3 +210,3 @@ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 07, 2015._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 10, 2015._ | ||
@@ -197,0 +213,0 @@ [collection-visit]: https://github.com/jonschlinkert/collection-visit |
67
utils.js
@@ -8,2 +8,69 @@ | ||
lazy('define-property', 'define'); | ||
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); | ||
} | ||
}; | ||
module.exports = lazy; |
13974
252
216