UNPKG

util-helpers

Version:

一个基于业务场景的工具方法库

36 lines (32 loc) 1.02 kB
'use strict'; var ut2 = require('ut2'); var regIdCard = /^[1-9]\d{5}(19|20)?\d{2}((0[1-9])|(1[012]))(([0-2][1-9])|10|20|30|31)\d{3}(\d|X)?$/i; function check(id) { var index, sum; for (sum = index = 0; index < 17; index++) { sum += (Math.pow(2, 17 - index) % 11) * Number(id[index]); } var num = (12 - (sum % 11)) % 11; if (num < 10) { return num === Number(id[17]); } else { return id[17].toUpperCase() === 'X'; } } function isIdCard(value, options) { if (options === void 0) { options = {}; } var _a = options.loose, loose = _a === void 0 ? false : _a, _b = options.checkCode, checkCode = _b === void 0 ? true : _b; var valueStr = ut2.toString(value); if (valueStr.length === 15 && loose) { return regIdCard.test(valueStr); } if (valueStr.length === 18 && regIdCard.test(valueStr)) { if (checkCode) { return check(valueStr); } return true; } return false; } module.exports = isIdCard;