UNPKG

@etsoo/smarterp-core

Version:

TypeScript APIs for SmartERP Core

66 lines (65 loc) 2.09 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CoreUtils = void 0; const ajv_formats_1 = __importDefault(require("ajv-formats")); /** * Core utilities */ var CoreUtils; (function (CoreUtils) { /** * Get avatar styles * 获取头像样式 * @param isOrg Is this an organization avatar? * @returns Styles */ function avatarStyles(isOrg = false) { return { width: "160px", height: isOrg ? "80px" : "160px", border: "1px solid #666" }; } CoreUtils.avatarStyles = avatarStyles; /** * Merge an array with another array, starting from the end * 合并数组,从末尾开始 * @param source Source array * @param target Target array */ function mergeArray(source, target) { for (let i = target.length - 1; i >= 0; i--) { const r = source[i]; if (!source.includes(r)) source.unshift(r); } } CoreUtils.mergeArray = mergeArray; let ajv = null; /** * Validate JSON input against a schema * 验证 JSON 输入是否符合架构 * @param schema JSON schema to validate against * @param input JSON input to validate * @returns Result */ async function validateJson(schema, input) { if (ajv == null) { const AjvImport = await import("ajv"); const AjvClass = AjvImport.Ajv ?? AjvImport.default; ajv = new AjvClass({ allErrors: true, strictTypes: false }); (0, ajv_formats_1.default)(ajv); } return [ ajv.validate(typeof schema === "string" ? JSON.parse(schema) : schema, typeof input === "string" ? JSON.parse(input) : input), ajv.errors ]; } CoreUtils.validateJson = validateJson; })(CoreUtils || (exports.CoreUtils = CoreUtils = {}));