{"version":3,"file":"index_of.mjs","names":[],"sources":["../../../src/expression/definitions/index_of.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 IndexOf implements Expression {\n    type: Type = NumberType;\n\n    constructor(\n        public needle: Expression,\n        public haystack: Expression,\n        public readonly key: string,\n        public fromIndex?: Expression\n    ) {}\n\n    static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression {\n        if (args.length <= 2 || args.length >= 5) {\n            return context.error(\n                `Expected 2 or 3 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        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        if (args.length === 4) {\n            const fromIndex = context.parse(args[3], 3, NumberType);\n            if (!fromIndex) return null;\n            return new IndexOf(needle, haystack, context.key, fromIndex);\n        } else {\n            return new IndexOf(needle, haystack, context.key);\n        }\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 (!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        let fromIndex;\n        if (this.fromIndex) {\n            fromIndex = this.fromIndex.evaluate(ctx) as number;\n        }\n\n        if (isValidNativeType(haystack, ['string'])) {\n            const rawIndex = haystack.indexOf(needle, fromIndex);\n            if (rawIndex === -1) {\n                return -1;\n            } else {\n                // The index may be affected by surrogate pairs, so get the length of the preceding substring.\n                return [...haystack.slice(0, rawIndex)].length;\n            }\n        } else if (isValidNativeType(haystack, ['array'])) {\n            return haystack.indexOf(needle, fromIndex);\n        } else {\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\n    eachChild(fn: (_: Expression) => void) {\n        fn(this.needle);\n        fn(this.haystack);\n        if (this.fromIndex) {\n            fn(this.fromIndex);\n        }\n    }\n\n    outputDefined() {\n        return false;\n    }\n}\n"],"mappings":";;;;AAkBA,IAAa,UAAb,MAAa,QAA8B;CAGvC,YACI,QACA,UACA,KACA,WACF;EAJS,KAAA,SAAA;EACA,KAAA,WAAA;EACS,KAAA,MAAA;EACT,KAAA,YAAA;EANE,KAAA,OAAA;CAOV;CAEH,OAAO,MAAM,MAA8B,SAAqC;EAC5E,IAAI,KAAK,UAAU,KAAK,KAAK,UAAU,GACnC,OAAO,QAAQ,MACX,wCAAwC,KAAK,SAAS,EAAE,UAC5D;EAGJ,MAAM,SAAS,QAAQ,MAAM,KAAK,IAAI,GAAG,SAAS;EAElD,MAAM,WAAW,QAAQ,MAAM,KAAK,IAAI,GAAG,SAAS;EAEpD,IAAI,CAAC,UAAU,CAAC,UAAU,OAAO;EACjC,IAAI,CAAC,YAAY,OAAO,MAAM;GAAC;GAAa;GAAY;GAAY;GAAU;EAAS,CAAC,GACpF,OAAO,QAAQ,MACX,oFAAoF,aAAa,OAAO,IAAI,EAAE,SAClH;EAGJ,IAAI,KAAK,WAAW,GAAG;GACnB,MAAM,YAAY,QAAQ,MAAM,KAAK,IAAI,GAAG,UAAU;GACtD,IAAI,CAAC,WAAW,OAAO;GACvB,OAAO,IAAI,QAAQ,QAAQ,UAAU,QAAQ,KAAK,SAAS;EAC/D,OACI,OAAO,IAAI,QAAQ,QAAQ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