UNPKG

haw-utils

Version:

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

82 lines (80 loc) 3.24 kB
(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.isChinese = 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; var chineseDictionary = { // 基本汉字 chineseBasic: "[\u4E00-\u9FA5]", // 基本汉字补充 chineseExtend: "[\u9EA6-\u9FEF]", // 汉字扩展A chineseExtendA: "[\u3400-\u4DB5]", // 汉字扩展B chineseExtendB: "[\uD840\uDC00-\uD869\uDED6]", // 汉字扩展C chineseExtendC: "[\uD869\uDF00-\uD86D\uDF34]", // 汉字扩展D chineseExtendD: "[\uD86D\uDF40-\uD86E\uDC1D]", // 汉字扩展E chineseExtendE: "[\uD86E\uDC20-\uD873\uDEA1]", // 汉字扩展F chineseExtendF: "[\uD873\uDEB0-\uD87A\uDFE0]" }; var looseChineseRegExp = chineseDictionary.chineseBasic + '+'; var chineseRegExp = '^' + chineseDictionary.chineseBasic + '+$'; var supportRegExpUnicode = RegExp.prototype.hasOwnProperty('unicode'); if (supportRegExpUnicode) { looseChineseRegExp = '(?:' + chineseDictionary.chineseBasic + '|' + chineseDictionary.chineseExtend + '|' + chineseDictionary.chineseExtendA + '|' + chineseDictionary.chineseExtendB + '|' + chineseDictionary.chineseExtendC + '|' + chineseDictionary.chineseExtendD + '|' + chineseDictionary.chineseExtendE + '|' + chineseDictionary.chineseExtendF + ')+'; chineseRegExp = '^(?:' + chineseDictionary.chineseBasic + '|' + chineseDictionary.chineseExtend + '|' + chineseDictionary.chineseExtendA + '|' + chineseDictionary.chineseExtendB + '|' + chineseDictionary.chineseExtendC + '|' + chineseDictionary.chineseExtendD + '|' + chineseDictionary.chineseExtendE + '|' + chineseDictionary.chineseExtendF + ')+$'; } /** * 检测值是否为中文 * * @static * @alias module:Validator.isChinese * @since 1.1.0 * @see {@link http://www.unicode.org/reports/tr38/#BlockListing|4.4 Listing of Characters Covered by the Unihan Database} * @param {String} value 要检测的值 * @param {Object} [options] 配置项 * @param {Boolean} [options.loose=false] 宽松模式。如果为true,只要包含中文即为true * @returns {Boolean} 值是否为中文 * @example * * isChinese('林某某'); * // => true * * isChinese('林A'); * // => false * * // 宽松模式,只要包含中文即为true * isChinese('林A', {loose: true}); * // => true * * isChinese('A林A', {loose: true}); * // => true * */ function isChinese(value) { var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, _ref$loose = _ref.loose, loose = _ref$loose === void 0 ? false : _ref$loose; var reg = new RegExp(loose ? looseChineseRegExp : chineseRegExp, supportRegExpUnicode ? 'u' : null); return reg.test(value); } var _default = isChinese; _exports["default"] = _default; });