forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuple.ts
More file actions
40 lines (34 loc) · 1.03 KB
/
Copy pathtuple.ts
File metadata and controls
40 lines (34 loc) · 1.03 KB
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
export function isTuple<T, N extends number>(arr: T[], len: N): arr is Tuple<T, N> {
return arr.length === len
}
/**
* Add tuple
*/
export const addVector = <L extends number>(a: Vector<L>, toAdd: Vector<L> | number): Vector<L> => {
const arr = a as number[]
if (typeof toAdd === 'number') {
return arr.map(v => v + toAdd) as Vector<L>
} else {
const b = toAdd as number[]
return arr.map((v, i) => v + (b[i] ?? 0)) as Vector<L>
}
}
/**
* Subtract tuple
*/
export const subVector = <L extends number>(a: Vector<L>, toSub: Vector<L> | number): Vector<L> => {
const arr = a as number[]
if (typeof toSub === 'number') {
return arr.map(v => v - toSub) as Vector<L>
} else {
const b = toSub as number[]
return arr.map((v, i) => v - (b[i] ?? 0)) as Vector<L>
}
}
/**
* Multiple tuple
*/
export const multiTuple = <L extends number>(a: Vector<L>, multiFactor: number): Vector<L> => {
const arr = a as number[]
return arr.map(v => v * multiFactor) as Vector<L>
}