All files / key-parser value-formatter.js

94.96% Statements 264/278
82.97% Branches 39/47
92.85% Functions 13/14
94.96% Lines 264/278

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 2791x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 19x 19x 19x 19x 19x 19x 19x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 16x 13x 16x 2x 2x 16x 16x 16x 1x 1x 1x 1x 16x     16x 16x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x     1x 1x 1x 1x 1x 1x 15x 15x             15x 15x 15x 15x 15x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 19x 19x 19x 19x 19x 19x 38x     38x 38x 19x 19x 1x 1x 1x 1x 1x 11x 11x 1x 1x 11x 10x 10x 10x     11x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 3x 3x 2x 2x 2x 3x 11x 9x 9x 11x 11x 11x 1x 1x 1x 1x 1x 1x 11x 11x 11x 2x 2x 9x 9x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 9x 9x 9x 11x 11x 11x 11x 11x 11x 20x 31x 20x 20x 20x 11x 11x 11x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { states } from './key-ast.util.js'
import { formatters as expressionFormatters } from './expression-formatters.js'
import { isInteger } from '../utils/algorithms/number.utils.js'
/** @import {AST, Token} from './key-ast.util.js' */
/** @import {CaptureExpressionInfo, FormatCall as DefaultFormatter} from './capture-expression-values.js' */
 
/** @type { Readonly<never[]>} */
const emptyArray = Object.freeze([])
 
/**
 * Add or replace format method from templateFormatter object
 * @param {Omit<TemplateFormatter, 'format'>} templateFormatter - target TemplateFormatter object
 * @returns {TemplateFormatter} TemplateFormatter object with updated `format` function
 */
const formatterWithFormat = (templateFormatter) => ({
  ...templateFormatter,
  format: (parameters, locale, defaultFormatters) => {
    const { strings, formatters } = templateFormatter
    let result = strings[0]
    for (let i = 1, e = strings.length; i < e; ++i) {
      result += formatters[i - 1](parameters, locale, defaultFormatters) + strings[i]
    }
    return result
  },
})
 
/**
 * Returns a formatter that returns the `value` content
 *
 * Used when the value does not have parameters
 * @param {string} value - value constant
 * @returns {TemplateFormatter} resulting template formatter
 */
const formatSimpleKey = (value) => ({
  strings: [value],
  formatters: emptyArray,
  format: () => value,
})
 
/**
 * parse capture key token reducer
 * @param {[string, CaptureExpressionsInfoDetail[]]} acc - accumulator
 * @param {Token} token - capture child token
 * @returns {[string, CaptureExpressionsInfoDetail[]]} updated accumulator
 */
function parseCaptureKeyToken (acc, token) {
  const [currentExpression, fragmentedCaptureExpressionsInfo] = acc
  switch (token.type) {
    case states.capture_expr:
      return [currentExpression ? `${currentExpression} ${token.text}` : token.text, fragmentedCaptureExpressionsInfo]
    case states.capture_expr_sep: {
      return ['', [...fragmentedCaptureExpressionsInfo, { type: 'expression', text: currentExpression }]]
    }
    case states.sq_string:
    case states.dq_string:
    case states.bt_string: {
      /** @type {CaptureExpressionsInfoDetail} */
      const info = { type: 'string', text: token.text.slice(1, token.text.length - 1) }
      return [currentExpression, [...fragmentedCaptureExpressionsInfo, info]]
    }
    default:
      console.error('error: invalid expression, ignoring...')
      return acc
  }
}
 
/**
 * Parse capture key to ease usage in {@link getFormatterFromTokens}
 * @param {Token} captureToken - target capture token
 * @returns {CaptureExpressionsInfoDetail[]} parse result
 */
function parseCaptureKey (captureToken) {
  const [currentExpression, fragmentedCaptureExpressionsInfo] = captureToken.childTokens.reduce(parseCaptureKeyToken, ['', []])
 
  if (currentExpression) {
    fragmentedCaptureExpressionsInfo.push({
      type: 'expression',
      text: currentExpression,
    })
  }
 
  return fragmentedCaptureExpressionsInfo
}
 
/**
 * Formatter to use for simple capture expressions (e.g. `{0}`)
 * @type {FormatterReducer}
 */
function applyDefaultFormatter (acc) {
  if (acc.defaultFormatters && typeof acc.position === 'number') {
    return {
      ...acc,
      result: acc.defaultFormatters[acc.position](acc.result, acc.locale),
    }
  }
  return acc
}
 
/**
 * @param {number} position - position to query
 * @returns {FormatterReducer} built FormatterReducer
 */
const positionFormatter = (position) => (acc) => {
  const { parameters } = acc
  if (parameters.length <= position) {
    return {
      ...acc,
      result: '',
      exit: true,
    }
  }
  return {
    ...acc,
    position,
    result: parameters[position],
  }
}
 
/**
 *
 * @param {FormatterReducer[]} fragmentedFormatters - list of formatter reducers
 * @param {Strings} parameters - parameter list
 * @param {Intl.Locale} locale - locale to format data
 * @param {DefaultFormatters} [defaultFormatters] - default formatter
 * @returns {string} format result
 */
const formatFromReducers = (fragmentedFormatters, parameters, locale, defaultFormatters = []) => {
  /** @type {FormatterReducerAccumulator} */
  let reducerAcc = {
    parameters,
    defaultFormatters,
    result: '',
    locale,
  }
  for (const fragmentedFormatter of fragmentedFormatters) {
    if (reducerAcc.exit) {
      return reducerAcc.result
    }
    reducerAcc = fragmentedFormatter(reducerAcc)
  }
  return reducerAcc.result
}
 
/**
 * @param {CaptureExpressionsInfoDetail} detail - capture token
 * @returns {FormatterReducer | null} - FormatterReducer or null if invalid
 */
function getFirstExpressionFormatterReducer (detail) {
  if (detail.type === 'string') {
    const { text } = detail
    return (acc) => ({ ...acc, result: text })
  } else if (isInteger(detail.text)) {
    const position = +detail.text
    return positionFormatter(position)
  } else {
    return null
  }
}
 
const printNothing = () => ''
 
/**
 * @param {Token} token - capture token
 * @returns {Formatter} resulting formatter
 */
function getFormatterFromCaptureToken (token) {
  const fragmentedCaptureExpressionsInfo = parseCaptureKey(token)
  if (fragmentedCaptureExpressionsInfo.length === 0) { return printNothing }
 
  const [firstInfo, ...restInfo] = fragmentedCaptureExpressionsInfo
  const firstReducer = getFirstExpressionFormatterReducer(firstInfo)
  if (!firstReducer) { return printNothing }
 
  const fragmentedFormatters = [firstReducer]
  for (const info of restInfo) {
    const { text } = info
    if (Object.hasOwn(expressionFormatters, text)) {
      const formatter = expressionFormatters[text]
      fragmentedFormatters.push((acc) => ({ ...acc, result: formatter.format(acc.result, acc.locale) }))
    }
  }
  if (fragmentedFormatters.length <= 1) {
    fragmentedFormatters.push(applyDefaultFormatter)
  }
 
  return (parameters, locale, defaultFormatters) => formatFromReducers(fragmentedFormatters, parameters, locale, defaultFormatters)
}
 
/**
 * This is for the formatter to work the same way as a template string, it always end with a string even if is empty
 * @param {Pick<TemplateFormatter, "strings"|"formatters">} params - building formatter
 * @returns {Pick<TemplateFormatter, "strings"|"formatters">} formatter that ends with string
 */
function guaranteeFormatterEndsWithString (params) {
  const { strings, formatters } = params
  if (strings.length === formatters.length) {
    return { strings: [...strings, ''], formatters }
  }
  return params
}
 
/**
 * @param {AST} ast  - AST of parsed value
 * @returns {TemplateFormatter} resulting formatter
 */
export function getFormatter (ast) {
  const { tokens } = ast
  const captureTokens = tokens.filter((token) => token.type === states.capture)
 
  if (captureTokens.length <= 0) {
    const textToMatch = tokens.map((token) => token.text).join('')
    return formatSimpleKey(textToMatch)
  }
 
  /** @type {string[]} */
  const strings = []
  /** @type {Formatter[]} */
  const formatters = []
 
  for (const keyToken of tokens) {
    if (keyToken.type !== states.capture) {
      strings.push(keyToken.text)
      continue
    }
    formatters.push(getFormatterFromCaptureToken(keyToken))
  }
 
  return formatterWithFormat(guaranteeFormatterEndsWithString({ strings, formatters }))
}
 
/**
 * @typedef {object} CaptureExpressionsInfoDetail
 * @property {'expression' | 'string'} type - type of detail
 * @property {string} text - detail text
 */
 
/**
 * @typedef {object} FormatterReducerAccumulator
 *
 * Formatter reducer accumulator, used when piping in the result with an expression (e.g. `{0 | relative time}`)
 * @property {Strings} parameters
 *  Parameters used in the i18n key, e.g. when translating "On 2023-01-01T20:00:00 I bought 10 fireworks"
 *  on key "On {date} I bought {number} fireworks", the parameters are going to be ["2023-01-01T20:00:00", "10"]
 * @property {DefaultFormatters}  defaultFormatters
 *   The default formatter to use for each parameter in `parameters`
 * @property {string}       result - The current result on accumulator, is the final result after passing all reducers
 * @property {Intl.Locale}  locale - Locale used when formatting the text
 * @property {number}       [position] - index of `parameters` to match text
 * @property {boolean}      [exit] - flag to exit early and use `result` as final result immediately, ignoring the rest of the reducers
 */
 
/**
 * @callback FormatterReducer
 * @param {FormatterReducerAccumulator} previous - reducer previous value
 * @returns {FormatterReducerAccumulator} next value
 */
 
/**
 * @callback Formatter
 *  A placeholder formatter
 * @param {Strings} parameters -
 *     Placeholder parameters, the placeholder may be empty ({}), may contain one parameter that is a string ({"text"}) or a capture expression position ({ 0 })
 * additional parameters allows to define how to convert the previous parameter result ({0 | relative time})
 * @param {Intl.Locale} locale - locale to format
 * @param {DefaultFormatters} [defaultFormatters] - default formatter
 * @returns {string} formatted string
 */
 
/**
 * @typedef {object} TemplateFormatter
 * @property {Strings} strings - raw strings
 * @property {Formatters} formatters - formatters of each capture token
 * @property {(parameters: Strings, locale: Intl.Locale, defaultFormatters?: DefaultFormatters) => string} format - format function
 */
 
/** @typedef {Readonly<string[]>} Strings - raw string array, immutable */
/** @typedef {Readonly<Formatter[]>} Formatters - formatters of each capture token */
/** @typedef {Readonly<DefaultFormatter[]>} DefaultFormatters - default formatters of each capture token */