# All files / src/util check-args.js

100% Statements 15/15
88.46% Branches 23/26
100% Functions 1/1
100% Lines 15/15

    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

    603x
    10x
    10x

    593x
    4x
    4x

    589x
    1x
    1x

    588x
    632x
    14x
    14x

    618x
    6x

     import ordinal from './ordinal.js'

    // Validation helper
    export default function checkArgs(name, args, {
                        nullable: nullable = false,
                        minArgs: minArgs = 1,
                        maxArgs: maxArgs = 1 } = {}) {
      if (minArgs === maxArgs && args.length !== minArgs) {
        const plural = minArgs === 1 ? '' : 's'
        throw new Error(`${name} must receive exactly ${minArgs} argument${plural}`)
      }
      if (args.length < minArgs) {
        const plural1 = minArgs === 1 ? '' : 's'
        throw new Error(
          `${name} must receive at least ${minArgs} argument${plural1}.`)
      }
      if (args.length > maxArgs) {
        const plural2 = maxArgs === 1 ? '' : 's'
        throw new Error(
          `${name} accepts at most ${maxArgs} argument${plural2}.`)
      }
      for (let i = 0; i < args.length; i++) {
        if (!nullable && args[i] === null) {
          const ordinality = maxArgs !== 1 ? ` ${ordinal(i + 1)}` : ''
          throw new Error(`The${ordinality} argument to ${name} must be non-null`)
        }
        if (args[i] === undefined) {
          throw new Error(
            `The ${ordinal(i + 1)} argument to ${name} must be defined`)
        }
      }
    }