|
| 1 | +export class Expressions { |
| 2 | + constructor() {} |
| 3 | + findStopLessThanOrEqualTo(stops, input) { |
| 4 | + const n = stops.length; |
| 5 | + var lowerIndex = 0; |
| 6 | + var upperIndex = n - 1; |
| 7 | + var currentIndex = 0; |
| 8 | + var currentValue, upperValue; |
| 9 | + |
| 10 | + while (lowerIndex <= upperIndex) { |
| 11 | + currentIndex = Math.floor((lowerIndex + upperIndex) / 2); |
| 12 | + currentValue = stops[currentIndex][0]; |
| 13 | + upperValue = stops[currentIndex + 1][0]; |
| 14 | + if (input === currentValue || (input > currentValue && input < upperValue)) { |
| 15 | + // Search complete |
| 16 | + return currentIndex; |
| 17 | + } else if (currentValue < input) { |
| 18 | + lowerIndex = currentIndex + 1; |
| 19 | + } else if (currentValue > input) { |
| 20 | + upperIndex = currentIndex - 1; |
| 21 | + } |
| 22 | + } |
| 23 | + return Math.max(currentIndex - 1, 0); |
| 24 | + } |
| 25 | + interpolationFactor(input, base, lowerValue, upperValue) { |
| 26 | + const difference = upperValue - lowerValue; |
| 27 | + const progress = input - lowerValue; |
| 28 | + |
| 29 | + if (difference === 0) { |
| 30 | + return 0; |
| 31 | + } else if (base === 1 || base === undefined) { |
| 32 | + return progress / difference; |
| 33 | + } else { |
| 34 | + return (Math.pow(base, progress) - 1) / (Math.pow(base, difference) - 1); |
| 35 | + } |
| 36 | + } |
| 37 | + number(a, b, t) { |
| 38 | + return a * (1 - t) + b * t; |
| 39 | + } |
| 40 | + array(from, to, t) { |
| 41 | + return from.map(function (d, i) { |
| 42 | + return this.number(d, to[i], t); |
| 43 | + }); |
| 44 | + } |
| 45 | + asExpression(exp) { |
| 46 | + if (Array.isArray(exp)) { |
| 47 | + return exp; |
| 48 | + } |
| 49 | + if (!exp.stops) { |
| 50 | + throw new Error(`${JSON.stringify(exp)} is not a correct expression`); |
| 51 | + } |
| 52 | + if (exp.base === 1 || exp.base === undefined) { |
| 53 | + return ['interpolate', ['linear'], ['zoom']].concat(exp.stops.reduce((acc, e) => acc.concat(e), [])); |
| 54 | + } |
| 55 | + return ['interpolate', ['exponential', exp.base], ['zoom']].concat(exp.stops.reduce((acc, e) => acc.concat(e), [])); |
| 56 | + } |
| 57 | + parseInterpolate(exp, value) { |
| 58 | + if (!exp || exp[0] !== 'interpolate' || exp.length < 4) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + const base = exp[1][0] === 'exponential' ? exp[1][1] : 1; |
| 63 | + const stops = exp.slice(3).reduce((acc, e, i) => { |
| 64 | + i % 2 === 0 ? acc.push([e]) : acc[acc.length - 1].push(e); |
| 65 | + return acc; |
| 66 | + }, []); |
| 67 | + if (stops.length === 1 || stops[0][0] >= value) { |
| 68 | + return { |
| 69 | + value: stops[0][1], |
| 70 | + }; |
| 71 | + } else if (stops[stops.length - 1][0] <= value) { |
| 72 | + return { |
| 73 | + value: stops[stops.length - 1][1], |
| 74 | + }; |
| 75 | + } |
| 76 | + const index = this.findStopLessThanOrEqualTo(stops, value); |
| 77 | + return { |
| 78 | + lower: stops[index][1], |
| 79 | + upper: stops[index + 1][1], |
| 80 | + t: this.interpolationFactor(value, base, stops[index][0], stops[index + 1][0]), |
| 81 | + }; |
| 82 | + } |
| 83 | + renderMatch(exp, match) { |
| 84 | + if (!Array.isArray(exp)) { |
| 85 | + return exp; |
| 86 | + } |
| 87 | + return this.decision( |
| 88 | + exp.map((e) => this.renderMatch(e, match)), |
| 89 | + match |
| 90 | + ); |
| 91 | + } |
| 92 | + lookup(exp, match) { |
| 93 | + if (!Array.isArray(exp)) { |
| 94 | + return exp; |
| 95 | + } |
| 96 | + switch (exp[0]) { |
| 97 | + case 'at': |
| 98 | + return this.lookup(exp[2], match)[exp[1]]; |
| 99 | + case 'get': |
| 100 | + case 'has': |
| 101 | + case '!has': |
| 102 | + return match[`${exp[0]}:${exp[1]}`]; |
| 103 | + case 'in': |
| 104 | + return this.lookup(exp[2], match).indexOf(exp[1]) >= 0; |
| 105 | + case '!in': |
| 106 | + return this.lookup(exp[2], match).indexOf(exp[1]) < 0; |
| 107 | + case 'index-of': |
| 108 | + return this.lookup(exp[2], match).indexOf(exp[1]); |
| 109 | + case 'length': |
| 110 | + return this.lookup(exp[1], match).length; |
| 111 | + case 'slice': |
| 112 | + return this.lookup(exp[1], match).slice(exp[2], exp[3]); |
| 113 | + } |
| 114 | + return exp; |
| 115 | + } |
| 116 | + decision(exp, match) { |
| 117 | + if (!Array.isArray(exp)) { |
| 118 | + return exp; |
| 119 | + } |
| 120 | + switch (exp[0]) { |
| 121 | + case '!': |
| 122 | + return !this.lookup(exp[1], match); |
| 123 | + case '==': |
| 124 | + return this.lookup(exp[1], match) == exp[2]; |
| 125 | + case '!=': |
| 126 | + return this.lookup(exp[1], match) != exp[2]; |
| 127 | + case '>': |
| 128 | + return this.lookup(exp[1], match) > exp[2]; |
| 129 | + case '<': |
| 130 | + return this.lookup(exp[1], match) < exp[2]; |
| 131 | + case '>=': |
| 132 | + return this.lookup(exp[1], match) >= exp[2]; |
| 133 | + case '<=': |
| 134 | + return this.lookup(exp[1], match) <= exp[2]; |
| 135 | + case 'all': |
| 136 | + return exp.slice(1).every((e) => this.lookup(e, match) === true); |
| 137 | + case 'some': |
| 138 | + return exp.slice(1).any((e) => this.lookup(e, match) === true); |
| 139 | + case 'match': { |
| 140 | + for (let i = 2; i < exp.length - 2; i = i + 2) { |
| 141 | + if (exp[1] == exp[i] || (Array.isArray(exp[i]) && exp[1] == exp[i][0])) { |
| 142 | + return exp[i + 1]; |
| 143 | + } |
| 144 | + } |
| 145 | + return exp[exp.length - 1]; |
| 146 | + } |
| 147 | + case 'case': { |
| 148 | + for (let i = 1; i < exp.length - 2; i = i + 2) { |
| 149 | + if (exp[i] === true) { |
| 150 | + return exp[i + 1]; |
| 151 | + } |
| 152 | + } |
| 153 | + return exp[exp.length - 1]; |
| 154 | + } |
| 155 | + case 'step': { |
| 156 | + let value = exp[1]; |
| 157 | + let res = exp[2]; |
| 158 | + if (value < exp[3]) { |
| 159 | + return res; |
| 160 | + } |
| 161 | + for (let i = 3; i < exp.length; i = i + 2) { |
| 162 | + if (exp[i] >= value) { |
| 163 | + return exp[i + 1]; |
| 164 | + } |
| 165 | + } |
| 166 | + return res; |
| 167 | + } |
| 168 | + case 'coalesce': |
| 169 | + return exp.slice(1).find((e) => e !== null && e !== undefined); |
| 170 | + } |
| 171 | + return this.lookup(exp, match); |
| 172 | + } |
| 173 | +} |
0 commit comments