Skip to content

Instantly share code, notes, and snippets.

@foobarfighter
Created December 18, 2011 06:44
Show Gist options
  • Save foobarfighter/1492582 to your computer and use it in GitHub Desktop.
Save foobarfighter/1492582 to your computer and use it in GitHub Desktop.
is this an underscore bug in _.wrap?
var _ = require('underscore')
, a = require('assert');
var firstArg;
var handler = function (){};
var wrapped = _.wrap(handler, function (func){
console.log('--> arguments is wrong or unexpected', arguments);
a.strictEqual(arguments[0], func);
a.strictEqual(arguments[1], firstArg);
a.strictEqual(arguments[2], 'second arg');
a.strictEqual(arguments[3], 'third arg');
});
firstArg = 'not an array... works correctly';
wrapped(firstArg, 'second arg', 'third arg');
// If the first arg is an array then things go awry
firstArg = ['one', 'two', 'three'];
wrapped(firstArg, 'second arg', 'third arg');
@foobarfighter
Copy link
Author

The first time "wrapped" executes without an array as the first argument and it passes the assertions. The second time "wrapped" runs with an array as the first argument and things blow up. Here is the output:

--> arguments is wrong or unexpected { '0': [Function],
'1': 'not an array... works correctly',
'2': 'second arg',
'3': 'third arg' }
--> arguments is wrong or unexpected { '0': [Function],
'1': 'one',
'2': 'two',
'3': 'three',
'4': 'second arg',
'5': 'third arg' }

node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
AssertionError: ["one","two","three"] === "one"
at /Users/bremeika/workspace/sandbox/underscore_poc.js:11:5
at /usr/local/lib/node_modules/underscore/underscore.js:587:22
at Object. (/Users/bremeika/workspace/sandbox/underscore_poc.js:21:1)
at Module._compile (module.js:404:26)
at Object..js (module.js:410:10)
at Module.load (module.js:336:31)
at Function._load (module.js:297:12)
at Array. (module.js:423:10)
at EventEmitter._tickCallback (node.js:126:26)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment