all files / src/ inclusion.js

100% Statements 27/27
100% Branches 18/18
100% Functions 6/6
100% Lines 22/22
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   18×             38× 38× 38× 44×     38× 43× 43×   43×     43× 22× 14×     21× 14×          
import Validators from './validators'
import { prepareMsg, prepare, memoize } from './helpers'
 
let inclusion = memoize(function (options) {
  return inclusionExclusion(true, options)
})
 
export default inclusion
 
export function inclusionExclusion (
  inclusion,
  { in: inc, within, caseSensitive, message, msg, if: ifCond, unless, allowBlank }
) {
  msg = msg || message
  within = [].concat(within || inc).map(function (val) {
    return '' + val
  })
 
  return prepare(ifCond, unless, allowBlank, function (value) {
    let cs = caseSensitive != null ? caseSensitive : Validators.defaultOptions.caseSensitive
    let array = within
 
    if (!cs) {
      value = value.toLowerCase()
      array = array.map(function (val) {
        return val.toLowerCase()
      })
    }
    if (inclusion) {
      if (array.indexOf(value) < 0) {
        return Validators.formatMessage(prepareMsg(msg, 'inclusion'))
      }
    } else {
      if (array.indexOf(value) >= 0) {
        return Validators.formatMessage(prepareMsg(msg, 'exclusion'))
      }
    }
  })
}