feat(All):Initial
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Create a container.
|
||||
*/
|
||||
export function createDivContainer(): HTMLDivElement {
|
||||
const div = document.createElement("div");
|
||||
document.body.appendChild(div);
|
||||
return div;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* 下载blob二进制对象
|
||||
* @param blob
|
||||
* @param filename
|
||||
*/
|
||||
export function downloadBlob(blob, filename) {
|
||||
const link = document.createElement('a');
|
||||
|
||||
if (link.href) {
|
||||
URL.revokeObjectURL(link.href);
|
||||
}
|
||||
|
||||
link.href = URL.createObjectURL(blob);
|
||||
link.download = filename || 'data.json';
|
||||
link.dispatchEvent(new MouseEvent('click'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载ArrayBuffer对象
|
||||
* @param buffer
|
||||
* @param filename
|
||||
*/
|
||||
export function saveArrayBuffer(buffer, filename) {
|
||||
downloadBlob(new Blob([buffer], {type: 'application/octet-stream'}), filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载text文档
|
||||
* @param text
|
||||
* @param filename
|
||||
*/
|
||||
export function saveString(text, filename) {
|
||||
downloadBlob(new Blob([text], {type: 'text/plain'}), filename);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* 递归访问嵌套属性
|
||||
* @param {object} obj
|
||||
* @param {string} path 属性路径字符串,eg: "a.b.c"
|
||||
*/
|
||||
export function getNestedProperty(obj:object, path:string):any {
|
||||
return path.split('.').reduce((o, key) => o?.[key], obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转义正则特殊字符
|
||||
* @param {string} str
|
||||
*/
|
||||
export function escapeRegExp(str:string) {
|
||||
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统主题色
|
||||
*/
|
||||
export function getOsTheme(){
|
||||
const isDarkTheme = window.matchMedia("(prefers-color-scheme: dark)"); // 是深色
|
||||
if (isDarkTheme.matches) {
|
||||
return 'dark';
|
||||
} else {
|
||||
return "light";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取rem的px值
|
||||
*/
|
||||
export function remToPxNumber(rem: number): number {
|
||||
const f = parseFloat(document.documentElement.style.fontSize);
|
||||
return f * rem;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export * from './object';
|
||||
export * from './performance';
|
||||
export * from './helper';
|
||||
export * from './download';
|
||||
export * from './verify';
|
||||
export * from './dom';
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* 将对象source的值深度遍历赋值给target对象相同key
|
||||
* @param target
|
||||
* @param source
|
||||
*/
|
||||
export function deepAssign(target, source) {
|
||||
for (const key in source) {
|
||||
if (source.hasOwnProperty(key)) {
|
||||
if (typeof source[key] === 'object' && source[key] !== null && !Array.isArray(source[key])) {
|
||||
if (!target[key]){
|
||||
target[key] = {};
|
||||
}
|
||||
deepAssign(target[key], source[key]);
|
||||
} else {
|
||||
target[key] = source[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 防抖函数
|
||||
* @param {Function} func - 需要防抖的函数
|
||||
* @param {number} wait - 时间间隔(毫秒)
|
||||
* @returns {Function} - 返回一个防抖后的函数
|
||||
*/
|
||||
export function debounce(func, wait): (...args: any[]) => void {
|
||||
let timer: NodeJS.Timeout | null = null;
|
||||
return function(){
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
timer = setTimeout(() => {
|
||||
func(...arguments)
|
||||
}, wait);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 节流函数
|
||||
* @param {Function} func - 需要节流的函数
|
||||
* @param {number} wait - 时间间隔(毫秒),表示在这个时间间隔内最多执行一次函数
|
||||
* @returns {Function} - 返回一个节流后的函数
|
||||
*/
|
||||
export function throttle(func, wait:number):(...args: any[]) => void {
|
||||
// 上一次执行函数的时间戳,初始值为 0
|
||||
let lastTime = 0;
|
||||
|
||||
// 返回一个闭包函数,作为节流后的函数
|
||||
return function () {
|
||||
// 获取当前时间戳
|
||||
const now = Date.now();
|
||||
|
||||
// 如果当前时间与上一次执行时间的差值大于等于 wait,则执行函数
|
||||
if (now - lastTime >= wait) {
|
||||
// 更新上一次执行函数的时间戳
|
||||
lastTime = now;
|
||||
// 调用原始函数,并传入参数
|
||||
func(...arguments);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* 验证方法
|
||||
*/
|
||||
|
||||
export const IS_MAC = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
|
||||
|
||||
export const isNil = (v) => v === null || v === undefined;
|
||||
|
||||
// 判断是否是空对象,排除数组
|
||||
export const isEmptyObject = (obj:object) => typeof obj === 'object' && obj !== null && !Array.isArray(obj) && Object.keys(obj).length === 0;
|
||||
Reference in New Issue
Block a user