1.0.7 • Published 5 months ago

jl-tools v1.0.7

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

jl-tools

介绍

TypeScript编写的工具函数

支持 ESM | CommonJS | iife

安装

npm i jl-tools

使用

颜色处理

/** 获取随机颜色 */
export declare function getColor(): string;

/** 随机十六进制颜色数组 */
export declare function getColorArr(size: number): string[];

/** rgb转十六进制 */
export declare function rgbToHex(color: string): string;

/**
 * 淡化`RGBA`颜色透明度
 * @param color rgba(0, 239, 255, 1)
 * @param strength 降低的强度
 */
export declare function lightenRGBColor(color: string, strength?: number): string;

/**
 * 颜色添加透明度 支持`rgb`和十六进制
 * @param color 颜色
 * @param opacity 透明度
 * @returns 返回类似如下格式的颜色 `#ffffff11`
 */
export declare function colorAddOpacity(color: string, opacity?: number): string;

DOM 处理

/**
 * 根据原始设计稿宽度 等比例转换大小
 * @param px 像素大小
 * @param designSize 设计稿大小 默认`1920`
 * @param type 根据什么缩放 默认是宽度
 */
export declare function adaptPx(px: number | string, designSize?: number, type?: 'height' | 'width'): string | number;

/** 处理`CSS`单位 */
export declare function handleCssUnit(value: string | number): string | number;

/**
 * 将像素值转换为`vw`或`vh`单位 如果传入百分比值 则直接返回
 * @param px - 要转换的像素值或百分比值
 * @param designSize 设计稿大小 默认为1920像素
 * @param unit 尺寸单位 默认为`vw`
 * @returns 转换后的值 带有指定单位
 */
export declare function pxToVw(px: number | string, designSize?: number, unit?: 'vw' | 'vh'): string | number;

/** 节流 */
export declare function throttle<P extends any[], T, R>(fn: (this: T, ...args: P) => R, delay?: number): (this: T, ...args: P) => R;

/** 防抖 */
export declare function debounce<P extends any[], T, R>(fn: (this: T, ...args: P) => R, delay?: number): (this: T, ...args: P) => void;

/**
 * 根据半径和角度获取坐标
 * @param r 半径
 * @param deg 角度
 */
export declare function getCoord(r: number, deg: number): number[];

/**
 * 取出`canvas`用一维数组描述的颜色中 某个坐标的`RGBA`数组
 * @param x 宽度中的第几列
 * @param y 高度中的第几行
 * @param imgData CanvasImageData
 * @param width 图像区域宽度
 * @returns `RGBA`数组
 */
export declare function getPixel(x: number, y: number, imgData: number[], width: number): number[];

/** 获取选中的文本 */
export declare const getSelectedText: () => string;

/** 文本复制到剪贴板 */
export declare const copyToClipboard: (text: string) => Promise<void>;

/** 清除所有cookies */
export declare const clearCookie: void | (() => void);

/** 是否为深色模式 */
export declare const isDarkMode: string | boolean;

/** 是否滑倒页面底部 */
export declare function isToBottom(): boolean;

/** 获取所有样式表 */
export declare function getAllStyle(): Promise<string>;

/**
 * 打印
 * @param el 打印的元素
 * @param styleStr 样式 可不传
 * @param href 打开的链接 默认使用`location.href`
 */
export declare function print(el: HTMLElement, styleStr: string | undefined, href?: string): void;
export declare function print(elStr: string, styleStr: string | undefined, href?: string): void;

/** 判断页面所有图片是否加载完成 */
export declare function judgeImgLoad(doc?: Document): Promise<unknown[]>;

/**
 * 返回一个双击键盘事件
 * @param code 上下左右
 * @param fn 双击后执行函数
 * @param gap 间隔时间
 */
export declare function doubleKeyDown<T, P, R>(code: KeyCode, fn: (this: T, ...args: P[]) => R, gap?: number): (e: KeyboardEvent) => R;

常用工具函数

/** 获取类型 */
export declare const getType: (data: any) => string;

/** 是否为引用数据类型 */
export declare const isObject: (target: any) => boolean;

/** 随机长度为`10`的字符串 */
export declare const randomStr: () => string;

/** 今年的第几天 */
export declare const dayOfYear: (date?: Date) => number;

/** 时分秒 */
export declare const timeFromDate: (date: Date) => string;

/** 摄氏度转华氏度 */
export declare const celsiusToFahrenheit: (celsius: number) => number;

/** 华氏度转摄氏度 */
export declare const fahrenheitToCelsius: (fahrenheit: number) => number;

/** 日期间隔 */
export declare function dayDiff(date1: TimeType, date2: TimeType): number;

/** 判断是否能强转成数字 */
export declare function isPureNum(value: string | number): boolean;

/**
 * 获取随机范围整型数值 不包含最大值
 * @param min
 * @param max
 */
export declare function getRandomNum(min: number, max: number): number;

/** 深拷贝 */
export declare function deepClone<T>(data: T, map?: WeakMap<WeakKey, any>): any;

/** 深度比较对象 `Map | Set`无法使用 */
export declare function deepCompare(o1: any, o2: any, seen?: WeakMap<WeakKey, any>): boolean;

/** 递归树拍平 */
export declare function arrToTree(arr: TreeData[]): TreeData[];

/**
 * 把数组分成n块
 * @param arr 数组
 * @param size 每个数组大小
 * @returns 返回二维数组`any[][]`
 */
export declare function arrToChunk(arr: any[], size: number): any[][];

/** 蛇形转驼峰 */
export declare function toCamel<T>(obj: T): {};

/** 柯里化 */
export declare function curry(): any;

/**
 * 数字补齐精度
 * @param num 数字
 * @param precision 精度长度 默认`2`
 * @param placeholder 补齐内容 默认`0`
 * @returns
 */
export declare function padNum(num: string | number, precision?: number, placeholder?: string): string;

/**
 * 解决 Number.toFixed 计算错误
 * @example
 * 1.335.toFixed(2) => '1.33'
 * numFixed(1.335) => 1.34
 *
 * @param num 数值
 * @param precision 精度 默认 2
 */
export declare function numFixed(num: number, precision?: number): number;

/**
 * 日期补零 把`yyyy-MM-dd` 转成 `yyyy-MM-dd HH:mm:ss`
 * @param date 格式: `2016-06-10` 必须和它长度保持一致
 * @param placeholder 后面补充的字符串 默认`00:00:00`
 * @returns 如`2016-06-10 10:00:00`
 */
export declare function padDate(date: string, placeholder?: string | null): string;

/**
 * 把日期转为`Date`
 * @param date 日期
 */
export declare function getValidDate(date: Date | string | number): string | number | Date;

/**
 * 返回给定日期是否小于某年`一月一日` 默认去年
 * @param curDate 当前日期
 * @param yearLen 年份长度 默认`1` 即去年
 */
export declare function isLtYear(curDate: Date | string | number, yearLen?: number): boolean;

/**
 * 下载
 * @param data 数据
 * @param filename 文件名
 * @param ext 后缀
 */
export declare function downloadByData(data: BlobPart, filename: string, ext: string): void;

/** 把`http`协议转换成当前站的 */
export declare function matchProtocol(url: string): string;

/**
 * 用cdn链接下载
 * @param url 链接
 * @param fileName 文件名
 */
export declare function downloadByUrl(url: string, fileName: string): Promise<void>;
1.0.7

5 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

2 years ago