-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrange.ts
40 lines (33 loc) · 979 Bytes
/
range.ts
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
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import curryN from './utils/curry_n.ts';
import { isInteger } from './utils/is.ts';
import type { PH } from './utils/types.ts';
// @types
type Range_2 = (to: number) => number[];
type Range_1 = (from: number) => number[];
type Range =
& ((from: number) => Range_2)
& ((from: PH, to: number) => Range_1)
& ((from: number, to: number) => number[]);
function _range(from: number, to: number) {
if (
isNaN(from) ||
isNaN(to) ||
!isInteger(from) ||
!isInteger(to)
) {
throw new Error(
`The arguments should be finite integer values but got\n\tfrom: ${from}\n\tto: ${to}`,
);
}
const result = [];
const l = to - from + 1;
if (l <= 0) return [];
result.length = l;
for (let i = 0; i < l; i++) {
result[i] = from++;
}
return result;
}
/** Returns a list of numbers from `from` to `to` **both inclusive**. */
export const range = curryN(2, _range) as Range;