const generateDigitWithMask = mask => pipe( take(length(mask)), split(''), mapIndexed((el, i) => el * mask[i]), reduce(add, 0), modulo(__, 11), subtract(11, __) )
const reduFinalSum = withoutLastDigit => (acc, digit, index, digits) => { const digitsMod = modulo(length(digits) - 1, 2) const indexMod = modulo(index, 2) const shouldUseIndex = cond([ [hasEvenDigitsLenghtAndEvenIndex, T], [hasOddDigitsLenghtAndOddIndex, T], [T, F], ])(digitsMod, indexMod) if (not(shouldUseIndex)) { return acc } const maskIndex = withoutLastDigit[index] return acc + mask[maskIndex] }
const validate = (cardNumber) => { const lastDigit = last(cardNumber) const withoutLastDigit = dropLast(1, cardNumber) const sumDigits = reduce(add, 0, withoutLastDigit) const digitsArray = withoutLastDigit.split('') const finalSum = digitsArray.reduceRight(reduFinalSum(withoutLastDigit), sumDigits) const toSubtract = modulo(finalSum, 10) let mod10 = subtract(10, toSubtract) if (mod10 === 10) { mod10 = 0 } return (mod10 === Number(lastDigit)) }
var log = require('./log'); var R = require('ramda'); var isOdd = R.modulo(R.__, 2); var getOdds = R.filter(isOdd); log( getOdds([1,2,3,4,5,6,7]) );
toString, addIndex, reduceRight, eqBy, juxt, init, sum, split, } from 'ramda' const reduceRightIndexed = addIndex(reduceRight) const clean = replace(/[^0-9]/g, '') const mask = [0, 1, 2, 3, 4, -4, -3, -2, -1, 0] const sameParity = eqBy(modulo(__, 2)) const finalSumReducer = (acc, digit, index, digits) => acc + (sameParity(digits.length - 1, index) ? mask[digits[index]] : 0) const validate = (cardNumber) => { const [withoutLastDigit, lastDigit] = juxt([init, last])(cardNumber) const finalSum = reduceRightIndexed( finalSumReducer, sum(withoutLastDigit), split('', withoutLastDigit) ) const rest = 10 - (finalSum % 10) return (rest === 10 ? 0 : rest) === parseInt(lastDigit, 10) } export default pipe(
const { inspect } = require('util'); const jsonparse = require('./json-parse'); const nestify = require('./nestify'); const nullify = require('./nullify-empty'); const TIMESTAMP_FORMAT = 'YYYY-MM-DD HH:mm:ss'; const bindError = _.curry((fn, sql, params) => fn.bind(null, sql, params)); const idMatch = _.curry((id, obj) => id + '' === obj.id + ''); const dateIsUnix = _.compose(_.equals(0), _.modulo(_.__, 1)); const momentFromString = (date) => moment(new Date(date)); const momentFromDate = _.cond([ [ _.isNil, moment ] , [ moment.isMoment, _.identity ] , [ dateIsUnix, moment.unix ] , [ _.T, momentFromString ] ]); const throwInsertFailedError = (sql, params) => { const param_string = inspect(params);
equals, last, length, modulo, not, pipe, reduce, replace, subtract, toString, } from 'ramda' const clean = replace(/[^0-9]/g, '') const isEven = pipe( modulo(__, 2), equals(0) ) const isOdd = pipe( isEven, not ) const hasEvenDigitsLenghtAndEvenIndex = (digitsMod, indexMod) => and(isEven(digitsMod), isEven(indexMod)) const hasOddDigitsLenghtAndOddIndex = (digitsMod, indexMod) => and(isOdd(digitsMod), isOdd(indexMod)) const mask = [0, 1, 2, 3, 4, -4, -3, -2, -1, 0]