fastchar-appjs
Version:
快速搭建VUE项目工具类的基本库,主要用于每个功能页面独立生成html,不使用vue单页面功能。
434 lines (433 loc) • 13.4 kB
TypeScript
/**
* FastHelper 常用的工具类
* @author Janesen
*/
export declare namespace FastHelper {
/**
* base64相关
*/
class Base64 {
/**
* 转码base64
* @param content
*/
static encode(content: any): string;
/**
* 解码base64
* @param content
*/
static decode(content: string): string;
/**
* 将base64转为file对象
* @param content base64内容
* @param fileName 文件名
*/
static base64ToFile(content: string, fileName: string): File;
/**
* 将file对象转为base64
* @param file
*/
static fileToBase64(file: File): Promise<unknown>;
}
/**
* 对象操作类
*/
class Objects {
/**
* 判断目标是否为对象类型
* @param source
*/
static isObject(source: any): boolean;
/**
* 判断是否为空,包含undefined
* @param source 目标值
*/
static isEmpty(source: any): boolean;
/**
* 判断目标是否没有任何属性值,如果为空数组,则数组有实体属性length
* @param source
*/
static isEmptyProperty(source: any): boolean;
/**
* 判断目标是否没有任何key
* @param source
*/
static isEmptyKey(source: any): boolean;
/**
* 判断两个对象是否相同,深度判断属性中的值是否相同
* 如果比较的属性类型都为函数类型,则跳过比较
* @param first
* @param second
*/
static isSame(first: any, second: any): boolean;
}
/**
* 数组相关
*/
class Arrays {
/**
* 判断目标是否为数组类型
* @param source
*/
static isArray(source: any): boolean;
/**
* 合并数组,返回一个新的数组
* @param items
*/
static merge(...items: any[]): any[];
/**
* 批量追加
* @param source 原数组
* @param items 添加的选项,如果选项类型为数组,则合并到数组中
*/
static append(source: any[], ...items: any[]): void;
/**
* 清空数组
* @param source
*/
static clear(source: any[]): void;
/**
* 判断是否存在于数组中
* @param source 数组
* @param value 值
*/
static exists(source: any[], value: any): boolean;
/**
* 获取值在数组的下标
* @param source 数组
* @param value 值
*/
static indexOf(source: any[], value: any): number;
/**
* 将值插入指定的位置
* @param source 数组
* @param index 位置
* @param value 值
*/
static insert(source: any[], index: number, value: any): void;
/**
* 将值替换到指定下标
* @param source 数组
* @param index 位置
* @param value 值
*/
static replace(source: any[], index: number, value: any): void;
/**
* 删除指定位置的数据
* @param source 数组
* @param index 下标
*/
static remove(source: any[], index: number): void;
/**
* 删除指定值
* @param source 数组
* @param value 值
*/
static removeItem(source: any[], value: any): void;
/**
* 判断两个数组是否相同
* 如果比较的值类型都为函数类型,则跳过比较
* @param first
* @param second
*/
static isSame(first: any[], second: any[]): boolean;
}
/**
* 字符串相关
*/
class Strings {
/**
* 构建唯一标识符
* @param prefix 前缀
*/
static buildOnlyCode(prefix: string): string;
/**
* 判断目标值是否为字符串
* @param source
*/
static isString(source: any): boolean;
/**
* 判断是否为空,包含undefined
* @param source 目标值
*/
static isEmpty(source: any): boolean;
/**
* 判断是否不为空
* @param source 目标值
*/
static isNotEmpty(source: any): boolean;
/**
* 获取字符串内容,如果为null或undefined则返回defaultValue
* @param source 目标值
* @param defaultValue 默认值
*/
static defaultValue(source: any, defaultValue: any): any;
/**
* 判断字符串是否以某个字符开头
* @param source 字符串
* @param prefix 字符
*/
static startWith(source: string, prefix: string): boolean;
/**
* 判断字符串是否以某个字符结尾
* @param source 字符串
* @param suffix 字符
*/
static endWith(source: string, suffix: string): boolean;
/**
* 首字母大写
* @param source 字符串
*/
static firstUpperCase(source: string): string;
/**
* 替换字符
* @param source 字符串
* @param oldStr 老的字符
* @param newStr 新的字符
*/
static replaceAll(source: string, oldStr: string, newStr: string): string;
/**
* 数字补0
* @param source
* @param length
*/
static prefixInteger(source: any, length: number): string;
}
/**
* 布尔值相关操作
*/
class Booleans {
/**
* 判断目标值是否为布尔值
* @param source
*/
static isBoolean(source: any): boolean;
/**
* 转为布尔值
* @param source 目标值
* @param defaultValue 默认值,当为空或无效的布尔值时 返回
*/
static parse(source: any, defaultValue?: boolean): boolean;
}
/**
* 数字相关
*/
class Numbers {
/**
* 判断目标值是否为布尔值
* @param source
*/
static isNumber(source: any): boolean;
/**
* 转为纯数字
* @param source
* @param defaultValue
*/
static parse(source: any, defaultValue?: number): number;
/**
* 提取纯数字[0-9],不包含小数点
* @param source 目标值
*/
static getNumberValue(source: any): number;
}
/**
* 日期相关
*/
class Dates {
/**
* 判断目标值是否是日期类型
* @param source
*/
static isDate(source: any): boolean;
/**
* 格式化日期
* @param source 日期
* @param pattern 格式类型,例如:yyyy-MM-DD HH:mm:ss,更多查看:https://momentjs.com/docs/#/displaying/
*/
static format(source: Date, pattern: string): string | null;
/**
* 将字符串日期转换为Date对象
* @param source 日期值
*/
static parse(source: string): Date | null;
/**
* 格式化日期的友好展示,例如:1分钟前,3分钟前
* @param source
* @param level 精确级别 1 分钟 2 小时 3 天 4 周 5 月
*/
static formatNice(source: string, level?: number): string | null;
/**
* 将总时间转换为中文描述,最高描述到:天,例如:2天03时45分09秒
* @param timestamp 时间戳 单位:毫秒
* @param chinese 是否用中文表达,默认:true
*/
static toDescription(timestamp: number, chinese: boolean): string;
}
/**
* 函数相关操作
*/
class Functions {
/**
* 判断目标类型是否为函数
* @param source
*/
static isFunction(source: any): boolean;
/**
* 批量执行函数并获取返回值
* @param source 单个函数或函数数组
* @param params 执行函数携带的参数
*/
static run(source: any, ...params: any[]): any | null;
/**
* 按顺序同步执行函数,当其中一个函数返回false时,则终止后续执行
* @param source 单个函数或函数数组
* @param params 执行函数携带的参数
*/
static syncRun(source: any, ...params: any[]): Promise<void>;
}
/**
* 节点相关
*/
class Elements {
/**
* 绑定节点属性变动的监听
* @param source 节点对象
* @param attrs 需要监听的属性集合
* @param callBack 回调
*/
static onAttributesChange(source: Element, attrs: any[], callBack?: (event: MutationRecord) => void): MutationObserver | null;
/**
* 绑定当前节点子节点的变动的监听
* @param source 节点对象
* @param callBack 回调
*/
static onChildrenChange(source: Element, callBack?: (event: MutationRecord) => void): MutationObserver | null;
/**
* 在目标节点后追加新的节点
* @param targetEl 目标节点
* @param newEl 新的节点
*/
static insertAfter(targetEl: Element, newEl: Element): void;
/**
* 在目标节点前追加新的节点
* @param targetEl 目标节点
* @param newEl 新的节点
*/
static insertBefore(targetEl: Element, newEl: Element): void;
/**
* 获取目标节点的实际占位空间,包含的内外边距
* @param targetEl
*/
static getRealSize(targetEl: Element): {
width: number;
height: number;
};
/**
* 获取目标节点的selector
* @param targetEl
*/
static getPath(targetEl: Element): string;
/**
* 判断目标元素的滚动条 是否已滚动到底部
* @param targetEl
* @param scrollNimble 检测的灵敏度,越大越灵敏
*/
static isScrollBottom(targetEl: Element, scrollNimble: number): boolean;
}
/**
* css样式相关代码
*/
class Styles {
/**
* 动态加载css代码
* @param style css代码
*/
static loadCssCode(style: string): void;
}
/**
* 事件相关工具
*/
class Events {
/**
* 获取异常信息的堆栈信息
* @param event
*/
static geErrorInfo(event: any): string;
}
/**
* 高德地图操作工具类
*/
class AMap {
/**
* 加载高德地图的js文件,具体查看 https://lbs.amap.com/api/jsapi-v2/guide/abc/load
* @param options
*/
static load(options: any): Promise<any> | null;
}
/**
* 窗口window操作对象
*/
class Windows {
/**
* 复制文本到剪贴板里
* @param content 内容
*/
static copyToBoard(content: string): void;
/**
* 添加window的onLoad事件,如果存在onLoad函数名则进行合并
* @param callBack
*/
static addOnLoad(callBack: () => void): void;
/**
* 添加window的函数,如果存在相同的函数名则进行合并
* @param functionName 函数名
* @param functionBody 函数执行的代码
*/
static addFunction(functionName: string, functionBody: (params: any) => void): void;
/**
* 安全的获取窗口宽度,有的手机渲染后document.body.clientWidth 莫明会返回0
*/
static safeGetClientWidth(): number;
/**
* 安全的获取窗口高度,有的手机渲染后document.body.clientHeight 莫明会返回0
*/
static safeGetClientHeight(): number;
}
/**
* JSON相关功能
*/
class Json {
private constructor();
/**
* 将json字符串转成对象
* @param jsonStr json字符串
* @returns {Object}
*/
static jsonToObject(jsonStr: string): any;
/**
* 将对象转成json字符串
* @param jsonObj 待转换的对象
* @returns {string}
*/
static objectToJson(jsonObj: any): string | null;
/**
* 将对象转成json字符串,注意此方法将转换函数对象,慎用!
* @param jsonObj 待转换的对象
* @return {string}
*/
static objectToJsonUnsafe(jsonObj: any): string;
/**
* 将json字符串转成对象
* @param jsonStr json字符串
* @returns {Object}
*/
static jsonToObjectUnsafe(jsonStr: string): any;
/**
* 合并两个json对象
* @param jsonData1 json对象
* @param jsonData2 json对象
* @return 合并后的新对象
*/
static mergeJson(jsonData1: any, jsonData2: any): any;
}
}