You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
827 B
TypeScript

// Copyright (C) 2020 Intel Corporation
//
// SPDX-License-Identifier: MIT
export default function range(x: number, y?: number): number[] {
if (typeof x !== 'undefined' && typeof y !== 'undefined') {
if (typeof x !== 'number' && typeof y !== 'number') {
throw new Error(`Range() expects number arguments. Got ${typeof x}, ${typeof y}`);
}
if (x >= y) {
throw new Error(`Range() expects the first argument less or equal than the second. Got ${x}, ${y}`);
}
return Array.from(Array(y - x), (_: number, i: number) => i + x);
}
if (typeof x !== 'undefined') {
if (typeof x !== 'number') {
throw new Error(`Range() expects number arguments. Got ${typeof x}`);
}
return [...Array(x).keys()];
}
return [];
}