Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- committed Aug 23, 2015
0 parents commit 3b4460c
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.sw[a-p]
/node_modules/
npm-ignore.log
/coverage/
4 changes: 4 additions & 0 deletions .istanbul.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
instrumentation:
excludes:
- test.js
- test/**/*
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/coverage/
/test.js
/test/
*.sw[a-p]
/node_modules/
13 changes: 13 additions & 0 deletions .travis.yml
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
21 changes: 21 additions & 0 deletions LICENSE
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.
35 changes: 35 additions & 0 deletions README.md
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).
19 changes: 19 additions & 0 deletions index.js
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;
};
};
34 changes: 34 additions & 0 deletions package.json
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"
}
}
40 changes: 40 additions & 0 deletions test/test.coffee
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!'

0 comments on commit 3b4460c

Please sign in to comment.