All files / src list.ts

90.9% Statements 10/11
100% Branches 4/4
100% Functions 4/4
90% Lines 9/10

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 2422x 22x                     22x 4274x 8524x 8512x 8512x 4274x         4274x  
import auto from './auto.js'
import raw from './raw.js'
import type S from './sanitised.js'
 
/**
 * @returns comma separated list of values.
 *
 * If there are no `undefined` values, `undefined` is propagated.
 *
 * Optional element to sanitised string mapping can be provided (defaults to auto-sanitation).
 */
export const list =
  <T>(values: readonly T[], sanitise: (value: T) => undefined | S = auto, separator = ', '): undefined | S => {
    const values_ = values
      .filter(_ => typeof _ !== 'undefined')
      .map(_ => sanitise(_)?.toString().trim())
      .filter(_ => typeof _ !== 'undefined')
    return values_.length > 0 ?
      raw(values_.join(separator)) :
      undefined
  }
 
export default list