@antdv/pro-utils
Version:
@antdv/pro-utils
210 lines (209 loc) • 6.62 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
bindTo: () => bindTo,
getNativeType: () => getNativeType,
getType: () => getType,
has: () => has,
hasOwn: () => hasOwn,
indent: () => indent,
isComplexType: () => isComplexType,
isInteger: () => isInteger,
isPlainObject: () => isPlainObject,
isVueTypeDef: () => isVueTypeDef,
toType: () => toType,
toValidateType: () => toValidateType,
validateType: () => validateType,
warn: () => warn
});
module.exports = __toCommonJS(stdin_exports);
var import_is_plain_object = require("is-plain-object");
var import_is = require("../is");
const FN_MATCH_REGEXP = /^\s*function (\w+)/;
const ObjProto = Object.prototype;
const isPlainObject = import_is_plain_object.isPlainObject;
let warn = () => {
};
if (process.env.NODE_ENV !== "production") {
const hasConsole = typeof console !== "undefined";
warn = hasConsole ? (msg) => console.warn(`[Antdv ProComponent warn]: ${msg}`) : () => {
};
}
const hasOwn = ObjProto.hasOwnProperty;
const isInteger = Number.isInteger || function isInteger2(value) {
return typeof value === "number" && Number.isFinite(value) && Math.floor(value) === value;
};
function has(obj, prop) {
return hasOwn.call(obj, prop);
}
function toValidateType(name, obj) {
const type = toType(name, obj);
return Object.defineProperty(type, "validate", {
value(fn) {
if ((0, import_is.isFunction)(this.validator)) {
warn(
`${this._vueTypes_name} - calling .validate() will overwrite the current custom validator function. Validator info:
${JSON.stringify(
this
)}`
);
}
this.validator = bindTo(fn, this);
return this;
}
});
}
function toType(name, obj) {
const type = Object.defineProperties(obj, {
_vueTypes_name: {
value: name,
writable: true
},
isRequired: {
get() {
this.required = true;
return this;
}
},
def: {
value(def) {
if (def === void 0) {
if (this.type === Boolean || Array.isArray(this.type) && this.type.includes(Boolean)) {
this.default = void 0;
return;
}
if (has(this, "default")) {
delete this.default;
}
return this;
}
if (!(0, import_is.isFunction)(def) && validateType(this, def, true) !== true) {
warn(`${this._vueTypes_name} - invalid default value: "${def}"`);
return this;
}
if ((0, import_is.isArray)(def)) {
this.default = () => [...def];
} else if (isPlainObject(def)) {
this.default = () => Object.assign({}, def);
} else {
this.default = def;
}
return this;
}
}
});
const { validator } = type;
if ((0, import_is.isFunction)(validator)) {
type.validator = bindTo(validator, type);
}
return type;
}
function bindTo(fn, ctx) {
return Object.defineProperty(fn.bind(ctx), "__original", {
value: fn
});
}
function isVueTypeDef(value, name) {
return isPlainObject(value) && has(value, "_vueTypes_name") && (!name || value._vueTypes_name === name);
}
function isComplexType(value) {
return isPlainObject(value) && (has(value, "type") || ["_vueTypes_name", "validator", "default", "required"].some(
(k) => has(value, k)
));
}
function getType(fn) {
var _a;
const type = (_a = fn == null ? void 0 : fn.type) != null ? _a : fn;
if (type) {
const match = type.toString().match(FN_MATCH_REGEXP);
return match ? match[1] : "";
}
return "";
}
function getNativeType(value) {
if (value === null || value === void 0) return "";
const match = value.constructor.toString().match(FN_MATCH_REGEXP);
return match ? match[1].replace(/^Async/, "") : "";
}
function validateType(type, value, silent = false) {
let typeToCheck;
let valid = true;
let expectedType = "";
if (!isPlainObject(type)) {
typeToCheck = { type };
} else {
typeToCheck = type;
}
const namePrefix = isVueTypeDef(typeToCheck) ? `${typeToCheck._vueTypes_name} - ` : "";
if (isComplexType(typeToCheck) && typeToCheck.type !== null) {
if (typeToCheck.type === void 0 || typeToCheck.type === true) {
return valid;
}
if (!typeToCheck.required && value == null) {
return valid;
}
if ((0, import_is.isArray)(typeToCheck.type)) {
valid = typeToCheck.type.some(
(_type) => validateType(_type, value, true) === true
);
expectedType = typeToCheck.type.map((_type) => getType(_type)).join(" or ");
} else {
expectedType = getType(typeToCheck);
if (expectedType === "Array") {
valid = (0, import_is.isArray)(value);
} else if (expectedType === "Object") {
valid = isPlainObject(value);
} else if (expectedType === "String" || expectedType === "Number" || expectedType === "Boolean" || expectedType === "Function") {
valid = getNativeType(value) === expectedType;
} else {
valid = value instanceof typeToCheck.type;
}
}
}
if (!valid) {
const msg = `${namePrefix}value "${value}" should be of type "${expectedType}"`;
if (silent === false) {
warn(msg);
return false;
}
return msg;
}
if (has(typeToCheck, "validator") && (0, import_is.isFunction)(typeToCheck.validator)) {
const oldWarn = warn;
const warnLog = [];
warn = (msg) => {
warnLog.push(msg);
};
valid = typeToCheck.validator(value);
warn = oldWarn;
if (!valid) {
const msg = (warnLog.length > 1 ? "* " : "") + warnLog.join("\n* ");
warnLog.length = 0;
if (silent === false) {
warn(msg);
return valid;
}
return msg;
}
}
return valid;
}
function indent(string) {
return string.replace(/^(?!\s*$)/gm, " ");
}