-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (42 loc) · 1005 Bytes
/
index.js
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
41
42
export const createTransform = (
transform,
options,
) => {
const {
from,
to,
samples,
...extras
} = ((options && typeof options === 'object') ? options: {});
if (isNaN(from)) {
throw new Error(
`Expected number from, encountered ${from}.`,
);
} else if (isNaN(to)) {
throw new Error(
`Expected number to, encountered ${to}.`,
);
} else if (isNaN(samples) || samples <= 0) {
throw new Error(
`Expected positive number samples, encountered ${samples}.`,
);
} else if (from > to) {
throw new Error(
`The specified range [from, to] must increase in a positive direction.`,
);
}
let val = from;
const inc = (to - from) / samples;
const inputRange = [...Array(samples)];
const outputRange = [...Array(samples)];
for (let i = 0; i < samples; i += 1) {
inputRange[i] = val;
outputRange[i] = transform(val, from, to);
val += inc;
}
return {
inputRange,
outputRange,
...extras,
};
}