args-to-arr
Version:
converts arguments or any other array-like object into an array starting from specific index.
120 lines (90 loc) • 2.39 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.toArray = factory());
}(this, function () { 'use strict';
/**
* isArray
*/
var isArray = Array.isArray;
/**
* toString
*/
var str = Object.prototype.toString;
/**
* Whether or not the given `val`
* is an array.
*
* example:
*
* isArray([]);
* // > true
* isArray(arguments);
* // > false
* isArray('');
* // > false
*
* @param {mixed} val
* @return {bool}
*/
var isArray_1 = isArray || function (val) {
return !! val && '[object Array]' == str.call(val);
};
var isWindow = function (obj) {
if (obj == null) {
return false;
}
var o = Object(obj);
return o === o.window;
};
var isFunction_1 = isFunction;
var toString = Object.prototype.toString;
function isFunction (fn) {
var string = toString.call(fn);
return string === '[object Function]' ||
(typeof fn === 'function' && string !== '[object RegExp]') ||
(typeof window !== 'undefined' &&
// IE8 and below
(fn === window.setTimeout ||
fn === window.alert ||
fn === window.confirm ||
fn === window.prompt))
}
var isArrayLike = function (obj) {
if (!obj) {
return false;
}
if (isArray_1(obj)) {
return true;
}
if (isFunction_1(obj) || isWindow(obj)) {
return false;
}
obj = Object(obj);
var length = 'length' in obj && obj.length;
if (obj.nodeType === 1 && length) {
return true;
}
return length === 0 ||
typeof length === 'number' && length > 0 && ( length - 1 ) in obj;
};
function toArray(args, start) {
if (!isArrayLike(args)) {
throw new TypeError((args + " can't be converted to array."));
}
if (start == null) {
start = 0;
}
if (typeof start !== "number") {
throw new TypeError((start + " is not a number."));
}
var len = args.length;
var result = new Array(len - start);
for (var i = start; i < len; i++) {
result[i - start] = args[i];
}
return result;
}
return toArray;
}));
//# sourceMappingURL=args-to-arr.umd.js.map