-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3b4460c
Showing
10 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = tab | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.coffee] | ||
indent_style = space | ||
|
||
[{package.json,*.yml}] | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.sw[a-p] | ||
/node_modules/ | ||
npm-ignore.log | ||
/coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
instrumentation: | ||
excludes: | ||
- test.js | ||
- test/**/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/coverage/ | ||
/test.js | ||
/test/ | ||
*.sw[a-p] | ||
/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
language: node_js | ||
|
||
script: | ||
- node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- --compilers coffee:coffee-script/register | ||
- cat coverage/lcov.info | node_modules/.bin/coveralls | ||
node_js: | ||
- "0.10" | ||
- "0.11" | ||
- "0.12" | ||
- "iojs" | ||
os: | ||
- linux | ||
- osx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 JD Ballard | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# node-weak-inherit [![Travis-CI.org Build Status](https://img.shields.io/travis/Qix-/node-weak-inherit.svg?style=flat-square)](https://travis-ci.org/Qix-/node-weak-inherit) [![Coveralls.io Coverage Rating](https://img.shields.io/coveralls/Qix-/node-weak-inherit.svg?style=flat-square)](https://coveralls.io/r/Qix-/node-weak-inherit) | ||
> Weakly inherit a class/function's prototype on any object | ||
This point of this module is to allow the extension of a class and its method | ||
on top of objects that are not necessarily classes themselves. | ||
|
||
This is useful for API builders in particular which need to "extend" a class | ||
on to a function that is still callable itself. | ||
|
||
## Example | ||
```javascript | ||
var weakInherit = require('weak-inherit'); | ||
var EventEmitter = require('events').EventEmitter; | ||
|
||
var fn = function (name) { | ||
console.log('Hello,', name); | ||
}; | ||
|
||
// anything we pass to `generator` will extend EventEmitter | ||
var generator = weakInherit(EventEmitter); | ||
|
||
var obj = generator(fn); | ||
|
||
fn('Qix'); //-> Hello, Qix | ||
|
||
fn.on('test', function () { | ||
console.log('Hi!'); | ||
}); | ||
|
||
fn.emit('test'); //-> Hi! | ||
``` | ||
|
||
## License | ||
Licensed under the [MIT License](http://opensource.org/licenses/MIT). | ||
You can find a copy of it in [LICENSE](LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
module.exports = function weakInherit(fn) { | ||
return function (obj) { | ||
fn.apply(obj, [].slice.call(arguments, 1)); | ||
|
||
if (!fn.prototype) { | ||
return obj; | ||
} | ||
|
||
for (var k in fn.prototype) { | ||
if (fn.prototype.hasOwnProperty(k)) { | ||
obj[k] = fn.prototype[k]; | ||
} | ||
} | ||
|
||
return obj; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "weak-inherit", | ||
"description": "Weakly inherit a class - no prototypes involved", | ||
"version": "0.1.0", | ||
"author": "Qix (http://github.com/qix-)", | ||
"keywords": [ | ||
"weak", | ||
"weakly", | ||
"inherit", | ||
"inherits", | ||
"object", | ||
"prototype", | ||
"property", | ||
"properties", | ||
"function" | ||
], | ||
"license": "MIT", | ||
"scripts": { | ||
"pretest": "xo", | ||
"test": "mocha --compilers coffee:coffee-script/register" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/qix-/node-weak-inherit.git" | ||
}, | ||
"devDependencies": { | ||
"coffee-script": "^1.9.3", | ||
"coveralls": "^2.11.2", | ||
"istanbul": "^0.3.17", | ||
"mocha": "^2.2.5", | ||
"should": "^7.0.1", | ||
"xo": "^0.6.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
should = require 'should' | ||
{EventEmitter} = require 'events' | ||
weakInherit = require '../' | ||
|
||
Error.stackTraceLimit = Infinity | ||
|
||
class Foo | ||
constructor: (@b)-> | ||
bar:-> 1234 | ||
qux:-> 'hello' | ||
qix:(@a)-> 42 + @a + @b | ||
|
||
it 'should inherit on a function', -> | ||
fn = (str)-> "test #{str}" | ||
generator = weakInherit Foo | ||
|
||
obj = generator fn, 10 | ||
|
||
(should fn 'hi').equal 'test hi' | ||
(should obj 'hi').equal 'test hi' | ||
obj.b.should.equal 10 | ||
(should obj.bar()).equal 1234 | ||
(should obj.qux()).equal 'hello' | ||
(should obj.qix 20).equal 72 | ||
obj.a.should.equal 20 | ||
|
||
it 'should work with EventEmitter', (done)-> | ||
fn = (str)-> "test #{str}" | ||
generator = weakInherit EventEmitter | ||
|
||
obj = generator fn | ||
|
||
(should fn 'hi').equal 'test hi' | ||
(should obj 'hi').equal 'test hi' | ||
(should obj.on).be.ok() | ||
(should obj.emit).be.ok() | ||
obj.on 'test', (message)-> | ||
message.should.equal 'hello!' | ||
done() | ||
obj.emit 'test', 'hello!' |