All files / lib/validations/validate/card brand.js

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                                      2x   2x   2x 351x 2005809x 52x   2005757x     39x 351x   2x                              
import {
  __,
  always,
  defaultTo,
  equals,
  find,
  head,
  ifElse,
  isEmpty,
  last,
  map,
  pipe,
  replace,
  toPairs,
  toString,
} from 'ramda'
 
import allBins from './bins'
 
const mapBins = map(__, allBins)
 
const clean = replace(/[^0-9]/g, '')
 
const cardNumberMatchBins = (cardNumber, brandBins) =>
  brandBins.reduce((acc, start) => {
    if (cardNumber.startsWith(start)) {
      return true
    }
    return acc
  }, false)
 
const makeCardMatcher = cardNumber => brandBins =>
  cardNumberMatchBins(cardNumber, brandBins)
 
const brand = pipe(
  makeCardMatcher,
  mapBins,
  toPairs,
  find(pipe(last, equals(true))),
  defaultTo(['unknown']),
  head,
)
 
export default pipe(
  toString,
  clean,
  ifElse(isEmpty, always('unknown'), brand)
)