Permalink
Browse files

update sync fallbacks to behave more like the native implementations …

…with unusual values
  • Loading branch information...
1 parent 1d42f7f commit 20b3dc64fcadcfc2bc84da3dc5ace7e622bf3ddb @caolan committed Mar 18, 2011
Showing with 117 additions and 9 deletions.
  1. +7 −8 nimble.js
  2. +1 −1 nimble.min.js
  3. +109 −0 test/test.js
View
@@ -20,28 +20,27 @@
return results;
};
- var isArray = Object.isArray || function(obj) {
- return Object.prototype.toString.call(obj) === '[object Array]';
- };
-
var fallback = function (name, fallback) {
var nativeFn = Array.prototype[name];
return function (obj, iterator, memo) {
- var fn = obj[name];
+ var fn = obj ? obj[name]: 0;
return fn && fn === nativeFn ?
fn.call(obj, iterator, memo):
fallback(obj, iterator, memo);
};
};
var eachSync = fallback('forEach', function (obj, iterator) {
- for (var i = 0, arr = keys(obj), len = arr.length; i < len; i++) {
- iterator(obj[arr[i]], arr[i], obj);
+ var isObj = obj instanceof Object;
+ var arr = isObj ? keys(obj): (obj || []), i;
+ for (i = 0, len = arr.length; i < len; i++) {
+ var k = isObj ? arr[i]: i;
+ iterator(obj[k], k, obj);
}
});
var eachParallel = function (obj, iterator, callback) {
- var len = isArray(obj) ? obj.length: keys(obj).length;
+ var len = obj.length || keys(obj).length;
if (!len) {
return callback();
}
View
Oops, something went wrong.
View
@@ -39,6 +39,26 @@ exports['each - sync, object'] = function (test) {
test.done();
};
+exports['each - sync, unsupported objects'] = function (test) {
+ test.strictEqual(
+ _.each(null, function (v) { return v; }),
+ undefined
+ );
+ test.strictEqual(
+ _.each(undefined, function (v) { return v; }),
+ undefined
+ );
+ test.strictEqual(
+ _.each(123, function (v) { return v; }),
+ undefined
+ );
+ test.strictEqual(
+ _.each('asdf', function (v) { return v; }),
+ undefined
+ );
+ test.done();
+};
+
exports['each - async'] = function (test) {
var calls = [];
_.each([1,2,3], function (value, index, arr, cb) {
@@ -143,6 +163,39 @@ exports['map - sync, object'] = function (test) {
test.done();
};
+exports['map - sync, unsupported objects'] = function (test) {
+ test.same(
+ _.map(null, function (v) { return v; }),
+ []
+ );
+ test.same(
+ _.map(undefined, function (v) { return v; }),
+ []
+ );
+ test.same(
+ _.map(123, function (v) { return v; }),
+ []
+ );
+ // IE does not support accessing of string chars by index
+ // and I don't consider it our job to alter this behaviour
+ if ('asdf'[0]) {
+ test.same(
+ _.map('asdf', function (v) { return v; }),
+ ['a','s','d','f']
+ );
+ }
+ else {
+ var r = _.map('asdf', function (v) { return v; });
+ // IE does some strange stuff to stop us testing using deepEqual
+ test.equal(r.length , 4);
+ test.ok(!r[0]);
+ test.ok(!r[1]);
+ test.ok(!r[2]);
+ test.ok(!r[3]);
+ }
+ test.done();
+};
+
exports['map - sync, original untouched'] = function(test){
var a = [1,2,3];
var results = _.map(a, function(x, callback){
@@ -291,6 +344,34 @@ exports['filter - sync, object'] = function (test) {
test.done();
};
+exports['filter - sync, unsupported objects'] = function (test) {
+ test.same(
+ _.filter(null, function (v) { return v; }),
+ []
+ );
+ test.same(
+ _.filter(undefined, function (v) { return v; }),
+ []
+ );
+ test.same(
+ _.filter(123, function (v) { return v; }),
+ []
+ );
+ // IE does not support accessing of string chars by index
+ // and I don't consider it our job to alter this behaviour
+ if ('asdf'[0]) {
+ test.same(
+ _.filter('asdf', function (v) { return v; }),
+ ['a','s','d','f']
+ );
+ }
+ else {
+ var r = _.filter('asdf', function (v) { return v; });
+ test.same(r, []);
+ }
+ test.done();
+};
+
exports['filter - sync, original untouched'] = function(test){
var a = [3,1,2];
var results = _.filter(a, function(x){
@@ -418,6 +499,34 @@ exports['reduce - sync, object'] = function (test) {
test.done();
};
+exports['reduce - sync, unsupported objects'] = function (test) {
+ test.strictEqual(
+ _.reduce(null, function (a,v) { return a + v; }, 'memo'),
+ 'memo'
+ );
+ test.strictEqual(
+ _.reduce(undefined, function (a,v) { return a + v; }, 'memo'),
+ 'memo'
+ );
+ test.strictEqual(
+ _.reduce(123, function (a,v) { return a + v; }, 'memo'),
+ 'memo'
+ );
+ // IE does not support accessing of string chars by index
+ // and I don't consider it our job to alter this behaviour
+ if ('asdf'[0]) {
+ test.strictEqual(
+ _.reduce('asdf', function (a,v) { return a + v; }, 'memo'),
+ 'memoasdf'
+ );
+ }
+ else {
+ var r = _.filter('asdf', function (v) { return v; });
+ test.same(r, 'memoundefinedundefinedundefinedundefined');
+ }
+ test.done();
+};
+
exports['reduce - async'] = function (test) {
var calls = [];
_.reduce([1,2,3], function (a, value, index, arr, cb) {

0 comments on commit 20b3dc6

Please sign in to comment.