Skip to content

Commit 35e821c

Browse files
committed
Sample .into(target) implementation for #3
1 parent 7b7fe11 commit 35e821c

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/index.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,29 @@
3535
}
3636

3737
/**
38-
* @return {Energy}
38+
* @return {Energy} emitter instance
3939
*/
4040
function energy(o) {
4141
return o instanceof Energy ? o[init]() : new Energy(o);
42-
}
42+
}
43+
44+
/**
45+
* @this {Function} wrapper that constructs the source instance
46+
* @param {Object|Function} target to convert into emitter
47+
* @return {Object|Function} target converted into emitter
48+
*/
49+
energy['into'] = function(target) {
50+
return defaults(target, this.call());
51+
};
52+
53+
/**
54+
* @this {Energy|Object|Function} source emitter
55+
* @param {Object|Function} target to convert into emitter
56+
* @return {Object|Function} target converted into emitter
57+
*/
58+
emitter['into'] = function(target) {
59+
return defaults(target, this);
60+
};
4361

4462
/**
4563
* @param {{length:number}} fns

test/index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,19 @@
1111
function yes() { return true; }
1212

1313
aok('instance', emitter instanceof energy);
14-
aok('listeners', typeof emitter.listeners(key).length == 'number');
14+
aok('listeners', typeof emitter.listeners(key).length == 'number');
15+
16+
aok('into', function() {
17+
function wannabe() {
18+
wannabe.emit('called');
19+
}
20+
21+
var bool = wannabe === energy.into(wannabe) && typeof wannabe.emit == 'function';
22+
bool && wannabe.once('called', function() {
23+
bool = this === wannabe;
24+
}) && wannabe();
25+
return bool;
26+
});
1527

1628
(function(id) {
1729
var bool = true, gone = indexOf ? function(what) {

0 commit comments

Comments
 (0)