-
Notifications
You must be signed in to change notification settings - Fork 22
/
pattern-clause.ts
42 lines (34 loc) · 1.21 KB
/
pattern-clause.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
import { Clause } from '../clause';
import { join, reduce, map, assign, castArray, isArray } from 'lodash';
import { Pattern } from './pattern';
export interface PatternOptions {
useExpandedConditions?: boolean;
}
export type PatternCollection = Pattern | Pattern[] | Pattern[][];
export class PatternClause extends Clause {
protected patterns: Pattern[][];
constructor(
patterns: PatternCollection,
options: PatternOptions = { useExpandedConditions: false },
) {
super();
const defaultOptions = {
useExpandedConditions: true,
};
const { useExpandedConditions } = assign(defaultOptions, options);
// Ensure patterns is a two dimensional array.
const arr = castArray<Pattern | Pattern[]>(patterns);
this.patterns = (arr[0] instanceof Array ? arr : [arr]) as Pattern[][];
// Add child patterns as clauses
this.patterns.forEach(arr => arr.forEach((pat) => {
pat.setExpandedConditions(useExpandedConditions);
pat.useParameterBag(this.parameterBag);
}));
}
build() {
const patternStrings = map(this.patterns, (pattern) => {
return reduce(pattern, (str, clause) => str + clause.build(), '');
});
return join(patternStrings, ', ');
}
}