Skip to content

Commit 5fc6d53

Browse files
committed
Implement isIterable
1 parent 41fd6d7 commit 5fc6d53

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/chai/interface/assert.js

+17
Original file line numberDiff line numberDiff line change
@@ -2484,6 +2484,23 @@ assert.oneOf = function (inList, list, msg) {
24842484
new Assertion(inList, msg, assert.oneOf, true).to.be.oneOf(list);
24852485
}
24862486

2487+
/**
2488+
* ### isIterable(obj, [message])
2489+
*
2490+
* Asserts that the target is an iterable, which means that it has a iterator
2491+
* with the exception of `String.`
2492+
*
2493+
* assert.isIterable([1, 2]);
2494+
*
2495+
* @param {unknown} obj
2496+
* @param {string} [msg]
2497+
* @namespace Assert
2498+
* @api public
2499+
*/
2500+
assert.isIterable = function(obj, msg) {
2501+
new Assertion(obj, msg, assert.isIterable, true).to.be.an('iterable');
2502+
}
2503+
24872504
/**
24882505
* ### .changes(function, object, property, [message])
24892506
*

test/assert.js

+30
Original file line numberDiff line numberDiff line change
@@ -2377,6 +2377,36 @@ describe('assert', function () {
23772377
}, 'blah: the argument to most must be a number');
23782378
});
23792379

2380+
it('iterable', function() {
2381+
assert.isIterable([1, 2, 3]);
2382+
assert.isIterable(new Map([[1, 'one'], [2, 'two'], [3, 'three']]));
2383+
assert.isIterable(new Set([1, 2, 3]));
2384+
2385+
err(function() {
2386+
assert.isIterable(42);
2387+
}, 'expected 42 to be an iterable');
2388+
2389+
err(function() {
2390+
assert.isIterable('hello');
2391+
}, "expected 'hello' to be an iterable");
2392+
2393+
err(function() {
2394+
assert.isIterable(undefined);
2395+
}, 'expected undefined to be an iterable');
2396+
2397+
err(function() {
2398+
assert.isIterable(null);
2399+
}, 'expected null to be an iterable');
2400+
2401+
err(function() {
2402+
assert.isIterable(true);
2403+
}, 'expected true to be an iterable');
2404+
2405+
err(function() {
2406+
assert.isIterable({ key: 'value' });
2407+
}, 'expected { key: \'value\' } to be an iterable');
2408+
});
2409+
23802410
it('change', function() {
23812411
var obj = { value: 10, str: 'foo' },
23822412
heroes = ['spiderman', 'superman'],

0 commit comments

Comments
 (0)