{"version":3,"file":"number_format.mjs","names":[],"sources":["../../../src/expression/definitions/number_format.ts"],"sourcesContent":["import {StringType, NumberType} from '../types';\n\nimport type {Expression} from '../expression';\nimport type {EvaluationContext} from '../evaluation_context';\nimport type {ParsingContext} from '../parsing_context';\nimport type {Type} from '../types';\n\nexport class NumberFormat implements Expression {\n    type: Type;\n    number: Expression;\n    /**\n     * BCP 47 language tag\n     */\n    locale: Expression | null;\n    /**\n     * ISO 4217 currency code, required if style=currency\n     */\n    currency: Expression | null;\n    /**\n     * CLDR or ECMA-402 unit specifier, required if style=unit\n     */\n    unit: Expression | null;\n    /**\n     * @default 0\n     */\n    minFractionDigits: Expression | null;\n    /**\n     * @default 3\n     */\n    maxFractionDigits: Expression | null;\n\n    constructor(\n        number: Expression,\n        locale: Expression | null,\n        currency: Expression | null,\n        unit: Expression | null,\n        minFractionDigits: Expression | null,\n        maxFractionDigits: Expression | null\n    ) {\n        this.type = StringType;\n        this.number = number;\n        this.locale = locale;\n        this.currency = currency;\n        this.unit = unit;\n        this.minFractionDigits = minFractionDigits;\n        this.maxFractionDigits = maxFractionDigits;\n    }\n\n    static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression {\n        if (args.length !== 3) return context.error('Expected two arguments.') as null;\n\n        const number = context.parse(args[1], 1, NumberType);\n        if (!number) return null;\n\n        const options = args[2] as any;\n        if (typeof options !== 'object' || Array.isArray(options))\n            return context.error('NumberFormat options argument must be an object.') as null;\n\n        let locale = null;\n        if (options['locale']) {\n            locale = context.parse(options['locale'], 1, StringType);\n            if (!locale) return null;\n        }\n\n        let currency = null;\n        if (options['currency']) {\n            currency = context.parse(options['currency'], 1, StringType);\n            if (!currency) return null;\n        }\n\n        let unit = null;\n        if (options['unit']) {\n            unit = context.parse(options['unit'], 1, StringType);\n            if (!unit) return null;\n        }\n\n        if (currency && unit) {\n            return context.error(\n                'NumberFormat options `currency` and `unit` are mutually exclusive'\n            ) as null;\n        }\n\n        let minFractionDigits = null;\n        if (options['min-fraction-digits']) {\n            minFractionDigits = context.parse(options['min-fraction-digits'], 1, NumberType);\n            if (!minFractionDigits) return null;\n        }\n\n        let maxFractionDigits = null;\n        if (options['max-fraction-digits']) {\n            maxFractionDigits = context.parse(options['max-fraction-digits'], 1, NumberType);\n            if (!maxFractionDigits) return null;\n        }\n\n        return new NumberFormat(\n            number,\n            locale,\n            currency,\n            unit,\n            minFractionDigits,\n            maxFractionDigits\n        );\n    }\n\n    evaluate(ctx: EvaluationContext) {\n        return new Intl.NumberFormat(this.locale ? this.locale.evaluate(ctx) : [], {\n            style: this.currency ? 'currency' : this.unit ? 'unit' : 'decimal',\n            currency: this.currency ? this.currency.evaluate(ctx) : undefined,\n            unit: this.unit ? this.unit.evaluate(ctx) : undefined,\n            minimumFractionDigits: this.minFractionDigits\n                ? this.minFractionDigits.evaluate(ctx)\n                : undefined,\n            maximumFractionDigits: this.maxFractionDigits\n                ? this.maxFractionDigits.evaluate(ctx)\n                : undefined\n        }).format(this.number.evaluate(ctx));\n    }\n\n    eachChild(fn: (_: Expression) => void) {\n        fn(this.number);\n        if (this.locale) {\n            fn(this.locale);\n        }\n        if (this.currency) {\n            fn(this.currency);\n        }\n        if (this.unit) {\n            fn(this.unit);\n        }\n        if (this.minFractionDigits) {\n            fn(this.minFractionDigits);\n        }\n        if (this.maxFractionDigits) {\n            fn(this.maxFractionDigits);\n        }\n    }\n\n    outputDefined() {\n        return false;\n    }\n}\n"],"mappings":";;AAOA,IAAa,eAAb,MAAa,aAAmC;CAwB5C,YACI,QACA,QACA,UACA,MACA,mBACA,mBACF;EACE,KAAK,OAAO;EACZ,KAAK,SAAS;EACd,KAAK,SAAS;EACd,KAAK,WAAW;EAChB,KAAK,OAAO;EACZ,KAAK,oBAAoB;EACzB,KAAK,oBAAoB;CAC7B;CAEA,OAAO,MAAM,MAA8B,SAAqC;EAC5E,IAAI,KAAK,WAAW,GAAG,OAAO,QAAQ,MAAM,yBAAyB;EAErE,MAAM,SAAS,QAAQ,MAAM,KAA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