-
Notifications
You must be signed in to change notification settings - Fork 37
/
filter.ts
46 lines (41 loc) · 1.28 KB
/
filter.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
/*!
* Copyright (c) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project.
*/
import { Predicate } from '../interfaces'
import { DatasetTransform } from './interfaces'
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const { filter: vegaFilter } = require('vega-transforms')
/**
* The filter transform removes objects from a data stream based on a provided filter expression.
*/
export interface FilterBuilder extends DatasetTransform {
/**
* The filter to use for limiting results. If the expression evaluates to
* false, the data object will be filtered.
*
* @param value
*/
filter(value: Predicate<any>): FilterBuilder
}
export class FilterBuilderImpl implements FilterBuilder {
private filterValue: Predicate<any> | undefined
public filter(value: Predicate<any>): FilterBuilder {
this.filterValue = value
return this
}
public build(df: any, from: any): FilterBuilder {
if (!this.filterValue) {
throw new Error('filter predicate must be defined')
}
const spec: any = {
expr: this.filterValue,
pulse: from,
}
const crossNode = df.add(vegaFilter, spec)
return crossNode
}
}
export function filter(predicate: Predicate<any>): FilterBuilder {
return new FilterBuilderImpl().filter(predicate)
}