Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[fix][test] Support older browsers that lack Array.prototype.some
  • Loading branch information
Jonathan Keslin committed Apr 3, 2017
1 parent b8c68dc commit f636767
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
21 changes: 17 additions & 4 deletions index.js
Expand Up @@ -321,10 +321,23 @@ Assert.add('eitherOfType, oneOfType', function multitypecheck(ofs, msg) {
var value = type(this.value)
, expect = format('`%j` (%s) to @ be a %s', this.value, value, ofs.join(' or a '));

return this.test(ofs.some(function(of) {
of = of.toString().toLowerCase();
return of === value;
}), msg, expect);
var test;
if (typeof ofs.some === 'function') {
test = ofs.some(function(of) {
of = of.toString().toLowerCase();
return of === value;
});
} else {
test = false;
for (var i = 0; i < ofs.length; i++) {
if (ofs[i].toString().toLowerCase() === value) {
test = true;
break;
}
}
}

return this.test(test, msg, expect);
});

/**
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Expand Up @@ -255,6 +255,18 @@ describe('Assertions', function assertions() {
try { assume({}).to.not.be.eitherOfType(['string', 'object']); }
catch (e) { next(); }
});

it('works without `Array.prototype.some`', function (next) {
var foo = ['string', 'object'];

// Override foo.some so we fallback to linear search
foo.some = null;

assume({}).to.be.eitherOfType(foo);

try { assume(1).to.be.eitherOfType(foo); }
catch (e) { next(); }
});
});

describe('#instanceof', function () {
Expand Down

0 comments on commit f636767

Please sign in to comment.