-
Notifications
You must be signed in to change notification settings - Fork 12
/
getOutputArray.ts
36 lines (33 loc) · 973 Bytes
/
getOutputArray.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
import { NumberArray } from 'cheminfo-types';
import { isAnyArray } from 'is-any-array';
export function getOutputArray<T extends NumberArray = Float64Array>(
output: T,
length: number,
): T;
export function getOutputArray(output: undefined, length: number): Float64Array;
export function getOutputArray<T extends NumberArray = Float64Array>(
output: T | undefined,
length: number,
): T;
/**
* This function
* @param output - undefined or a new array
* @param length - length of the output array
* @returns
*/
export function getOutputArray(
output: NumberArray | undefined,
length: number,
): NumberArray {
if (typeof output !== 'undefined') {
if (!isAnyArray(output)) {
throw new TypeError('output option must be an array if specified');
}
if (output.length !== length) {
throw new TypeError('the output array does not have the correct length');
}
return output;
} else {
return new Float64Array(length);
}
}