@etsoo/smarterp-core
Version:
TypeScript APIs for SmartERP Core
60 lines (59 loc) • 1.79 kB
JavaScript
import addFormats from "ajv-formats";
/**
* Core utilities
*/
export 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
});
addFormats(ajv);
}
return [
ajv.validate(typeof schema === "string" ? JSON.parse(schema) : schema, typeof input === "string" ? JSON.parse(input) : input),
ajv.errors
];
}
CoreUtils.validateJson = validateJson;
})(CoreUtils || (CoreUtils = {}));