UNPKG

node-elizabeth

Version:
60 lines (50 loc) 1.84 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.asciiLetters = exports.digits = exports.asciiLowerCase = exports.asciiUpperCase = undefined; exports.pull = pull; exports.luhnChecksum = luhnChecksum; exports.uniform = uniform; var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _path = require('path'); var _path2 = _interopRequireDefault(_path); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function pull(file, locale) { try { var localeFilePath = _path2.default.join(__dirname, 'locales', locale, file); return JSON.parse(_fs2.default.readFileSync(localeFilePath, 'utf-8')); } catch (e) { throw new Error(locale + ' is unsupported'); } } function luhnChecksum(num) { var arr = []; num = num.toString().replace(/\D\-|\s|\/|\*/g, ''); for (var i = 0; i < num.length; i++) { if (i % 2 === 0) { var m = parseInt(num[i]) * 2; if (m > 9) { arr.push(m - 9); } else { arr.push(m); } } else { var n = parseInt(num[i]); arr.push(n); } } var sum = 0; arr.map(function (item) { return sum += item; }); return sum % 10; } function uniform(a, b, fixed) { return (Math.random() * (b - a) + a).toFixed(fixed) * 1; } var asciiUpperCase = exports.asciiUpperCase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; var asciiLowerCase = exports.asciiLowerCase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; var digits = exports.digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; var asciiLetters = exports.asciiLetters = [].concat(asciiUpperCase, asciiLowerCase);