UNPKG

@intlayer/core

Version:

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

1 lines 1.42 kB
{"version":3,"file":"getCondition.cjs","names":[],"sources":["../../../src/interpreter/getCondition.ts"],"sourcesContent":["import type { ConditionContentStates } from '../transpiler';\n\n/**\n * Allow to pick a content based on a condition.\n *\n * Usage:\n *\n * ```ts\n * const content = getCondition({\n * 'true': 'The condition is validated',\n * 'false': 'The condition is not validated',\n * }, true);\n * // 'The condition is validated'\n * ```\n *\n * The last key provided will be used as the fallback value.\n *\n * ```ts\n * const content = getCondition({\n * 'false': 'The condition is not validated',\n * 'true': 'The condition is validated',\n * }, undefined);\n * // 'The condition is validated'\n */\nexport const getCondition = <Content>(\n conditionContent: ConditionContentStates<Content>,\n state?: boolean\n): Content => {\n const stateList = Object.keys(conditionContent);\n const fallbackState = stateList[\n stateList.length - 1\n ] as keyof typeof conditionContent;\n\n // Default or error handling if no keys match\n return (\n conditionContent[`${state}` as keyof typeof conditionContent] ??\n conditionContent.fallback ??\n (conditionContent[fallbackState] as Content)\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAwBA,MAAa,gBACX,kBACA,UACY;CACZ,MAAM,YAAY,OAAO,KAAK,iBAAiB;CAC/C,MAAM,gBAAgB,UACpB,UAAU,SAAS;AAIrB,QACE,iBAAiB,GAAG,YACpB,iBAAiB,YAChB,iBAAiB"}