This repository was archived by the owner on Mar 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathfilter.ts
More file actions
1013 lines (966 loc) · 26.1 KB
/
filter.ts
File metadata and controls
1013 lines (966 loc) · 26.1 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import arrify = require('arrify');
import * as escapeStringRegexp from 'escape-string-regexp';
import * as is from 'is';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const isUtf8 = require('is-utf8');
import {Mutation} from './mutation';
/**
* @private
*/
export class FilterError extends Error {
constructor(filter: string) {
super();
this.name = 'FilterError';
this.message = `Unknown filter: ${filter}.`;
}
}
export interface Column {
name?: string | RegExp;
cellLimit?: number;
start?: BoundData;
end?: {};
family?: BoundData;
}
export interface BoundData {
inclusive?: boolean;
value?: {};
}
export interface Time {
start: Date | number;
end: Date | number;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type RawFilter = any;
export interface Condition {
pass: {};
fail: {};
test: {};
}
export interface Row {
cellLimit?: number;
sample?: string;
cellOffset?: number;
key?: string;
}
export interface ValueFilter {
value?: string;
start?: BoundData;
end?: BoundData;
strip?: boolean;
}
/**
* A filter takes a row as input and produces an alternate view of the row based
* on specified rules. For example, a row filter might trim down a row to
* include just the cells from columns matching a given regular expression, or
* might return all the cells of a row but not their values. More complicated
* filters can be composed out of these components to express requests such as,
* "within every column of a particular family, give just the two most recent
* cells which are older than timestamp X."
*
* There are two broad categories of filters (true filters and transformers),
* as well as two ways to compose simple filters into more complex ones
* ({@link Filter#interleave}). They work as follows:
*
* True filters alter the input row by excluding some of its cells wholesale
* from the output row. An example of a true filter is the
* {@link Filter#value} filter, which excludes cells whose values
* don't match the specified pattern. All regex true filters use RE2 syntax
* (https://github.com/google/re2/wiki/Syntax) and are evaluated as full
* matches. An important point to keep in mind is that RE2(.) is equivalent by
* default to RE2([^\n]), meaning that it does not match newlines. When
* attempting to match an arbitrary byte, you should therefore use the escape
* sequence '\C', which may need to be further escaped as '\\C' in your client
* language.
*
* Transformers alter the input row by changing the values of some of its
* cells in the output, without excluding them completely. Currently, the only
* supported transformer is the {@link Filter#value} `strip` filter,
* which replaces every cell's value with the empty string.
*
* The total serialized size of a filter message must not
* exceed 4096 bytes, and filters may not be nested within each other to a depth
* of more than 20.
*
* Use the following table for the various examples found throughout the
* filter documentation.
*
* | Row Key | follows:gwashington | follows:jadams | follows:tjefferson |
* | ----------- |:-------------------:|:--------------:|:------------------:|
* | gwashington | | 1 | |
* | tjefferson | 1 | 1 | |
* | jadams | 1 | | 1 |
*
* @class
*/
export class Filter {
filters_: Array<{[index: string]: {}}> = [];
constructor() {
this.filters_ = [];
}
/**
* @throws TypeError
*
* Transforms Arrays into a simple regular expression for matching multiple
* values.
*
* @param {regex|string|string[]} regex Either a plain regex, a regex in
* string form or an array of strings.
*
* @returns {string}
*
* @example
* ```
* const regexString = Filter.convertToRegExpString(['a', 'b', 'c']);
* // => '(a|b|c)'
* ```
*/
static convertToRegExpString(
regex:
| RegExp
| RegExp[]
| string
| string[]
| Buffer
| Buffer[]
| number
| number[],
): string | Buffer {
if (is.regexp(regex)) {
return regex.toString().replace(/^\/|\/$/g, '');
}
if (Array.isArray(regex)) {
return `(${(regex as string[])
.map(Filter.convertToRegExpString)
.join('|')})`;
}
if (typeof regex === 'string') {
return regex;
}
if (typeof regex === 'number') {
return regex.toString();
}
if (Buffer.isBuffer(regex)) {
const encodingToUse = isUtf8(regex) ? 'utf8' : 'binary';
const regexToEscape = regex.toString(encodingToUse);
const escapedString = escapeStringRegexp(regexToEscape);
return Buffer.from(escapedString, encodingToUse);
}
throw new TypeError("Can't convert to RegExp String from unknown type.");
}
/**
* Creates a range object. All bounds default to inclusive.
*
* @param {?object|string} start Lower bound value.
* @param {?object|string} end Upper bound value.
* @param {string} key Key used to create range value keys.
*
* @returns {object}
*
* @example
* ```
* const {Bigtable} = require('@google-cloud/bigtable');
* const Filter = Bigtable.Filter;
*
* const range = Filter.createRange('value1', 'value2', 'Test');
* // {
* // startTestInclusive: new Buffer('value1'),
* // endTestExclusive: new Buffer('value2')
* // }
*
* //-
* // It's also possible to pass in objects to specify inclusive/exclusive
* // bounds.
* //-
* const upperBound = {
* value: 'value3',
* inclusive: false
* };
*
* const range = Filter.createRange(upperBound, null, 'Test2');
* // => {
* // startTest2Exclusive: 'value3'
* // }
* ```
*/
static createRange(
start: BoundData | null,
end: BoundData | null,
key: string,
) {
const range = {};
if (start) {
Object.assign(range, createBound('start', start, key));
}
if (end) {
Object.assign(range, createBound('end', end, key));
}
return range;
function createBound(boundName: string, boundData: BoundData, key: string) {
const isInclusive = boundData.inclusive !== false;
const boundKey = boundName + key + (isInclusive ? 'Closed' : 'Open');
const bound = {} as {[index: string]: {}};
bound[boundKey] = Mutation.convertToBytes(boundData.value || boundData);
return bound;
}
}
/**
* @throws FilterError
*
* Turns filters into proto friendly format.
*
* @param {object[]} filters The list of filters to be parsed.
*
* @returns {object}
*
* @example
* ```
* const filter = Filter.parse([
* {
* family: 'my-family',
* }, {
* column: 'my-column'
* }
* ]);
* // {
* // chain: {
* // filters: [
* // {
* // familyNameRegexFilter: 'my-family'
* // },
* // {
* // columnQualifierRegexFilter: 'my-column'
* // }
* // ]
* // }
* // }
* ```
*/
static parse(filters: RawFilter[] | RawFilter) {
interface Fn {
[index: string]: Function;
}
const filter = new Filter();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
arrify(filters).forEach((filterObj: any) => {
const key = Object.keys(filterObj)[0];
if (typeof (filter as {} as Fn)[key] !== 'function') {
throw new FilterError(key);
}
(filter as {} as Fn)[key](filterObj[key]);
});
return filter.toProto();
}
/**
* Sets passAllFilter or blockAllFilter
*
* @param {boolean} pass Whether to passAllFilter or blockAllFilter
*
* Assign true for enabling passAllFilter and false for enabling blockAllFilter
*
* @example
* ```
* //-
* // Matches all cells, regardless of input. Functionally equivalent to
* // leaving `filter` unset, but included for completeness.
* //-
* const filter = {
* all: true
* };
*
* //-
* // Does not match any cells, regardless of input. Useful for temporarily
* // disabling just part of a filter.
* //-
* const filter = {
* all: false
* };
* ```
*/
all(pass: boolean): void {
const filterName = pass ? 'passAllFilter' : 'blockAllFilter';
this.set(filterName, true);
}
/**
* Matches only cells from columns whose qualifiers satisfy the given RE2
* regex.
* @param {?regex|string|object} column Matching Column to filter with
*
* Note that, since column qualifiers can contain arbitrary bytes, the '\C'
* escape sequence must be used if a true wildcard is desired. The '.'
* character will not match the new line character '\n', which may be
* present in a binary qualifier.
*
* @example
* ```
* //-
* // Using the following filter, we would retrieve the `tjefferson` and
* // `gwashington` columns.
* //-
* const filter = [
* {
* column: /[a-z]+on$/
* }
* ];
*
* //-
* // You can also provide a string (optionally containing regexp characters)
* // for simple column filters.
* //-
* const filter = [
* {
* column: 'gwashington'
* }
* ];
*
* //-
* // Or you can provide an array of strings if you wish to match against
* // multiple columns.
* //-
* const filter = [
* {
* column: [
* 'gwashington',
* 'tjefferson'
* ]
* }
* ];
*
* //-
* // If you wish to use additional column filters, consider using the following
* // syntax.
* //-
* const filter = [
* {
* column: {
* name: 'gwashington'
* }
* }
* ];
*
*
* //-
* // <h4>Column Cell Limits</h4>
* //
* // Matches only the most recent number of versions within each column. For
* // example, if the `versions` is set to 2, this filter would only match
* // columns updated at the two most recent timestamps.
* //
* // If duplicate cells are present, as is possible when using an
* // {@link Filter#interleave} filter, each copy of the cell is
* // counted separately.
* //-
* const filter = [
* {
* column: {
* cellLimit: 2
* }
* }
* ];
*
* //-
* // <h4>Column Ranges</h4>
* //
* // Specifies a contiguous range of columns within a single column family.
* // The range spans from <column_family>:<start_qualifier> to
* // <column_family>:<end_qualifier>, where both bounds can be either
* // inclusive or exclusive. By default both are inclusive.
* //
* // When the `start` bound is omitted it is interpreted as an empty string.
* // When the `end` bound is omitted it is interpreted as Infinity.
* //-
* const filter = [
* {
* column: {
* family: 'follows',
* start: 'gwashington',
* end: 'tjefferson'
* }
* }
* ];
*
* //-
* // By default, both the `start` and `end` bounds are inclusive. You can
* // override these by providing an object explicity stating whether or not it
* // is `inclusive`.
* //-
* const filter = [
* {
* column: {
* family: 'follows',
* start: {
* value: 'gwashington',
* inclusive: false
* },
* end: {
* value: 'jadams',
* inclusive: false
* }
* }
* }
* ];
* ```
*/
column(column: RegExp | string | {}): void {
let col: Column;
if (!is.object(column)) {
col = {
name: column as string,
};
} else {
col = column as Column;
}
if (col.name) {
const name = Mutation.convertToBytes(
Filter.convertToRegExpString(col.name),
);
this.set('columnQualifierRegexFilter', name);
}
if (typeof col.cellLimit === 'number') {
this.set('cellsPerColumnLimitFilter', col.cellLimit!);
}
if (col.start || col.end) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const range: any = Filter.createRange(col.start!, col.end!, 'Qualifier');
range.familyName = col.family;
this.set('columnRangeFilter', range);
}
}
/**
* A filter which evaluates one of two possible filters, depending on
* whether or not a `test` filter outputs any cells from the input row.
*
* IMPORTANT NOTE: The `test` filter does not execute atomically with the
* pass and fail filters, which may lead to inconsistent or unexpected
* results. Additionally, condition filters have poor performance, especially
* when filters are set for the fail condition.
*
* @param {object} condition Condition to filter.
*
* @example
* ```
* //-
* // In the following example we're creating a filter that will check if
* // `gwashington` follows `tjefferson`. If he does, we'll get all of the
* // `gwashington` data. If he does not, we'll instead return all of the
* // `tjefferson` data.
* //-
* const filter = [
* {
* condition: {
* // If `test` outputs any cells, then `pass` will be evaluated on the
* // input row. Otherwise `fail` will be evaluated.
* test: [
* {
* row: 'gwashington'
* },
* {
* family: 'follows'
* },
* {
* column: 'tjefferson'
* }
* ],
*
* // If omitted, no results will be returned in the true case.
* pass: [
* {
* row: 'gwashington'
* }
* ],
*
* // If omitted, no results will be returned in the false case.
* fail: [
* {
* row: 'tjefferson'
* }
* ]
* }
* }
* ];
* ```
*/
condition(condition: Condition) {
this.set('condition', {
predicateFilter: Filter.parse(condition.test),
trueFilter: Filter.parse(condition.pass),
falseFilter: Filter.parse(condition.fail),
});
}
/**
* Matches only cells from columns whose families satisfy the given RE2
* regex. For technical reasons, the regex must not contain the ':'
* character, even if it is not being used as a literal.
* Note that, since column families cannot contain the new line character
* '\n', it is sufficient to use '.' as a full wildcard when matching
* column family names.
*
* @param {regex} family Expression to filter family
*
* @example
* ```
* const filter = [
* {
* family: 'follows'
* }
* ];
* ```
*/
family(
family:
| RegExp
| string
| number
| Buffer
| RegExp[]
| string[]
| number[]
| Buffer[],
): void {
const f = Filter.convertToRegExpString(family);
this.set('familyNameRegexFilter', f);
}
/**
* Applies several filters to the data in parallel and combines the results.
*
* @param {object} filters The elements of "filters" all process a copy of the input row, and the
* results are pooled, sorted, and combined into a single output row.
* If multiple cells are produced with the same column and timestamp,
* they will all appear in the output row in an unspecified mutual order.
* All interleaved filters are executed atomically.
*
* @example
* ```
* //-
* // In the following example, we're creating a filter that will retrieve
* // results for entries that were either created between December 17th, 2015
* // and March 22nd, 2016 or entries that have data for `follows:tjefferson`.
* //-
* const filter = [
* {
* interleave: [
* [
* {
* time: {
* start: new Date('December 17, 2015'),
* end: new Date('March 22, 2016')
* }
* }
* ],
* [
* {
* family: 'follows'
* },
* {
* column: 'tjefferson'
* }
* ]
* ]
* }
* ];
* ```
*/
interleave(filters: RawFilter[]): void {
this.set('interleave', {
filters: filters.map(Filter.parse),
});
}
/**
* Applies the given label to all cells in the output row. This allows
* the client to determine which results were produced from which part of
* the filter.
*
* @param {string} label Label to determine filter point
* Values must be at most 15 characters in length, and match the RE2
* pattern [a-z0-9\\-]+
*
* Due to a technical limitation, it is not currently possible to apply
* multiple labels to a cell. As a result, a chain filter may have no more than
* one sub-filter which contains a apply label transformer. It is okay for
* an {@link Filter#interleave} to contain multiple apply label
* transformers, as they will be applied to separate copies of the input. This
* may be relaxed in the future.
*
* @example
* ```
* const filter = {
* label: 'my-label'
* };
* ```
*/
label(label: string): void {
this.set('applyLabelTransformer', label);
}
/**
* Matches only cells from rows whose keys satisfy the given RE2 regex. In
* other words, passes through the entire row when the key matches, and
* otherwise produces an empty row.
*
* @param {?regex|string|string[]} row Row format to Filter
*
* Note that, since row keys can contain arbitrary bytes, the '\C' escape
* sequence must be used if a true wildcard is desired. The '.' character
* will not match the new line character '\n', which may be present in a
* binary key.
*
* @example
* ```
* //-
* // In the following example we'll use a regular expression to match all
* // row keys ending with the letters "on", which would then yield
* // `gwashington` and `tjefferson`.
* //-
* const filter = [
* {
* row: /[a-z]+on$/
* }
* ];
*
* //-
* // You can also provide a string (optionally containing regexp characters)
* // for simple key filters.
* //-
* const filter = [
* {
* row: 'gwashington'
* }
* ];
*
* //-
* // Or you can provide an array of strings if you wish to match against
* // multiple keys.
* //-
* const filter = [
* {
* row: [
* 'gwashington',
* 'tjefferson'
* ]
* }
* ];
*
* //-
* // If you wish to use additional row filters, consider using the following
* // syntax.
* //-
* const filter = [
* {
* row: {
* key: 'gwashington'
* }
* }
* ];
*
* //-
* // <h4>Row Samples</h4>
* //
* // Matches all cells from a row with probability p, and matches no cells
* // from the row with probability 1-p.
* //-
* const filter = [
* {
* row: {
* sample: 1
* }
* }
* ];
*
* //-
* // <h4>Row Cell Offsets</h4>
* //
* // Skips the first N cells of each row, matching all subsequent cells.
* // If duplicate cells are present, as is possible when using an
* // {@link Filter#interleave}, each copy of the cell is counted
* // separately.
* //-
* const filter = [
* {
* row: {
* cellOffset: 2
* }
* }
* ];
*
* //-
* // <h4>Row Cell Limits</h4>
* //
* // Matches only the first N cells of each row.
* // If duplicate cells are present, as is possible when using an
* // {@link Filter#interleave}, each copy of the cell is counted
* // separately.
* //-
* const filter = [
* {
* row: {
* cellLimit: 4
* }
* }
* ];
* ```
*/
row(row: Row | string | RegExp | string[]): void {
let r: Row;
if (!is.object(row)) {
r = {
key: row as string,
};
} else {
r = row as Row;
}
if (r.key) {
const key = Mutation.convertToBytes(Filter.convertToRegExpString(r.key));
this.set('rowKeyRegexFilter', key);
}
if (r.sample) {
this.set('rowSampleFilter', r.sample);
}
if (typeof r.cellOffset === 'number') {
this.set('cellsPerRowOffsetFilter', r.cellOffset!);
}
if (typeof r.cellLimit === 'number') {
this.set('cellsPerRowLimitFilter', r.cellLimit!);
}
}
/**
* Stores a filter object.
*
* @param {string} key Filter name.
* @param {*} value Filter value.
*/
set(key: string, value: {}): void {
this.filters_.push({
[key]: value,
});
}
/**
* This filter is meant for advanced use only. Hook for introspection into the
* filter. Outputs all cells directly to the output of the read rather than to
* any parent filter.
* Despite being excluded by the qualifier filter, a copy of every cell that
* reaches the sink is present in the final result.
* As with an {@link Filter#interleave} filter, duplicate cells are
* possible, and appear in an unspecified mutual order.
*
* Cannot be used within {@link Filter#condition} filter.
*
* @param {boolean} sink
*
* @example
* ```
* //-
* // Using the following filter, a copy of every cell that reaches the sink is
* // present in the final result, despite being excluded by the qualifier
* // filter
* //-
* const filter = [
* {
* family: 'follows'
* },
* {
* interleave: [
* [
* {
* all: true
* }
* ],
* [
* {
* label: 'prezzy'
* },
* {
* sink: true
* }
* ]
* ]
* },
* {
* column: 'gwashington'
* }
* ];
*
* //-
* // As with an {@link Filter#interleave} filter, duplicate cells
* // are possible, and appear in an unspecified mutual order. In this case we
* // have a duplicates with multiple `gwashington` columns because one copy
* // passed through the {@link Filter#all} filter while the other was
* // passed through the {@link Filter#label} and sink. Note that one
* // copy has label "prezzy" while the other does not.
* //-
* ```
*/
sink(sink: boolean): void {
this.set('sink', sink);
}
/**
* Matches only cells with timestamps within the given range.
*
* @param {object} time Start and End time Object
*
* @example
* ```
* const filter = [
* {
* time: {
* start: new Date('December 17, 2006 03:24:00'),
* end: new Date()
* }
* }
* ];
* ```
*/
time(time: Time): void {
const range = Mutation.createTimeRange(time.start, time.end);
this.set('timestampRangeFilter', range);
}
/**
* If we detect multiple filters, we'll assume it's a chain filter and the
* execution of the filters will be the order in which they were specified.
*/
toProto(): null | {} {
if (!this.filters_.length) {
return null;
}
if (this.filters_.length === 1) {
return this.filters_[0];
}
return {
chain: {
filters: this.filters_,
},
};
}
/**
* Matches only cells with values that satisfy the given regular expression.
* Note that, since cell values can contain arbitrary bytes, the '\C' escape
* sequence must be used if a true wildcard is desired. The '.' character
* will not match the new line character '\n', which may be present in a
* binary value.
*
* @param {?string|string[]|object} value Value to filter cells
*
* @example
* ```
* const filter = [
* {
* value: /[0-9]/
* }
* ];
*
* //-
* // You can also provide a string (optionally containing regexp characters)
* // for value filters.
* //-
* const filter = [
* {
* value: '1'
* }
* ];
*
* //-
* // You can also provide an array of strings if you wish to match against
* // multiple values.
* //-
* const filter = [
* {
* value: ['1', '9']
* }
* ];
*
* //-
* // Or you can provide a Buffer or an array of Buffers if you wish to match
* // against specfic binary value(s).
* //-
* const userInputedFaces = [Buffer.from('.|.'), Buffer.from(':-)')];
* const filter = [
* {
* value: userInputedFaces
* }
* ];
*
* //-
* // <h4>Value Ranges</h4>
* //
* // Specifies a contiguous range of values.
* //
* // When the `start` bound is omitted it is interpreted as an empty string.
* // When the `end` bound is omitted it is interpreted as Infinity.
* //-
* const filter = [
* {
* value: {
* start: '1',
* end: '9'
* }
* }
* ];
*
* //-
* // By default, both the `start` and `end` bounds are inclusive. You can
* // override these by providing an object explicity stating whether or not it
* // is `inclusive`.
* //-
* const filter = [
* {
* value: {
* start: {
* value: '1',
* inclusive: false
* },
* end: {
* value: '9',
* inclusive: false
* }
* }
* }
* ];
*
* //-
* // <h4>Strip Values</h4>
* //
* // Replaces each cell's value with an emtpy string.
* //-
* const filter = [
* {
* value: {
* strip: true
* }
* }
* ];
* ```
*/
value(value: string | string[] | ValueFilter): void {
let v: ValueFilter;
if (!is.object(value)) {
v = {
value: value as string,
};
} else {
v = value as ValueFilter;
}
if (v.value) {
const valueReg = Mutation.convertToBytes(
Filter.convertToRegExpString(v.value),
);