UNPKG

haw-utils

Version:

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

90 lines (89 loc) 4.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.isPassword = 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; /** * 检测值是否符合密码强度 * 注意该校验只校验是否存在不同字符(大小写字母、数字、特殊符号),不判断长度 * * @static * @alias module:Validator.isPassword * @since 1.1.0 * @param {String} value 要检测的值 * @param {Object} [options] 配置项 * @param {Number} [options.level=2] 密码强度 1-包含一种字符 2-包含两种字符 3-包含三种字符。(大写字母、小写字母、数字、特殊字符) * @param {Boolean} [options.ignoreCase=false] 忽略大小写,即大小写字母视为一种字符 * @param {String} [options.special=-_!@#$%^&*] 特殊字符 * @returns {Boolean} 值是否符合密码强度 * @example * * isPassword('a12345678'); * // => true * * isPassword('a12345678', {level: 3}); * // => false * * isPassword('Aa12345678', {level: 3}); * // => true * * isPassword('Aa12345678', {level: 3, ignoreCase: true}); * // => false * * isPassword('_Aa12345678', {level: 3, ignoreCase: true}); * // => true * * // 仅支持 数字、字母、特殊字符,其他字符如中文字符是校验不通过的 * isPassword('_Aa一二三45678', {level: 3, ignoreCase: true}); * // => false * * isPassword(' _Aa12345678', {level: 3, ignoreCase: true}); * // => false */ function isPassword(value) { var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, _ref$level = _ref.level, level = _ref$level === void 0 ? 2 : _ref$level, _ref$ignoreCase = _ref.ignoreCase, ignoreCase = _ref$ignoreCase === void 0 ? false : _ref$ignoreCase, _ref$special = _ref.special, special = _ref$special === void 0 ? "-_!@#$%^&*" : _ref$special; var numRegStr = "\\d"; var lowercaseRegStr = "a-z"; var uppercaseRegStr = "A-Z"; var ignorecaseRegStr = "a-zA-Z"; var reg = null; if (level === 1) { reg = new RegExp("^[".concat(numRegStr).concat(ignorecaseRegStr).concat(special, "]+$")); } else if (level === 2) { if (ignoreCase) { reg = new RegExp("^(?![".concat(ignorecaseRegStr, "]+$)(?!").concat(numRegStr, "+$)(?![").concat(special, "]+$)[").concat(ignorecaseRegStr).concat(numRegStr).concat(special, "]+$")); } else { reg = new RegExp("^(?![".concat(lowercaseRegStr, "]+$)(?![").concat(uppercaseRegStr, "]+$)(?!").concat(numRegStr, "+$)(?![").concat(special, "]+$)[").concat(ignorecaseRegStr).concat(numRegStr).concat(special, "]+$")); } } else if (level === 3) { if (ignoreCase) { // ^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)(?![a-zA-z\d]+$)(?![a-zA-z!@#$%^&*]+$)(?![\d!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$ reg = new RegExp("^(?![".concat(ignorecaseRegStr, "]+$)(?!").concat(numRegStr, "+$)(?![").concat(special, "]+$)(?![").concat(ignorecaseRegStr).concat(numRegStr, "]+$)(?![").concat(ignorecaseRegStr).concat(special, "]+$)(?![").concat(numRegStr).concat(special, "]+$)[").concat(ignorecaseRegStr).concat(numRegStr).concat(special, "]+$")); } else { reg = new RegExp("^(?![".concat(lowercaseRegStr, "]+$)(?![").concat(uppercaseRegStr, "]+$)(?!").concat(numRegStr, "+$)(?![").concat(special, "]+$)(?![").concat(lowercaseRegStr).concat(numRegStr, "]+$)(?![").concat(uppercaseRegStr).concat(numRegStr, "]+$)(?![").concat(lowercaseRegStr).concat(special, "]+$)(?![").concat(uppercaseRegStr).concat(special, "]+$)(?![").concat(numRegStr).concat(special, "]+$)[").concat(ignorecaseRegStr).concat(numRegStr).concat(special, "]+$")); } } return reg ? reg.test(value) : false; } var _default = isPassword; _exports["default"] = _default; });