Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Commit

Permalink
Node update (#21)
Browse files Browse the repository at this point in the history
* Updated some syntax

* Update to Node4. Closes #15.

* Fixed lint issue.

* Updated dependencies.
  • Loading branch information
arb authored and cjihrig committed May 2, 2016
1 parent 31f59ff commit 4501c76
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 74 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
node_modules
.git
.idea/
jsconfig.json
typings/
.vscode
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
sudo: false
language: node_js
node_js:
- "0.10"
- "0.12"
- "4"
- "iojs"
- "iojs-v1"
- "iojs-v2"
- "6"
60 changes: 27 additions & 33 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
'use strict';
// Load modules
var Assert = require('assert');
var Get = require('lodash.get');
var Merge = require('lodash.merge');
var Set = require('lodash.set');

// Declare internals
var internals = {
store: []
};

internals.store.lookUp = function (obj, path) {
var i = this.length;
var lookup;
const Assert = require('assert');
const Get = require('lodash.get');
const Set = require('lodash.set');
const STORE = [];

STORE.lookUp = function (obj, path) {
let i = this.length;
let lookup;
while (!lookup && i--) {
var item = this[i];
const item = this[i];

if (item.obj === obj && item.path === path) {
lookup = item;
Expand All @@ -26,39 +21,39 @@ internals.store.lookUp = function (obj, path) {
};
};

exports.replace = function (obj, path, fn, options) {
module.exports.replace = function (obj, path, fn, options) {
Assert.ok(obj, 'obj must be defined');
Assert.ok(typeof obj === 'object' || typeof obj === 'function', 'obj must be an object or a constructor');
var func = Get(obj, path);
const func = Get(obj, path);
Assert.strictEqual(typeof func, 'function', 'path must be a valid function of obj');
Assert.strictEqual(typeof fn, 'function', 'fn must be a function object');

var find = internals.store.lookUp(obj, path);
const find = STORE.lookUp(obj, path);
Assert.strictEqual(find.result, undefined, 'there is already a replace for "obj"[' + path + ']');

options = Merge({}, options);
options = Object.assign({}, options);

var startOn = options.startOn | 0; // Force to an integer
var invocations = 0;
var stand = {
const startOn = options.startOn | 0; // Force to an integer
let invocations = 0;
const stand = {
obj: obj,
path: path
};
internals.store.push(stand);

function restore () {
var find = internals.store.lookUp(obj, path);
internals.store.splice(find.index, 1);
Set(obj, path, this.original);
}
STORE.push(stand);

var result = {
const result = {
original: func,
restore: restore,
restore,
get invocations () { return invocations; },
_stand: stand
};

function restore () {
const find = STORE.lookUp(obj, path);
STORE.splice(find.index, 1);
Set(obj, path, func);
}

function wrapper () {
invocations++;

Expand All @@ -70,11 +65,10 @@ exports.replace = function (obj, path, fn, options) {
result.restore();
}

var args = new Array(arguments.length + 1);

const args = new Array(arguments.length + 1);
args[0] = result;

for (var i = 0; i < arguments.length; ++i) {
for (let i = 0; i < arguments.length; ++i) {
args[i + 1] = arguments[i];
}

Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
],
"devDependencies": {
"belly-button": "2.x.x",
"code": "1.x.x",
"lab": "6.x.x"
"code": "2.x.x",
"lab": "10.x.x"
},
"dependencies": {
"lodash.get": "3.7.x",
"lodash.merge": "4.3.x",
"lodash.set": "3.7.x"
"lodash.get": "4.2.1",
"lodash.set": "4.1.0"
}
}
62 changes: 31 additions & 31 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
'use strict';
// Load modules

var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var StandIn = require('../');
const Code = require('code');
const Lab = require('lab');
const lab = exports.lab = Lab.script();
const StandIn = require('../');

// Declare test aliases
var describe = lab.describe;
var it = lab.it;
var expect = Code.expect;
const describe = lab.describe;
const it = lab.it;
const expect = Code.expect;

describe('stand-in', function () {
describe('replace', function () {
it('replaces a defined method', function (done) {
var log = StandIn.replace(console, 'log', function (stand, value) {
const log = StandIn.replace(console, 'log', function (stand, value) {
expect(value).to.equal('test');
expect(stand.invocations).to.equal(1);
log.restore();
Expand All @@ -25,13 +25,13 @@ describe('stand-in', function () {
});

it('correctly restores a replaced method', function (done) {
var foo = {
const foo = {
bar: function (valueone, valuetwo) {
throw new Error('this is a test error');
}
};

var replace = StandIn.replace(foo, 'bar', function (stand, valueone, valuetwo) {
const replace = StandIn.replace(foo, 'bar', function (stand, valueone, valuetwo) {
expect(valueone).to.equal(1);
expect(valuetwo).to.equal(2);

Expand All @@ -45,14 +45,14 @@ describe('stand-in', function () {
});

it('uses the correct this context', function (done) {
var foo = {
const foo = {
value: 55,
bar: function (valueone, valuetwo) {
console.log('%s,%s');
}
};

var replace = StandIn.replace(foo, 'bar', function (stand, valueone, valuetwo) {
const replace = StandIn.replace(foo, 'bar', function (stand, valueone, valuetwo) {
expect(valueone).to.equal(1);
expect(valuetwo).to.equal(2);
expect(this.value).to.equal(55);
Expand All @@ -67,13 +67,13 @@ describe('stand-in', function () {
});

it('provides a mechanism to use the original method', function (done) {
var foo = {
const foo = {
bar: function () {
return false;
}
};

var replace = StandIn.replace(foo, 'bar', function () {
const replace = StandIn.replace(foo, 'bar', function () {
expect(replace.original()).to.equal(false);
replace.restore();
done();
Expand All @@ -83,9 +83,9 @@ describe('stand-in', function () {
});

it('provides the stand-in object as the first parameter to the function', function (done) {
var log = StandIn.replace(console, 'log', function (stand, value) {
const log = StandIn.replace(console, 'log', function (stand, value) {
expect(value).to.equal('test');
expect(stand).to.deep.equal(log);
expect(stand).to.equal(log);

stand.restore();
done();
Expand All @@ -95,7 +95,7 @@ describe('stand-in', function () {
});

it('supports deep paths for replace', function (done) {
var x = {
const x = {
foo: {
bar: {
baz: function (value) {
Expand All @@ -115,7 +115,7 @@ describe('stand-in', function () {
});

it('supports replacing prototype methods', function (done) {
var Person = function (name) {
const Person = function (name) {
this.name = name;
};
Person.prototype.print = function (value) {
Expand All @@ -127,7 +127,7 @@ describe('stand-in', function () {
return this.name;
});

var x = new Person('adam');
const x = new Person('adam');
expect(x.print()).to.equal('adam');
expect(x.print).to.throw(Error);
done();
Expand All @@ -141,21 +141,21 @@ describe('stand-in', function () {
return this.val;
};

var stand = StandIn.replace(Foo, 'prototype.getVal', function (stand) {
const stand = StandIn.replace(Foo, 'prototype.getVal', function (stand) {
return 'stand';
});

stand.restore();

var foo = new Foo('bar');
const foo = new Foo('bar');
expect(foo.getVal()).to.equal('bar');
done();
});

it('only activate the stand for certain invocations', function (done) {
var obj = { method: function (value) { return -1 * value; } };
var calls = 0;
var stand = StandIn.replace(obj, 'method', function (stand, value) {
const obj = { method: function (value) { return -1 * value; } };
let calls = 0;
const stand = StandIn.replace(obj, 'method', function (stand, value) {
calls++;
return value;
}, { startOn: 2, stopAfter: 3 });
Expand Down Expand Up @@ -200,7 +200,7 @@ describe('stand-in', function () {
});

it('throws an error if obj[function] is not a function', function (done) {
var foo = {
const foo = {
bar: 1
};

Expand All @@ -212,7 +212,7 @@ describe('stand-in', function () {
});

it('throws an error if fn is not a function', function (done) {
var foo = {
const foo = {
bar: function () {}
};

Expand All @@ -226,7 +226,7 @@ describe('stand-in', function () {

describe('duplication', function () {
it('throws an error if you try to replace without restoring on the same object', function (done) {
var log = StandIn.replace(console, 'log', function (value) {});
const log = StandIn.replace(console, 'log', function (value) {});

expect(function () {
StandIn.replace(console, 'log', function (value) {});
Expand All @@ -238,13 +238,13 @@ describe('stand-in', function () {
});

it('allows duplication methods if obj is a new instance', function (done) {
var bar = function (value) {
const bar = function (value) {
console.log(value);
};
var error = StandIn.replace(console, 'error', function (stand, value) {});
const error = StandIn.replace(console, 'error', function (stand, value) {});

for (var i = 0; i < 10; ++i) {
var x = {};
for (let i = 0; i < 10; ++i) {
const x = {};
x.bar = bar;
x.value = i;

Expand Down

0 comments on commit 4501c76

Please sign in to comment.