forked from graphql/graphql-js
/
isIteratableObject.js
28 lines (27 loc) · 940 Bytes
/
isIteratableObject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* Returns true if the provided object is an Object (i.e. not a string literal)
* and implements the Iterator protocol.
*
* This may be used in place of [Array.isArray()][isArray] to determine if
* an object should be iterated-over e.g. Array, Map, Set, Int8Array,
* TypedArray, etc. but excludes string literals.
*
* @example
*
* isIteratableObject([ 1, 2, 3 ]) // true
* isIteratableObject(new Map()) // true
* isIteratableObject('ABC') // false
* isIteratableObject({ key: 'value' }) // false
* isIteratableObject({ length: 1, 0: 'Alpha' }) // false
*/
declare function isIteratableObject(
value: mixed,
// $FlowFixMe[invalid-in-rhs]
): boolean %checks(value instanceof Iterable);
// eslint-disable-next-line no-redeclare
export function isIteratableObject(maybeIteratable: mixed): boolean {
return (
typeof maybeIteratable === 'object' &&
typeof maybeIteratable?.[Symbol.iterator] === 'function'
);
}