/** * {} * @author yutent * @date 2024/03/06 09:55:31 */ import { xlink, xmlns, xhtmlns, HTML_TAGS, SPEC_ATTR, doc, win } from './lib/constants.js' export function uuid(prefix = '') { return prefix + Math.random().toString(16).slice(-8) } export function noop() {} //驼峰转换为连字符线风格 export function hyphen(target) { return target.replace(/([a-z\d])([A-Z]+)/g, '$1-$2').toLowerCase() } //连字符转换为驼峰风格 export function camelize(target) { //提前判断,提高效率 if (target.indexOf('-') < 0) { return target } return target.replace(/\-([a-z])/g, (m, s) => s.toUpperCase()) } export function type(o) { if (o === null) { return null } return Object.prototype.toString.call(o).slice(8, -1).toLowerCase() } export function $(selector, container, multi) { let fn = multi ? 'querySelectorAll' : 'querySelector' if (container) { return container[fn](selector) } return document.body[fn](selector) } export function $$(selector, container) { return $(selector, container, true) } export function h(el, props = null, children) { if (typeof el === 'string') { el = doc.createElementNS(HTML_TAGS[el] ? xhtmlns : xmlns, el) } if (props) { for (let key in props) { let val = props[key] if (val === void 0) { continue } if (!SPEC_ATTR[key]) { key = hyphen(key) } if (val === null) { el.removeAttribute(key) } else { el.setAttribute(key, val) } } } if (children) { if (Array.isArray(children)) { children = children.join('') } if (el.tagName.toLowerCase() === 'foreignobject') { let f = h('div', null, children) el.appendChild(f) } else { el.appendChild(doc.createTextNode(children)) } el.normalize() } return el } window.h = h export function clone(obj) { if (typeof obj == 'function' || Object(obj) !== obj) { return obj } let res = new obj.constructor() for (let key in obj) if (obj.hasOwnProperty(key)) { res[key] = clone(obj[key]) } return res } export function preload(src, callback) { let img = new Image() img.onload = function () { callback(img) } img.src = src }