-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathderive.ts
53 lines (49 loc) · 1.36 KB
/
derive.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
41
42
43
44
45
46
47
48
49
50
51
52
53
/*!
* Copyright (c) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project.
*/
import type { DeriveArgs } from '@datashaper/schema'
import { MathOperator } from '@datashaper/schema'
import { escape } from 'arquero'
import type { ColumnTableStep } from './util/factories.js'
import { stepVerbFactory } from './util/factories.js'
/**
* Executes an arquero derive.
* This basically just supports math operations between two columns.
*/
export const deriveStep: ColumnTableStep<DeriveArgs> = (
input,
{ column1, column2, operator, to },
) => {
// eslint-disable-next-line
const func = escape((d: any) => {
const l = d[column1]
const r = d[column2]
let result
switch (operator) {
case MathOperator.Add:
// anything besides numbers should be reasonably concatenated with +
// so skip the NaN check at the end
if (typeof l === 'number' && typeof r === 'number') {
result = l + r
} else {
return `${l}${r}`
}
break
case MathOperator.Subtract:
result = l - r
break
case MathOperator.Multiply:
result = l * r
break
case MathOperator.Divide:
result = l / r
break
default:
throw new Error(`Unsupported operator: [${operator}]`)
}
return isNaN(result) ? null : result
})
return input.derive({ [to]: func })
}
export const derive = stepVerbFactory(deriveStep)