UNPKG

@cmtlyt/cl-utils

Version:
233 lines (232 loc) 9.29 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "./baseFunc"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkTypeHandle = exports.useTypeCheckHandle = void 0; const baseFunc_1 = require("./baseFunc"); const isNaN = value => { return value !== value; }; const isNull = value => { return value === null; }; const isUndefined = value => { return value === undefined; }; const isEmpty = value => { var _a; const type = (0, baseFunc_1.getType)(value); const handleMap = { array(value) { return !value.length; }, string(value) { return !value.length; }, object(value) { return !(Object.getOwnPropertyNames(value).length + Object.getOwnPropertySymbols(value).length); }, }; return isUndefined(value) || isNull(value) || isNaN(value) || ((_a = handleMap[type]) === null || _a === void 0 ? void 0 : _a.call(handleMap, value)) || false; }; function checkValueType(value, type) { return type === 'any' ? true : (0, baseFunc_1.checkInString)(type)((0, baseFunc_1.getType)(value)); } function checkListPropsTyps(options) { const { value: list, subProps: configList, subPropsType: allItemType } = options; let res = { result: true }; // 如果存在subProps的话先验证subProps的配置 if (configList.length) { const checkResult = configList.every((config, idx) => { res = checkTypeFromConfig(list[idx], config); return res.result; }); if (!checkResult) return res; } // 如果存在所有项的统一类型的话就对所有类型进行验证 if (allItemType) { const checkResult = list.every(item => { res = checkTypeFromConfig(item, allItemType); return res.result; }); if (!checkResult) return res; } // 如果通过了前面的校验则直接返回校验成功的结果 return { result: true }; } function checkObjctPropsType(options) { const { value: obj, subProps: configList, subPropsType: allItemType } = options; let res = { result: true }; const checkPassKeyList = []; const checkResult = configList.every(config => { if (!config.key) { res.result = false; return false; } checkPassKeyList.push(config.key); res = checkTypeFromConfig(obj[config.key], config); return res.result; }); if (!checkResult) { return res; } if (allItemType) { const checkResult = Object.keys(obj).every(key => { res = checkTypeFromConfig(obj[key], allItemType); return res.result; }); if (!checkResult) return res; } return { result: true }; } function checkStringPropType(options) { const { value: str, regexp } = options; let res = { result: true }; const checkResult = regexp === null || regexp === void 0 ? void 0 : regexp.every(reg => { const nReg = new RegExp(reg); res = { result: false, message: `${str}未通过${nReg.toString()}验证` }; return nReg.test(str); }); if (!checkResult) { return res; } return { result: true }; } const isSimpleType = (0, baseFunc_1.checkInString)('number,boolean'); function checkIsSimpleType(value, config) { const valueType = (0, baseFunc_1.getType)(value); return isSimpleType(valueType) && !config.subProps && !config.regexp; } function checkTypeFromConfig(value, config) { // 如果nullable不为空,并且为false // required为true if ((!isEmpty(config.nullable) && !config.nullable && isEmpty(value)) || (config.required && isEmpty(value) && !isNaN(value))) { return { result: false, message: `不能为空\nvalue:>${value}\nconfig:${JSON.stringify(config)}`, getProps: () => ({ value, config }), }; } else if (config.nullable && isEmpty(value)) { return { result: true }; } if (!checkValueType(value, config.type)) { return { result: false, message: `value:>${value}\nconfig:${JSON.stringify(config)}`, getProps: () => ({ value, config }), }; } if (checkIsSimpleType(value, config)) { return { result: true, }; } return complexCheck(value, config); } const checkHandleMap = { array: checkListPropsTyps, object: checkObjctPropsType, string: checkStringPropType, }; function complexCheck(value, config) { const valueType = (0, baseFunc_1.getType)(value); const checkHandle = checkHandleMap[valueType]; if (!checkHandle) { console.warn(`暂不支持${valueType}类型数据的复杂验证`); return { result: true }; } return checkHandle({ value, subProps: config.subProps || [], subPropsType: config.subPropsType, regexp: config.regexp, }); } function generateCheckFunc(callback, typeConfig) { return function check(inputProps) { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { const { result: typeCheckResult, message, getProps, } = checkListPropsTyps({ value: inputProps, subProps: typeConfig.props }); if (!typeCheckResult) { console.error(message); return reject(getProps === null || getProps === void 0 ? void 0 : getProps()); } try { const callbackResult = yield callback(...inputProps); // 存在返回值类型校验 if (typeConfig.return) { const { result: returnTypeCheckResult, message, getProps, } = checkTypeFromConfig(callbackResult, typeConfig.return); if (!returnTypeCheckResult) { console.error(message); return reject(getProps === null || getProps === void 0 ? void 0 : getProps()); } } resolve(callbackResult); } catch (e) { return reject(e); } })); }; } /** * 封装函数,使其具有类型校验功能 * @param {function} callback * @param {object} typeConfig * @param {object[]} typeConfig.props * @param {object?} typeConfig.return * @returns {function} */ function useTypeCheckHandle(callback, typeConfig) { const checkFunc = generateCheckFunc(callback, typeConfig); return (...args) => checkFunc(args); } exports.useTypeCheckHandle = useTypeCheckHandle; function __checkTypeHandle(data, typeConfig) { const dataType = (0, baseFunc_1.getType)(data); const configType = (0, baseFunc_1.getType)(typeConfig); let res = { result: true }; const checkHandle = isSimpleType(dataType) ? checkTypeFromConfig : complexCheck; if (configType === 'array') { ; typeConfig.every(config => { res = checkHandle(data, config); return res.result; }); } else { res = checkHandle(data, typeConfig); } return res.result; } /** * 直接检查数据类型 * @param {any} data * @param {TArray<ITypeConfig> | ITypeConfig} typeConfig * @returns {boolean} */ function checkTypeHandle(data, typeConfig) { return __checkTypeHandle(data, typeConfig); } exports.checkTypeHandle = checkTypeHandle; });