mout
Version:
Modular Utilities
36 lines (33 loc) • 930 B
text/typescript
import kindOf from './kindOf';
import GLOBAL from './GLOBAL';
/**
* Convert array-like object into array
*/
function toArray(val) {
const ret = [];
const kind = kindOf(val);
let n;
if (val != null) {
if (
val.length == null ||
kind === 'String' ||
kind === 'Function' ||
kind === 'RegExp' ||
val === GLOBAL
) {
// string, regexp, function have .length but user probably just want
// to wrap value into an array..
ret[ret.length] = val;
} else {
// window returns true on isObject in IE7 and may have length
// property. `typeof NodeList` returns `function` on Safari so
// we can't use it (#58)
n = val.length;
while (n--) {
ret[n] = val[n];
}
}
}
return ret;
}
export default toArray;