All files / validator/yupSource number.js

76.36% Statements 42/55
68.25% Branches 43/63
81.25% Functions 26/32
79.59% Lines 39/49

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          162x     46x 23x       23x   23x 23x 83x   83x 1x   82x 4x 4x   3x       82x   2x         3x   163x   163x       6x           12x                                   1x           7x           2x           12x           2x       1x       5x     12x         1x       1x       1x       1x       7x         1x       7x         1x       7x         1x       7x                                                  
import { number as locale } from './locale'
import MixedSchema from './mixed'
import inherits from './util/inherits'
import isAbsent from './util/isAbsent'
 
let isNaN = value => value != +value
 
export default function NumberSchema(payload) {
  if (!(this instanceof NumberSchema))
    return typeof payload === 'function'
      ? payload(new NumberSchema())
      : new NumberSchema()
 
  MixedSchema.call(this, { type: 'number' })
 
  this.withMutation(() => {
    this.transform(function (value) {
      let parsed = value
 
      if (this._allowEmptyString && parsed === '') {
        parsed = ''
      } else {
        if (typeof parsed === 'string') {
          parsed = parsed.replace(/\s/g, '')
          if (parsed === '') return NaN
          // don't use parseFloat to avoid positives on alpha-numeric strings
          parsed = +parsed
        }
      }
 
      if (this.isType(parsed)) return parsed
 
      return parseFloat(parsed)
    })
  })
}
 
inherits(NumberSchema, MixedSchema, {
  _typeCheck(value) {
    Iif (value instanceof Number) value = value.valueOf()
 
    return typeof value === 'number' && !isNaN(value)
  },
 
  min(min, message = locale.min) {
    return this.test({
      message,
      name: 'min',
      exclusive: true,
      params: { min },
      test(value) {
        return isAbsent(value) || value >= this.resolve(min)
      },
    })
  },
 
  max(max, message = locale.max) {
    return this.test({
      message,
      name: 'max',
      exclusive: true,
      params: { max },
      test(value) {
        return isAbsent(value) || value <= this.resolve(max)
      },
    })
  },
 
  lessThan(less, message = locale.lessThan) {
    return this.test({
      message,
      name: 'max',
      exclusive: true,
      params: { less },
      test(value) {
        return isAbsent(value) || value < this.resolve(less)
      },
    })
  },
 
  moreThan(more, message = locale.moreThan) {
    return this.test({
      message,
      name: 'min',
      exclusive: true,
      params: { more },
      test(value) {
        return isAbsent(value) || value > this.resolve(more)
      },
    })
  },
 
  positive(msg = locale.positive) {
    return this.moreThan(0, msg)
  },
 
  negative(msg = locale.negative) {
    return this.lessThan(0, msg)
  },
 
  integer(message = locale.integer) {
    return this.test({
      name: 'integer',
      message,
      test: val => isAbsent(val) || Number.isInteger(val),
    })
  },
 
  id(msg = locale.id) {
    return this.positive(msg).integer(msg)
  },
 
  positiveInteger(msg = locale.positiveInteger) {
    return this.positive(msg).integer(msg)
  },
 
  negativeInteger(msg = locale.negativeInteger) {
    return this.negative(msg).integer(msg)
  },
 
  nonPositive(msg = locale.nonPositive) {
    return this.test({
      name: 'nonPositive',
      message: msg,
      exclusive: true,
      test: val => isAbsent(val) || val <= 0,
    })
  },
 
  nonNegative(msg = locale.nonNegative) {
    return this.test({
      name: 'nonNegative',
      message: msg,
      exclusive: true,
      test: val => isAbsent(val) || val >= 0,
    })
  },
 
  nonPositiveInteger(msg = locale.nonPositiveInteger) {
    return this.test({
      name: 'nonPositiveInteger',
      message: msg,
      exclusive: true,
      test: val => isAbsent(val) || (Number.isInteger(val) && val <= 0),
    })
  },
 
  nonNegativeInteger(msg = locale.nonNegativeInteger) {
    return this.test({
      name: 'nonNegativeInteger',
      message: msg,
      exclusive: true,
      test: val => isAbsent(val) || (Number.isInteger(val) && val >= 0),
    })
  },
 
  truncate() {
    return this.transform(value => (!isAbsent(value) ? value | 0 : value))
  },
 
  round(method) {
    var avail = ['ceil', 'floor', 'round', 'trunc']
    method = (method && method.toLowerCase()) || 'round'
 
    // this exists for symemtry with the new Math.trunc
    if (method === 'trunc') return this.truncate()
 
    if (avail.indexOf(method.toLowerCase()) === -1)
      throw new TypeError(
        'Only valid options for round() are: ' + avail.join(', '),
      )
 
    return this.transform(value =>
      !isAbsent(value) ? Math[method](value) : value,
    )
  },
})