1.0.3 • Published 5 months ago

lby-utils v1.0.3

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

lby-utils

安装

使用 npm 安装

npm install lby-utils

使用 yarn 安装

yarn add lby-utils

引用包

在 Node.js 中使用

const { arrEqual } = require('lby-utils');

// 示例
const result = arrEqual([1, 2, 3], [1, 2, 3]);
console.log(result); // true

或者

const lbyUtils = require('lby-utils');

// 示例
const result = lbyUtils.arrEqual([1, 2, 3], [1, 2, 3]);
console.log(result); // true

在 ES Module 中使用

import { arrEqual } from 'lby-utils';

// 示例
const result = arrEqual([1, 2, 3], [1, 2, 3]);
console.log(result); // true

在浏览器中使用

可以通过 CDN 引入:

<script src="https://unpkg.com/lby-utils"></script>
<script>
  const result = lbyUtils.arrEqual([1, 2, 3], [1, 2, 3]);
  console.log(result); // true
</script>

目录

  • 数组操作
    • arrEqual - 比较两个数组是否相等
    • arrIncludes - 判断数组中是否包含某个值
    • arrMerge - 合并多个对象
    • arrRemove - 删除数组中的某个元素
    • arrUnique - 数组去重
    • foreachTree - 遍历树形数据
  • 日期操作
    • convertDate - 几天后的日期
    • formatDate - 日期格式化
    • formatJust - 返回相对时间或指定格式的时间字符串
    • isToday - 验证一个日期是不是今天
    • isYesterday - 验证一个日期是不是昨天
    • toTimestamp - 时间转时间戳
  • 存储操作
    • cache - 设置、获取、删除、清空缓存localStorage
    • cookie - 设置、获取、删除 cookie
    • session - 设置、获取、删除、清空 sessionStorage
  • 工具函数
    • base64ToFile - 将 base64 字符串转换为 File 对象
    • copyText - 复制文本到剪贴板
    • createDownload - 创建下载链接
    • debounce - 防抖函数
    • deepClone - 深拷贝
    • encryptRsa - 使用 RSA 公钥加密数据
    • fileToBase64 - 将图片文件转换为 base64 字符串
    • fileUrlToBase64 - 将图片链接转换为 base64 字符串
    • getEleId - 生成一个唯一的id
    • getUrlParams - 获取url参数
    • getUuid - 生成uuid
    • subText - 截取字符串
    • throttle - 节流函数
    • zipImage - 压缩图片
  • 验证函数
    • isIPv4 - 验证是否为IPv4地址
    • isNoWord - 判断是否为非英文字符串
    • isPostcode - 判断是否为邮政编码
    • isLowercase - 判断是否为小写字母字符串
    • isCapital - 判断是否为大写字母字符串
    • isEnglish - 判断是否为英文字符串
    • isNumber - 判断是否为数字字符串
    • isDate - 判断是否为日期字符串
    • isCarNumber - 判断是否为车牌号字符串
    • isChineseName - 判断是否为中文姓名字符串
    • isAccountNumber - 判断是否为账号字符串
    • isIDCard - 判断是否为身份证号字符串
    • isPassword - 判断是否为密码字符串
    • isEmail - 判断是否为邮箱字符串
    • isPhone - 判断是否为手机号字符串

使用文档请在 https://www.npmjs.com/package/lby-utils 查看。

数组操作

/**
 * @param {any[]} arr1
 * @param {any[]} arr2
 * @returns {boolean}
 * @description 比较两个数组是否相等
 * @example arrEqual([1, 'hello', {a: 1}, [2, 3]], [1, 'hello', {a: 1}, [2, 3]]) => true
 */
export function arrEqual(arr1: any[], arr2: any[]): boolean {
  // implementation
}
/**
 * @param arr - 数组
 * @param value - 值
 * @returns {boolean}
 * @description 判断数组中是否包含某个值
 * @example  arrIncludes([1, 2, 3, 4, 5], 3) => true
 */
export function arrIncludes<T>(arr: T[], value: T): boolean {
  // implementation
}
/**
 * @returns {Object}
 * @description 合并多个对象
 * @example  arrMerge({a: 1}, {b: 2}) => {a: 1, b: 2}
 */
export function arrMerge() {
  // implementation
}
/**
 * @param {Array} arr
 * @param {any} ele
 * @returns {Array}
 * @description 删除数组中的某个元素
 * @example  arrRemove([1, 2, 3, 4, 5], 3) => [1, 2, 4, 5]
 */
export function arrRemove(arr: any[], ele: any): any[] {
  // implementation
}
/**
 * @param {Array} arr
 * @returns {Array}
 * @description 数组去重
 * @example  arrUnique([1, 2, 2, 3, 4, 4, 5]) => [1, 2, 3, 4, 5]
 */
export function arrUnique(arr: any[]): any[] {
  // implementation
}
/**
 * @param data 数据源
 * @param childrenName 子节点字段名
 * @param callback 回调函数
 * @description 遍历树形数据
 * @example foreachTree(data, 'children', (item) => { console.log(item) })
 */
export function foreachTree(data: any[], childrenName = 'children', callback: (item: any) => void) {
  // implementation
}

日期操作

/**
 * @param day: number
 * @param date: string
 * @returns string
 * @description: 几天后的日期
 * @example: convertDate(1, '2021-01-01') => '2021-01-02'
 */
export function convertDate(day: number, date: string): string {
  // implementation
}
/**
 * @param date - Date object or date string
 * @param format - Date format string (default: 'YYYY-MM-DD HH:mm:ss')
 * @returns - Formatted date string
 * @description - 日期格式化
 * @example - formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss') => '2021-09-01 12:00:00' or formatDate('2021-09-01', 'YYYY-MM-DD') => '2021-09-01'
 */
export function formatDate(date: Date | string, format: string = 'YYYY-MM-DD HH:mm:ss'): string {
  // implementation
}
/**
 * @param time 时间戳或时间字符串
 * @param option 格式化选项
 * @returns 格式化后的时间字符串
 * @description 根据时间戳或时间字符串,返回相对时间或指定格式的时间字符串
 * @example formatJust(1620000000000) => '1天前'
 */
export function formatJust(time: number | string, option?: string): string {
  // implementation
}
/**
 * @param val
 * @returns
 * @description 验证一个日期是不是今天
 * @example isToday(new Date()) => true
 */
export function isToday(val: string | number | Date): boolean {
  // implementation
}
/**
 * @param {string | number | Date} val
 * @returns {boolean}
 * @description 验证一个日期是不是昨天
 * @example isYesterday(new Date()) => true
 */
export function isYesterday(val: string | number | Date): boolean {
  // implementation
}
/**
 * @param {string} time
 * @returns {number}
 * @description 时间转时间戳
 * @example toTimestamp('2021-01-01 00:00:00') => 1609430400000
 */
export function toTimestamp(time: string): number {
  // implementation
}

存储操作

/**
 * @param key 
 * @param value 
 * @description 设置缓存
 * @example setCache('name', '张三')
 */
export function setCache(key: string, value: any) {
  // implementation
}

/**
 * @param key 
 * @description 获取缓存
 * @example getCache('name')
 */
export function getCache(key: string) {
  // implementation
}

/**
 * @param key 
 * @description 删除缓存
 * @example removeCache('name')
 */
export function removeCache(key: string) {
  // implementation
}

/**
 * @description 清空缓存
 * @example clearCache()
 */
export function clearCache() {
  // implementation
}
/**
 * @param name 
 * @param value 
 * @param days 
 * @description 设置 cookie
 * @example setCookie('name', '张三', 7)
 */
export function setCookie(name: string, value: string, days: number) {
  // implementation
}

/**
 * @param name 
 * @description 获取 cookie
 * @example getCookie('name')
 */
export function getCookie(name: string) {
  // implementation
}

/**
 * @param name 
 * @description 删除 cookie
 * @example removeCookie('name')
 */
export function removeCookie(name: string) {
  // implementation
}
/**
 * @param key 键
 * @param value 值
 * @description 设置 sessionStorage
 * @example setSession('name', '张三')
 */
export function setSession(key: string, value: any) {
  // implementation
}

/**
 * @param key 键
 * @description  获取 sessionStorage
 * @example getSession('name')
 */
export function getSession(key: string) {
  // implementation
}

/**
 * @param key 键
 * @description 删除 sessionStorage
 * @example removeSession('name')
 */
export function removeSession(key: string) {
  // implementation
}

/**
 * @description 清空 sessionStorage
 * @example clearSession()
 */
export function clearSession() {
  // implementation
}

工具函数

/**
 * @param dataUrl base64字符串
 * @param filename 文件名[默认为'file']
 * @returns File
 * @description 将 base64 字符串转换为 File 对象
 * @example base64ToFile('data:image/png;base64,').then(file => console.log(file))
 */
export function base64ToFile(dataUrl: string, filename: string = 'file'): File {
  // implementation
}
/**
 * @param text 要复制的文本
 * @description 复制文本到剪贴板
 * @example copyText('Hello, world!')
 */
export function copyText(text: string): void {
  // implementation
}
/**
 * @param {string} url 下载链接
 * @param {string} filename 下载文件名
 * @return {void}
 * @description 创建下载链接
 * @example createDownloadLink('https://www.baidu.com')
 */
export const createDownloadLink = (url: string, filename?: string) => {
  // implementation
}
/**
 * @param {Function} func 需要防抖的函数
 * @param {number} wait 防抖时间
 * @return {Function}
 * @description 防抖函数
 * @example debounce(() => console.log('hello'), 1000)
 */
export function debounce<T extends (...args: any[]) => void>(func: T, wait: number): T {
  // implementation
}
/**
 * @param value 需要克隆的值
 * @param weakMap 弱引用对象
 * @return {T} 返回克隆后的值
 * @description 深拷贝
 * @example deepClone({ a: 1, b: { c: 2 } })
 */
export function deepClone<T>(value: T, weakMap = new WeakMap()): T {
  // implementation
}
/**
 * 使用 RSA 公钥加密数据
 * @param data - 需要加密的字符串
 * @param publicKey - RSA 公钥
 * @returns 加密后的字符串,若加密失败则返回 null
 */
export function encryptWithRSA(data: string, publicKey: string): string | null {
  // implementation
}

/**
 * 使用 RSA 私钥解密数据
 * @param encryptedData - 加密后的字符串
 * @param privateKey - RSA 私钥
 * @returns 解密后的字符串,若解密失败则返回 null
 */
export function decryptWithRSA(encryptedData: string, privateKey: string): string | null {
  // implementation
}
/**
 * @param file - File object
 * @param callback - 回调函数,返回 base64 字符串
 * @returns void
 * @description 将图片文件转换为 base64 字符串
 * @example fileToBase64(file, (base64) => { console.log(base64) })
 */
export function fileToBase64(file: File, callback: (base64: string) => void): void {
  // implementation
}
/**
 * @param url - 图片链接
 * @returns/**
 * @description 将图片链接转换为 base64 字符串
 * @example fileUrlToBase64('https://example.com/image.jpg').then(base64 => console.log(base64))
 */
export async function fileUrlToBase64(url: string): Promise<string> {
  // implementation
}

getEleId

/**
 * @param {string} prefix - id前缀
 * @returns {string} - 返回一个唯一的id
 * @description 生成一个唯一的id
 * @example getEleId('id-') => 'id-1f1-1f1'
 */
export function getEleId(prefix: string = 'id-'): string {
  // implementation
}
/**
 * @param name
 * @description 获取url参数
 * @example getUrlParams('id')
 */
export function getUrlParams(name: string): string | null {
  // implementation
}
/**
 * @param {number} len
 * @param {number} radix
 * @returns {string}
 * @description 生成uuid
 * @example getUuid(8, 16) => "098b3e2c"
 */
export function getUuid(len: number = 32, radix: number = 16): string {
  // implementation
}
/**
 * @param str: string
 * @param length: number
 * @returns string
 * @description: 截取字符串
 * @example: subText('hello world', 5) => 'hello...'
 */
export function subText(str: string, length: number): string {
  // implementation
}
/**
 * @param {type} FN_PARAMS 
 * @return {type}
 * @description 节流函数
 * @example throttle(() => console.log('hello'), 1000)
 */
export function throttle<T extends (...args: any[]) => void>(func: T, wait: number): T {
  // implementation
}
/**
 * @param file 图片文件
 * @param maxSizeMB 最大大小(MB)
 * @param callback 回调函数,参数为压缩后的图片数据 URL
 * @description 压缩图片
 * @example zipImage(file, 1, (zipDataUrl) => { console.log(zipDataUrl) })
 */
export function zipImage(file: File, maxSizeMB: number, callback: (zipDataUrl: string) => void): void {
  // implementation
}

验证函数

/**
 * @param value 
 * @returns 
 * @description 验证是否为IPv4地址
 * @example isIPv4('192.168.1.1') => true
 */
export function isIPv4(value: string): boolean {
  // implementation
}
/**
 * @param value - 待验证的非英文字符串
 * @returns 
 * @description 判断是否为非英文字符串
 * @example isNoWord('123') => true
 */
export function isNoWord(value: string): boolean {
  // implementation
}
/**
 * @param value - 待验证的邮政编码字符串
 * @returns 
 * @description 判断是否为邮政编码
 * @example isPostcode('123456') => true
 */
export function isPostcode(value: string): boolean {
  // implementation
}
/**
 * @param value - 待验证的小写字母字符串
 * @returns 
 * @description 判断是否为小写字母字符串
 * @example isLowercase('abc') => true
 */
export function isLowercase(value: string): boolean {
  // implementation
}
/**
 * @param value - 待验证的大写字母字符串
 * @returns 
 * @description 判断是否为大写字母字符串
 * @example isCapital('ABC') => true
 */
export function isCapital(value: string): boolean {
  // implementation
}
/**
 * @param value - 待验证的英文字符串
 * @returns 
 * @description 判断是否为英文字符串
 * @example isEnglish('Hello') => true
 */
export function isEnglish(value: string): boolean {
  // implementation
}
/**
 * @param value - 待验证的数字字符串
 * @returns 
 * @description 判断是否为数字字符串
 * @example isNumber('123') => true
 */
export function isNumber(value: string): boolean {
  // implementation
}
/**
 * @param value - 待验证的日期字符串
 * @returns 
 * @description 判断是否为日期字符串
 * @example isDate('2021-01-01') => true
 */
export function isDate(value: string): boolean {
  // implementation
}
/**
 * @param value - 待验证的车牌号字符串
 * @returns 
 * @description 判断是否为车牌号字符串
 * @example isCarNumber('粤B12345') => true
 */
export function isCarNumber(value: string): boolean {
  // implementation
}
/**
 * @param value - 待验证的中文姓名字符串
 * @returns 
 * @description 判断是否为中文姓名字符串
 * @example isChineseName('张三') => true
 */
export function isChineseName(value: string): boolean {
  // implementation
}
/**
 * @param value - 待验证的账号字符串
 * @returns 
 * @description 判断是否为账号字符串
 * @example isAccountNumber('user123') => true
 */
export function isAccountNumber(value: string): boolean {
  // implementation
}
/**
 * @param value - 待验证的身份证号字符串
 * @returns 
 * @description 判断是否为身份证号字符串
 * @example isIDCard('123456789012345678') => true
 */
export function isIDCard(value: string): boolean {
  // implementation
}
/**
 * @param value - 待验证的密码字符串
 * @returns 
 * @description 判断是否为密码字符串
 * @example isPassword('P@ssw0rd') => true
 */
export function isPassword(value: string): boolean {
  // implementation
}
/**
 * @param value - 待验证的邮箱字符串
 * @returns 
 * @description 判断是否为邮箱字符串
 * @example isEmail('example@example.com') => true
 */
export function isEmail(value: string): boolean {
  // implementation
}
/**
 * @param value - 待验证的手机号字符串
 * @returns 
 * @description 判断是否为手机号字符串
 * @example isPhone('13800138000') => true
 */
export function isPhone(value: string): boolean {
  // implementation
}
1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago