|
1 | 1 | import { evalExp } from '../render/syntax'
|
2 |
| -import Filter from './filter/filter' |
| 2 | +import { FilterArgs, Filter } from './filter/filter' |
3 | 3 | import Scope from '../scope/scope'
|
4 | 4 |
|
5 |
| -enum ParseState { |
6 |
| - INIT = 0, |
7 |
| - FILTER_NAME = 1, |
8 |
| - FILTER_ARG = 2 |
9 |
| -} |
10 |
| - |
11 |
| -export default class { |
12 |
| - initial: any |
| 5 | +export default class Value { |
| 6 | + private strictFilters: boolean |
| 7 | + initial: string |
13 | 8 | filters: Array<Filter> = []
|
14 | 9 |
|
15 | 10 | /**
|
16 | 11 | * @param str value string, like: "i have a dream | truncate: 3
|
17 | 12 | */
|
18 | 13 | constructor (str: string, strictFilters: boolean) {
|
19 |
| - let buffer = '' |
20 |
| - let quoted = '' |
21 |
| - let state = ParseState.INIT |
22 |
| - let sealed = false |
23 |
| - |
24 |
| - let filterName = '' |
25 |
| - let filterArgs: string[] = [] |
26 |
| - |
27 |
| - for (let i = 0; i < str.length; i++) { |
28 |
| - if (quoted) { |
29 |
| - if (str[i] === quoted) { |
30 |
| - quoted = '' |
31 |
| - sealed = true |
32 |
| - } |
33 |
| - buffer += str[i] |
34 |
| - } else if (/\s/.test(str[i])) { |
35 |
| - if (!buffer) continue |
36 |
| - else sealed = true |
37 |
| - } else if (str[i] === '|') { |
38 |
| - if (state === ParseState.INIT) { |
39 |
| - this.initial = buffer |
40 |
| - } else { |
41 |
| - if (state === ParseState.FILTER_NAME) filterName = buffer |
42 |
| - else filterArgs.push(buffer) |
43 |
| - this.filters.push(new Filter(filterName, filterArgs, strictFilters)) |
44 |
| - filterName = '' |
45 |
| - filterArgs = [] |
46 |
| - } |
47 |
| - state = ParseState.FILTER_NAME |
48 |
| - buffer = '' |
49 |
| - sealed = false |
50 |
| - } else if (state === ParseState.FILTER_NAME && str[i] === ':') { |
51 |
| - filterName = buffer |
52 |
| - state = ParseState.FILTER_ARG |
53 |
| - buffer = '' |
54 |
| - sealed = false |
55 |
| - } else if (state === ParseState.FILTER_ARG && str[i] === ',') { |
56 |
| - filterArgs.push(buffer) |
57 |
| - buffer = '' |
58 |
| - sealed = false |
59 |
| - } else if (sealed) continue |
60 |
| - else { |
61 |
| - if ((str[i] === '"' || str[i] === "'") && !quoted) quoted = str[i] |
62 |
| - buffer += str[i] |
| 14 | + const tokens = Value.tokenize(str) |
| 15 | + this.strictFilters = strictFilters |
| 16 | + this.initial = tokens[0] |
| 17 | + this.parseFilters(tokens, 1) |
| 18 | + } |
| 19 | + private parseFilters (tokens: string[], begin: number) { |
| 20 | + let i = begin |
| 21 | + while (i < tokens.length) { |
| 22 | + if (tokens[i] !== '|') { |
| 23 | + i++ |
| 24 | + continue |
63 | 25 | }
|
| 26 | + const j = ++i |
| 27 | + while (i < tokens.length && tokens[i] !== '|') i++ |
| 28 | + this.parseFilter(tokens, j, i) |
64 | 29 | }
|
65 |
| - |
66 |
| - if (buffer) { |
67 |
| - if (state === ParseState.INIT) this.initial = buffer |
68 |
| - else if (state === ParseState.FILTER_NAME) this.filters.push(new Filter(buffer, [], strictFilters)) |
69 |
| - else { |
70 |
| - filterArgs.push(buffer) |
71 |
| - this.filters.push(new Filter(filterName, filterArgs, strictFilters)) |
| 30 | + } |
| 31 | + private parseFilter (tokens: string[], begin: number, end: number) { |
| 32 | + const name = tokens[begin] |
| 33 | + const args: FilterArgs = [] |
| 34 | + let argName, argValue |
| 35 | + for (let i = begin + 1; i < end + 1; i++) { |
| 36 | + if (i === end || tokens[i] === ',') { |
| 37 | + if (argName || argValue) { |
| 38 | + args.push(argName ? [argName, argValue] : <string>argValue) |
| 39 | + } |
| 40 | + argValue = argName = undefined |
| 41 | + } else if (tokens[i] === ':') { |
| 42 | + argName = argValue |
| 43 | + argValue = undefined |
| 44 | + } else if (argValue === undefined) { |
| 45 | + argValue = tokens[i] |
72 | 46 | }
|
73 | 47 | }
|
| 48 | + this.filters.push(new Filter(name, args, this.strictFilters)) |
74 | 49 | }
|
75 | 50 | value (scope: Scope) {
|
76 | 51 | return this.filters.reduce(
|
77 | 52 | (prev, filter) => filter.render(prev, scope),
|
78 | 53 | evalExp(this.initial, scope))
|
79 | 54 | }
|
| 55 | + static tokenize (str: string): Array<'|' | ',' | ':' | string> { |
| 56 | + const tokens = [] |
| 57 | + let i = 0 |
| 58 | + while (i < str.length) { |
| 59 | + const ch = str[i] |
| 60 | + if (ch === '"' || ch === "'") { |
| 61 | + const j = i |
| 62 | + for (i += 2; i < str.length && str[i - 1] !== ch; ++i); |
| 63 | + tokens.push(str.slice(j, i)) |
| 64 | + } else if (/\s/.test(ch)) { |
| 65 | + i++ |
| 66 | + } else if (/[|,:]/.test(ch)) { |
| 67 | + tokens.push(str[i++]) |
| 68 | + } else { |
| 69 | + const j = i++ |
| 70 | + for (; i < str.length && !/[|,:\s]/.test(str[i]); ++i); |
| 71 | + tokens.push(str.slice(j, i)) |
| 72 | + } |
| 73 | + } |
| 74 | + return tokens |
| 75 | + } |
80 | 76 | }
|
0 commit comments