2023-03-30 23:49:57 +08:00
|
|
|
export const isString = (value: any): value is string => {
|
|
|
|
return typeof value === 'string';
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isArray = (value: any): value is Array<any> => {
|
|
|
|
return Array.isArray(value);
|
|
|
|
};
|
|
|
|
|
2023-03-20 17:40:16 +08:00
|
|
|
export const isEmpty = (value: unknown) => {
|
|
|
|
if (isPlainObject(value)) {
|
|
|
|
return Object.keys(value).length === 0;
|
|
|
|
}
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
return value.length === 0;
|
|
|
|
}
|
|
|
|
return !value;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isPlainObject = (value) => {
|
|
|
|
if (Object.prototype.toString.call(value) !== '[object Object]') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const prototype = Object.getPrototypeOf(value);
|
|
|
|
return prototype === null || prototype === Object.prototype;
|
|
|
|
};
|
2023-03-30 23:49:57 +08:00
|
|
|
|
|
|
|
export const hasEmptyValue = (objOrArr: object | any[]) => {
|
|
|
|
let result = true;
|
|
|
|
for (const key in objOrArr) {
|
|
|
|
result = false;
|
|
|
|
if (isArray(objOrArr[key]) && objOrArr[key].length === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!objOrArr[key]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (isPlainObject(objOrArr[key]) || isArray(objOrArr[key])) {
|
|
|
|
return hasEmptyValue(objOrArr[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
2023-11-14 14:15:47 +08:00
|
|
|
|
|
|
|
export const nextTick = (fn: () => void) => {
|
|
|
|
setTimeout(fn);
|
|
|
|
};
|
2024-04-15 19:03:29 +08:00
|
|
|
|
|
|
|
export function fuzzysearch(needle: string, haystack: string): boolean {
|
|
|
|
const hlen = haystack.length;
|
|
|
|
const nlen = needle.length;
|
|
|
|
if (nlen > hlen) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (nlen === hlen) {
|
|
|
|
return needle === haystack;
|
|
|
|
}
|
|
|
|
outer: for (let i = 0, j = 0; i < nlen; i++) {
|
|
|
|
const nch = needle.charCodeAt(i);
|
|
|
|
while (j < hlen) {
|
|
|
|
if (haystack.charCodeAt(j++) === nch) {
|
|
|
|
continue outer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|