haw-utils
Version:
一个基于业务场景的工具方法库
102 lines (94 loc) • 3.06 kB
JavaScript
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports"], factory);
} else if (typeof exports !== "undefined") {
factory(exports);
} else {
var mod = {
exports: {}
};
factory(mod.exports);
global.isBusinessLicense = mod.exports;
}
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports) {
"use strict";
Object.defineProperty(_exports, "__esModule", {
value: true
});
_exports["default"] = void 0;
// 基础规则,由14位数字本体码和1位数字校验码组成,其中本体码从左至右依次为:6位首次登记机关码、8位顺序码。
var baseReg = /^\d{15}$/;
/**
* 计算校验码
*
* @private
* @since 3.5.0
* @param {String} preCode 营业执照前14位
* @returns {String} 校验码
*/
function sumCheckCode(preCode) {
var retNum; // 校验位数字
var pj = 10; // Pj+1 11,初始为10
for (var j = 0; j < 14; j++) {
var sj = pj + Number(preCode[j]);
var sj10 = sj % 10;
sj10 = sj10 === 0 ? 10 : sj10;
var pj1 = sj10 * 2;
pj = pj1 % 11;
}
// 反模10计算
if (pj === 10 || pj === 1) {
retNum = 1;
} else {
retNum = 11 - pj;
}
return retNum;
}
/**
* 检测值是否为营业执照号,也叫工商注册号。由14位数字本体码和1位数字校验码组成,其中本体码从左至右依次为:6位首次登记机关码、8位顺序码。
*
* @static
* @alias module:Validator.isBusinessLicense
* @see {@link https://wenku.baidu.com/view/19873704cc1755270722087c.html|GS15—2006 工商行政管理市场主体注册号编制规则}
* @since 3.5.0
* @param {String} value 要检测的值
* @param {Object} [options] 配置项
* @param {Boolean} [options.loose=false] 宽松模式。如果为true,不校验校验位。
* @returns {Boolean} 值是否为营业执照号
* @example
*
* isBusinessLicense('310115600985533');
* // => true
*
* isBusinessLicense('3101156009');
* // => false
*
* isBusinessLicense('3101156009', { loose: true });
* // => false
*
* isBusinessLicense('310115600985535');
* // => false
*
* isBusinessLicense('310115600985535', { loose: true });
* // => true
*/
function isBusinessLicense(value) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref$loose = _ref.loose,
loose = _ref$loose === void 0 ? false : _ref$loose;
var passBaseRule = baseReg.test(value);
// 宽松模式 或 基础规则不通过直接返回
if (loose || !passBaseRule) {
return passBaseRule;
}
// 前14位
var preCode = value.substr(0, 14);
// 校验码
var lastCode = value.substr(-1);
// 计算校验码
var checkCode = sumCheckCode(preCode);
return lastCode === String(checkCode);
}
var _default = isBusinessLicense;
_exports["default"] = _default;
});