Skip to content

Commit

Permalink
add bind option
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Jun 17, 2016
1 parent 94c7861 commit 32e5fe7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
30 changes: 27 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var processFn = function (fn, P, opts) {
var processFn = function (fn, P, opts, bind) {
return function () {
var that = this;
var that = bind || this;
var args = new Array(arguments.length);

for (var i = 0; i < arguments.length; i++) {
Expand Down Expand Up @@ -39,6 +39,11 @@ var pify = module.exports = function (obj, P, opts) {

opts = opts || {};
var exclude = opts.exclude || [/.+Sync$/];
var bind = opts.bind === false ? false : (opts.bind || obj);

if (bind === true) {
bind = obj;
}

var filter = function (key) {
var match = function (pattern) {
Expand All @@ -59,7 +64,26 @@ var pify = module.exports = function (obj, P, opts) {
return Object.keys(obj).reduce(function (ret, key) {
var x = obj[key];

ret[key] = typeof x === 'function' && filter(key) ? processFn(x, P, opts) : x;
if (typeof x === 'function') {
if (filter(key)) {
x = processFn(x, P, opts, bind);
}

ret[key] = x;
} else if (bind) {
Object.defineProperty(ret, key, {
enumerable: true,
configurable: true,
get: function () {
return bind[key];
},
set: function (val) {
bind[key] = val;
}
});
} else {
ret[key] = x;
}

return ret;
}, ret);
Expand Down
2 changes: 1 addition & 1 deletion optimization-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ sut.unicorn().then(function () {
});
}).catch(function (err) {
console.log(err.stack);
process.exit(1);
process.exit(1); // eslint-disable-line xo/no-process-exit
});
63 changes: 63 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,69 @@ test('wrap core method', async t => {
t.is(JSON.parse(await fn(fs.readFile)('package.json')).name, 'pify');
});

test('binds to the original object by default', async t => {
const obj = {
x: 'foo',
y: function (cb) {
setImmediate(() => cb(null, this.x));
}
};

const pified = fn(obj);
obj.x = 'bar';

t.is(await pified.y(), 'bar');
t.is(pified.x, 'bar');
});

test('bind:true will bind to the original object', async t => {
const obj = {
x: 'foo',
y: function (cb) {
setImmediate(() => cb(null, this.x));
}
};

const pified = fn(obj, {bind: true});
obj.x = 'bar';

t.is(await pified.y(), 'bar');
t.is(pified.x, 'bar');
});

test('bind:otherObj will bind to otherObj', async t => {
const obj = {
x: 'foo',
y: function (cb) {
setImmediate(() => cb(null, this.x));
}
};

const otherObj = {
x: 'baz'
};

const pified = fn(obj, {bind: otherObj});

t.is(await pified.y(), 'baz');
});

test('bind:false creates a copy', async t => {
const obj = {
x: 'foo',
y: function (cb) {
setImmediate(() => cb(null, this.x));
}
};

const pified = fn(obj, {bind: false});

obj.x = 'bar';

t.is(await pified.y(), 'foo');
t.is(pified.x, 'foo');
});

test('module support', async t => {
t.is(JSON.parse(await fn(fs).readFile('package.json')).name, 'pify');
});
Expand Down

0 comments on commit 32e5fe7

Please sign in to comment.