{"version":3,"file":"in.mjs","names":[],"sources":["../../../src/expression/definitions/in.ts"],"sourcesContent":["import {\n    BooleanType,\n    StringType,\n    ValueType,\n    NullType,\n    typeToString,\n    NumberType,\n    isValidType,\n    isValidNativeType\n} from '../types';\nimport {RuntimeError} from '../runtime_error';\nimport {typeOf} from '../values';\n\nimport type {Expression} from '../expression';\nimport type {ParsingContext} from '../parsing_context';\nimport type {EvaluationContext} from '../evaluation_context';\nimport type {Type} from '../types';\n\nexport class In implements Expression {\n    type: Type = BooleanType;\n\n    constructor(\n        public needle: Expression,\n        public haystack: Expression,\n        public readonly key: string\n    ) {}\n\n    static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression {\n        if (args.length !== 3) {\n            return context.error(\n                `Expected 2 arguments, but found ${args.length - 1} instead.`\n            ) as null;\n        }\n\n        const needle = context.parse(args[1], 1, ValueType);\n\n        const haystack = context.parse(args[2], 2, ValueType);\n\n        if (!needle || !haystack) return null;\n\n        if (!isValidType(needle.type, [BooleanType, StringType, NumberType, NullType, ValueType])) {\n            return context.error(\n                `Expected first argument to be of type boolean, string, number or null, but found ${typeToString(needle.type)} instead`\n            ) as null;\n        }\n\n        return new In(needle, haystack, context.key);\n    }\n\n    evaluate(ctx: EvaluationContext) {\n        const needle = this.needle.evaluate(ctx) as any;\n        const haystack = this.haystack.evaluate(ctx) as any;\n\n        if (!haystack) return false;\n\n        if (!isValidNativeType(needle, ['boolean', 'string', 'number', 'null'])) {\n            throw new RuntimeError(\n                `Expected first argument to be of type boolean, string, number or null, but found ${typeToString(typeOf(needle))} instead.`,\n                this.key\n            );\n        }\n\n        if (!isValidNativeType(haystack, ['string', 'array'])) {\n            throw new RuntimeError(\n                `Expected second argument to be of type array or string, but found ${typeToString(typeOf(haystack))} instead.`,\n                this.key\n            );\n        }\n\n        return haystack.indexOf(needle) >= 0;\n    }\n\n    eachChild(fn: (_: Expression) => void) {\n        fn(this.needle);\n        fn(this.haystack);\n    }\n\n    outputDefined() {\n        return true;\n    }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