UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

1 lines 3.4 kB
{"version":3,"file":"getEnumeration.mjs","names":[],"sources":["../../../src/interpreter/getEnumeration.ts"],"sourcesContent":["import type { EnterFormat, EnumerationContentState } from '../transpiler';\n\n/**\n * Find the matching condition for a quantity.\n *\n * Usage:\n *\n * ```ts\n * const key = findMatchingCondition({\n * '<=-2.3': 'You have less than -2.3',\n * '<1': 'You have less than one',\n * '2': 'You have two',\n * '>=3': 'You have three or more',\n * }, 2);\n * // '2'\n * ```\n *\n * The order of the keys will define the priority of the content.\n *\n * ```ts\n * const key = findMatchingCondition({\n * '<4': 'You have less than four',\n * '2': 'You have two',\n * }, 2);\n * // '<4'\n * ```\n *\n * If no keys match, the default key is '1'.\n */\nexport const findMatchingCondition = <Content>(\n enumerationContent: EnumerationContentState<Content>,\n quantity: number\n): EnterFormat => {\n const numericKeys = Object.keys(enumerationContent);\n\n for (const key of numericKeys) {\n const isEqual =\n (!key.startsWith('>') &&\n !key.startsWith('<') &&\n !key.startsWith('=') &&\n parseFloat(key) === quantity) ||\n (key.startsWith('=') && parseFloat(key.slice(1)) === quantity);\n const isSuperior =\n key.startsWith('>') && quantity > parseFloat(key.slice(1));\n const isSuperiorOrEqual =\n key.startsWith('>=') && quantity >= parseFloat(key.slice(2));\n const isInferior =\n key.startsWith('<') && quantity < parseFloat(key.slice(1));\n const isInferiorOrEqual =\n key.startsWith('<=') && quantity <= parseFloat(key.slice(2));\n\n if (\n isEqual ||\n isSuperior ||\n isSuperiorOrEqual ||\n isInferior ||\n isInferiorOrEqual\n ) {\n return key as EnterFormat;\n }\n }\n};\n\n/**\n * Allow to pick a content based on a quantity.\n *\n * Usage:\n *\n * ```ts\n * const content = getEnumeration({\n * '<=-2.3': 'You have less than -2.3',\n * '<1': 'You have less than one',\n * '2': 'You have two',\n * '>=3': 'You have three or more',\n * }, 2);\n * // 'You have two'\n * ```\n *\n * The order of the keys will define the priority of the content.\n *\n * ```ts\n * const content = getEnumeration({\n * '<4': 'You have less than four',\n * '2': 'You have two',\n * }, 2);\n * // 'You have less than four'\n * ```\n *\n */\nexport const getEnumeration = <Content>(\n enumerationContent: EnumerationContentState<Content>,\n quantity: number\n): Content => {\n const key =\n findMatchingCondition<Content>(enumerationContent, quantity) ?? 'fallback';\n\n // Default or error handling if no keys match\n return enumerationContent[key] as Content;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAa,yBACX,oBACA,aACgB;CAChB,MAAM,cAAc,OAAO,KAAK,mBAAmB;AAEnD,MAAK,MAAM,OAAO,aAAa;EAC7B,MAAM,UACH,CAAC,IAAI,WAAW,IAAI,IACnB,CAAC,IAAI,WAAW,IAAI,IACpB,CAAC,IAAI,WAAW,IAAI,IACpB,WAAW,IAAI,KAAK,YACrB,IAAI,WAAW,IAAI,IAAI,WAAW,IAAI,MAAM,EAAE,CAAC,KAAK;EACvD,MAAM,aACJ,IAAI,WAAW,IAAI,IAAI,WAAW,WAAW,IAAI,MAAM,EAAE,CAAC;EAC5D,MAAM,oBACJ,IAAI,WAAW,KAAK,IAAI,YAAY,WAAW,IAAI,MAAM,EAAE,CAAC;EAC9D,MAAM,aACJ,IAAI,WAAW,IAAI,IAAI,WAAW,WAAW,IAAI,MAAM,EAAE,CAAC;EAC5D,MAAM,oBACJ,IAAI,WAAW,KAAK,IAAI,YAAY,WAAW,IAAI,MAAM,EAAE,CAAC;AAE9D,MACE,WACA,cACA,qBACA,cACA,kBAEA,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+Bb,MAAa,kBACX,oBACA,aACY;AAKZ,QAAO,mBAHL,sBAA+B,oBAAoB,SAAS,IAAI"}