|
| 1 | +// @flow |
| 2 | + |
| 3 | +import {ValueType, BooleanType, toString} from '../types'; |
| 4 | +import RuntimeError from '../runtime_error'; |
| 5 | +import {typeOf} from '../values'; |
| 6 | + |
| 7 | +import type {Expression} from '../expression'; |
| 8 | +import type ParsingContext from '../parsing_context'; |
| 9 | +import type EvaluationContext from '../evaluation_context'; |
| 10 | +import type {Type} from '../types'; |
| 11 | +import type {Value} from '../values'; |
| 12 | + |
| 13 | +function isComparableType(type: Type) { |
| 14 | + return type.kind === 'boolean' || |
| 15 | + type.kind === 'string' || |
| 16 | + type.kind === 'number' || |
| 17 | + type.kind === 'null' || |
| 18 | + type.kind === 'value'; |
| 19 | +} |
| 20 | + |
| 21 | +function isComparableRuntimeValue(needle: boolean | string | number | null) { |
| 22 | + return typeof needle === 'boolean' || |
| 23 | + typeof needle === 'string' || |
| 24 | + typeof needle === 'number'; |
| 25 | +} |
| 26 | + |
| 27 | +function isSearchableRuntimeValue(haystack: Array<Value> | string) { |
| 28 | + return Array.isArray(haystack) || |
| 29 | + typeof haystack === 'string'; |
| 30 | +} |
| 31 | + |
| 32 | +class In implements Expression { |
| 33 | + type: Type; |
| 34 | + needle: Expression; |
| 35 | + haystack: Expression; |
| 36 | + |
| 37 | + constructor(needle: Expression, haystack: Expression) { |
| 38 | + this.type = BooleanType; |
| 39 | + this.needle = needle; |
| 40 | + this.haystack = haystack; |
| 41 | + } |
| 42 | + |
| 43 | + static parse(args: $ReadOnlyArray<mixed>, context: ParsingContext) { |
| 44 | + if (args.length !== 3) { |
| 45 | + return context.error(`Expected 2 arguments, but found ${args.length - 1} instead.`); |
| 46 | + } |
| 47 | + |
| 48 | + const needle = context.parse(args[1], 1, ValueType); |
| 49 | + |
| 50 | + const haystack = context.parse(args[2], 2, ValueType); |
| 51 | + |
| 52 | + if (!needle || !haystack) return null; |
| 53 | + |
| 54 | + if (!isComparableType(needle.type)) { |
| 55 | + return context.error(`Expected first argument to be of type boolean, string, number or null, but found ${toString(needle.type)} instead`); |
| 56 | + } |
| 57 | + |
| 58 | + return new In(needle, haystack); |
| 59 | + } |
| 60 | + |
| 61 | + evaluate(ctx: EvaluationContext) { |
| 62 | + const needle = (this.needle.evaluate(ctx): any); |
| 63 | + const haystack = (this.haystack.evaluate(ctx): any); |
| 64 | + |
| 65 | + if (!needle || !haystack) return false; |
| 66 | + |
| 67 | + if (!isComparableRuntimeValue(needle)) { |
| 68 | + throw new RuntimeError(`Expected first argument to be of type boolean, string or number, but found ${toString(typeOf(needle))} instead.`); |
| 69 | + } |
| 70 | + |
| 71 | + if (!isSearchableRuntimeValue(haystack)) { |
| 72 | + throw new RuntimeError(`Expected second argument to be of type array or string, but found ${toString(typeOf(haystack))} instead.`); |
| 73 | + } |
| 74 | + |
| 75 | + return haystack.indexOf(needle) >= 0; |
| 76 | + } |
| 77 | + |
| 78 | + eachChild(fn: (Expression) => void) { |
| 79 | + fn(this.needle); |
| 80 | + fn(this.haystack); |
| 81 | + } |
| 82 | + |
| 83 | + possibleOutputs() { |
| 84 | + return [true, false]; |
| 85 | + } |
| 86 | + |
| 87 | + serialize() { |
| 88 | + return ["in", this.needle.serialize(), this.haystack.serialize()]; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +export default In; |
0 commit comments