all files / src/ length.js

100% Statements 24/24
100% Branches 25/25
100% Functions 3/3
100% Lines 20/20
1 branch Ignored     
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                             42× 42×   42× 42× 42×   42× 42×     42× 46× 15×   31×   25× 13×            
import Validators from './validators'
import { prepareMsg, prepare, isNumber, selectNum, memoize } from './helpers'
 
let length = memoize(function ({
  '=': equal,
  is,
  max,
  maximum,
  min,
  minimum,
  in: range,
  within,
  message,
  msg,
  if: ifCond,
  unless,
  allowBlank
}) {
  msg = msg || message
 
  equal = selectNum(equal, is)
  min = selectNum(min, minimum)
  max = selectNum(max, maximum)
 
  range = range || within
  if (range && isNumber(range[0]) && isNumber(range[1])) {
    min = range[0]
    max = range[1]
  }
 
  return prepare(ifCond, unless, allowBlank, function (value) {
    if (equal !== null && value.length !== equal) {
      return Validators.formatMessage(prepareMsg(msg, 'wrongLength', { count: equal }))
    }
    if (max !== null && value.length > max) {
      return Validators.formatMessage(prepareMsg(msg, 'tooLong', { count: max }))
    }
    if (min !== null && value.length < min) {
      return Validators.formatMessage(prepareMsg(msg, 'tooShort', { count: min }))
    }
  })
})
 
export default length