UNPKG

@intlayer/core

Version:

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

74 lines (72 loc) 2.15 kB
//#region src/interpreter/getEnumeration.ts /** * Find the matching condition for a quantity. * * Usage: * * ```ts * const key = findMatchingCondition({ * '<=-2.3': 'You have less than -2.3', * '<1': 'You have less than one', * '2': 'You have two', * '>=3': 'You have three or more', * }, 2); * // '2' * ``` * * The order of the keys will define the priority of the content. * * ```ts * const key = findMatchingCondition({ * '<4': 'You have less than four', * '2': 'You have two', * }, 2); * // '<4' * ``` * * If no keys match, the default key is '1'. */ const findMatchingCondition = (enumerationContent, quantity) => { const numericKeys = Object.keys(enumerationContent); for (const key of numericKeys) { const isEqual = !key.startsWith(">") && !key.startsWith("<") && !key.startsWith("=") && parseFloat(key) === quantity || key.startsWith("=") && parseFloat(key.slice(1)) === quantity; const isSuperior = key.startsWith(">") && quantity > parseFloat(key.slice(1)); const isSuperiorOrEqual = key.startsWith(">=") && quantity >= parseFloat(key.slice(2)); const isInferior = key.startsWith("<") && quantity < parseFloat(key.slice(1)); const isInferiorOrEqual = key.startsWith("<=") && quantity <= parseFloat(key.slice(2)); if (isEqual || isSuperior || isSuperiorOrEqual || isInferior || isInferiorOrEqual) return key; } }; /** * Allow to pick a content based on a quantity. * * Usage: * * ```ts * const content = getEnumeration({ * '<=-2.3': 'You have less than -2.3', * '<1': 'You have less than one', * '2': 'You have two', * '>=3': 'You have three or more', * }, 2); * // 'You have two' * ``` * * The order of the keys will define the priority of the content. * * ```ts * const content = getEnumeration({ * '<4': 'You have less than four', * '2': 'You have two', * }, 2); * // 'You have less than four' * ``` * */ const getEnumeration = (enumerationContent, quantity) => { return enumerationContent[findMatchingCondition(enumerationContent, quantity) ?? "fallback"]; }; //#endregion exports.findMatchingCondition = findMatchingCondition; exports.getEnumeration = getEnumeration; //# sourceMappingURL=getEnumeration.cjs.map