args-to-arr
Version:
converts arguments or any other array-like object into an array starting from specific index.
29 lines (19 loc) • 538 B
text/typescript
import isArrayLike from "is-array-like";
function toArray<T>(args: ArrayLike<T>, start?: number | null): T[] {
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.`);
}
const len = args.length;
const result = new Array(len - start);
for (let i = start; i < len; i++) {
result[i - start] = args[i];
}
return result;
}
export default toArray;