-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
5 changed files
with
82 additions
and
66 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,3 @@ | ||
coverage/ | ||
unherit.js | ||
unherit.min.js |
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 |
---|---|---|
@@ -1,47 +1,47 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
/* Dependencies. */ | ||
var xtend = require('xtend'); | ||
var inherits = require('inherits'); | ||
var xtend = require('xtend') | ||
var inherits = require('inherits') | ||
|
||
/* Expose. */ | ||
module.exports = unherit; | ||
module.exports = unherit | ||
|
||
/* Create a custom constructor which can be modified | ||
* without affecting the original class. */ | ||
function unherit(Super) { | ||
var result; | ||
var key; | ||
var value; | ||
var result | ||
var key | ||
var value | ||
|
||
inherits(Of, Super); | ||
inherits(From, Of); | ||
inherits(Of, Super) | ||
inherits(From, Of) | ||
|
||
/* Clone values. */ | ||
result = Of.prototype; | ||
result = Of.prototype | ||
|
||
for (key in result) { | ||
value = result[key]; | ||
value = result[key] | ||
|
||
if (value && typeof value === 'object') { | ||
result[key] = 'concat' in value ? value.concat() : xtend(value); | ||
result[key] = 'concat' in value ? value.concat() : xtend(value) | ||
} | ||
} | ||
|
||
return Of; | ||
return Of | ||
|
||
/* Constructor accepting a single argument, | ||
* which itself is an `arguments` object. */ | ||
function From(parameters) { | ||
return Super.apply(this, parameters); | ||
return Super.apply(this, parameters) | ||
} | ||
|
||
/* Constructor accepting variadic arguments. */ | ||
function Of() { | ||
if (!(this instanceof Of)) { | ||
return new From(arguments); | ||
return new From(arguments) | ||
} | ||
|
||
return Super.apply(this, arguments); | ||
return Super.apply(this, arguments) | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1,78 +1,82 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var EventEmitter = require('events').EventEmitter; | ||
var test = require('tape'); | ||
var unherit = require('.'); | ||
var EventEmitter = require('events').EventEmitter | ||
var test = require('tape') | ||
var unherit = require('.') | ||
|
||
test('unherit(Super)', function (t) { | ||
var Emitter = unherit(EventEmitter); | ||
test('unherit(Super)', function(t) { | ||
var Emitter = unherit(EventEmitter) | ||
|
||
t.equal(Emitter.prototype.defaultMaxListeners, undefined); | ||
t.equal(Emitter.prototype.defaultMaxListeners, undefined) | ||
|
||
Emitter.prototype.defaultMaxListeners = 0; | ||
Emitter.prototype.defaultMaxListeners = 0 | ||
|
||
t.equal(new Emitter().defaultMaxListeners, 0, 'should work (1)'); | ||
t.equal(new EventEmitter().defaultMaxListeners, undefined, 'should work (2)'); | ||
t.equal(new Emitter().defaultMaxListeners, 0, 'should work (1)') | ||
t.equal(new EventEmitter().defaultMaxListeners, undefined, 'should work (2)') | ||
|
||
t.equal(new Emitter().constructor, Emitter, 'should work (3)'); | ||
t.equal(new EventEmitter().constructor, EventEmitter, 'should work (4)'); | ||
t.equal(new Emitter().constructor, Emitter, 'should work (3)') | ||
t.equal(new EventEmitter().constructor, EventEmitter, 'should work (4)') | ||
|
||
Emitter = unherit(EventEmitter); | ||
Emitter = unherit(EventEmitter) | ||
|
||
t.ok(new Emitter() instanceof EventEmitter, 'should fool `instanceof` checks'); | ||
t.ok(new Emitter() instanceof EventEmitter, 'should fool `instanceof` checks') | ||
|
||
/* Constructor which internally uses an `instanceof` | ||
* check. */ | ||
function A(one, two, three) { | ||
t.equal(one, 'foo'); | ||
t.equal(two, 'bar'); | ||
t.equal(three, 'baz'); | ||
t.ok(this instanceof A); | ||
t.equal(one, 'foo') | ||
t.equal(two, 'bar') | ||
t.equal(three, 'baz') | ||
t.ok(this instanceof A) | ||
|
||
this.values = [].slice.call(arguments); | ||
this.values = [].slice.call(arguments) | ||
} | ||
|
||
var B = unherit(A); | ||
var B = unherit(A) | ||
|
||
// eslint-disable-next-line new-cap | ||
var b = B('foo', 'bar', 'baz'); | ||
var b = B('foo', 'bar', 'baz') | ||
|
||
t.ok(b instanceof A, 'should fool `instanceof` without `new` (1)'); | ||
t.ok(b instanceof B, 'should fool `instanceof` without `new` (2)'); | ||
t.ok(b instanceof A, 'should fool `instanceof` without `new` (1)') | ||
t.ok(b instanceof B, 'should fool `instanceof` without `new` (2)') | ||
|
||
t.deepEqual( | ||
b.values, | ||
['foo', 'bar', 'baz'], | ||
'should fool `instanceof` without `new` (3)' | ||
); | ||
) | ||
|
||
var E; | ||
var F; | ||
var E | ||
var F | ||
|
||
function C() {} | ||
|
||
C.prototype.values = [1, 2]; | ||
C.prototype.values = [1, 2] | ||
|
||
function Proto() {} | ||
|
||
function D() {} | ||
Proto.prototype = C.prototype; | ||
Proto.prototype = C.prototype | ||
|
||
D.prototype = new Proto(); | ||
D.prototype.values = [1, 2, 3]; | ||
D.prototype = new Proto() | ||
D.prototype.values = [1, 2, 3] | ||
|
||
E = unherit(D); | ||
E = unherit(D) | ||
|
||
/* This failed in 1.0.4 */ | ||
t.deepEqual(E.prototype.values, [1, 2, 3], 'shouldn’t fail on inheritance (1)'); | ||
t.deepEqual(new E().values, [1, 2, 3], 'shouldn’t fail on inheritance (2)'); | ||
t.deepEqual( | ||
E.prototype.values, | ||
[1, 2, 3], | ||
'shouldn’t fail on inheritance (1)' | ||
) | ||
t.deepEqual(new E().values, [1, 2, 3], 'shouldn’t fail on inheritance (2)') | ||
|
||
E.prototype.values.push(4); | ||
E.prototype.values.push(4) | ||
|
||
F = unherit(D); | ||
F = unherit(D) | ||
|
||
t.deepEqual(F.prototype.values, [1, 2, 3], 'shouldn clone values (1)'); | ||
t.deepEqual(new F().values, [1, 2, 3], 'shouldn clone values (2)'); | ||
t.deepEqual(F.prototype.values, [1, 2, 3], 'shouldn clone values (1)') | ||
t.deepEqual(new F().values, [1, 2, 3], 'shouldn clone values (2)') | ||
|
||
t.end(); | ||
}); | ||
t.end() | ||
}) |