UNPKG

slightning-coco-widget

Version:

SLIGHTNING 的 CoCo 控件框架。

138 lines (137 loc) 3.6 kB
import * as stringify from "@slightning/anything-to-string"; export function merge(target, ...sources) { for (const source of sources) { for (const [key, value] of Object.entries(source)) { if (Array.isArray(value)) { if (!Array.isArray(target[key])) { target[key] = []; } target[key] = target[key].push(...value); } else if (value != null && typeof value == "object") { if (target[key] == null || typeof target[key] != "object") { target[key] = {}; } merge(target[key], value); } else { target[key] = value; } } } return target; } export function capitalize(text) { return text.charAt(0).toUpperCase() + text.slice(1); } export function excludeBoolean(value) { if (typeof value == "boolean") { return null; } return value; } export function XMLEscape(text) { return text .replace(/\n/g, "&#10;") .replace(/\u0020/gu, "&#32;") .replace(/"/g, "&#34;") .replace(/'/g, "&#39;") .replace(/</g, "&#60;") .replace(/>/g, "&#62;") .replace(/\u2002/gu, "&#8194;") .replace(/\u2003/gu, "&#8195;") .replace(/\u00A0/gu, "&#160;") .replace(/ /g, "&#12288;"); } export function splitArray(array, separator) { const result = []; let part = []; for (const item of array) { if (item === separator) { result.push(part); part = []; continue; } part.push(item); } result.push(part); return result; } export function addMessageToError(message, error) { if (typeof error == "string") { return `${message}${error}`; } else if (Array.isArray(error)) { return [message, ...error]; } else { return [message, error]; } } export function errorToArray(error) { if (Array.isArray(error)) { return error; } else { return [error]; } } export function betterToString(data) { return stringify.default(data, { rules: stringify.Rules.LESSER, indent: " ", depth: 2 }); } export function CoCoLogToLineArray(message) { let lineArray = message.split("\n"); if (lineArray.length > 1) { lineArray[0] = `┼ ${lineArray[0]}`; for (let i = 1; i < lineArray.length; i++) { lineArray[i] = `│ ${lineArray[i]}`; } } return lineArray; } export class Range { constructor(left, right) { this.left = left; this.right = right; } includes(value) { if (Array.isArray(this.left)) { if (value < this.left[0]) { return false; } } else if (value <= this.left) { return false; } if (Array.isArray(this.right)) { if (value > this.right[0]) { return false; } } else if (value >= this.right) { return false; } return true; } toString() { let result = ""; if (Array.isArray(this.left)) { result += "[" + this.left[0]; } else { result += "(" + this.left; } result += ","; if (Array.isArray(this.right)) { result += this.right[0] + "["; } else { result += this.right + ")"; } return result; } }