# common
常用类
# base
# promisify
在线运行API
柯里化一个 Promise 函数
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 func function否 - 需要处理的函数 - 返回值:
(...args: any[]) => Promise<unknown> - 示例:
点击查看代码示例
import { promisify } from "@goodluck/util"; const delay = promisify((d, cb) => setTimeout(cb, d)); delay(2000).then(() => console.log('Hi!')); // -> Promise resolves after 2s
# sleep
延时等待,异步形式的 setTimeout
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 duration number否 - 等待时长 - 返回值:
Promise<void> - 示例:
点击查看代码示例
import { sleep } from "@goodluck/util"; await sleep(1000); console.log('Hi!');
# constructorName
获取类型构造函数名称
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 val any否 - 类型值 lowerCase boolean是 false返回的构造函数名称是否全部转为小写字母 - 返回值:
string - 示例:
点击查看代码示例
import { constructorName } from "@goodluck/util"; constructorName(1); // -> Number constructorName(NaN, true); // -> number class Test { get [Symbol.toStringTag]() { return "Test"; } } constructorName(new Test()); // -> Test
# bytesFormat
字节单位格式化
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 byte number否 - 字节数 fractional number是 2保留小数位数 - 返回值:
string - 示例:
点击查看代码示例
import { bytesFormat } from "@goodluck/util"; bytesFormat(64541); // -> 63.03KB
# numeralsArabicToRoman
阿拉伯数字转罗马数字
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 num number否 - 需要转换的正整数 simplify boolean是 false是否简化转换结果 - 返回值:
string - 示例:
点击查看代码示例
import { numeralsArabicToRoman } from "@goodluck/util"; numeralsArabicToRoman(5); // -> V numeralsArabicToRoman(5621); // -> DCXXI
# numeralsArabicToChinese
阿拉伯数字转中文数字
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 num number否 - 需要转换的正整数 - 返回值:
string - 示例:
点击查看代码示例
import { numeralsArabicToChinese } from "@goodluck/util"; numeralsArabicToChinese(5); // -> 五 numeralsArabicToChinese(5621); // -> 五千六百二十一
# Pagination
分页管理器
- 类型:
class - 参数:
参数名 类型 是否可选 默认值 说明 options PaginationOptions是 - 初始化参数 # PaginationOptions
参数名 类型 是否可选 默认值 说明 size number是 20每页条数 index number是 1页码 min number是 1最小页码(首页页码) max number是 Infinity最大页码(尾页页码) isLast boolean是 false是否已经尾页 - 公有方法、属性:
# Pagination.size
获取每页条数- 类型:
number - 示例:
点击查看代码示例
import { Pagination } from "@goodluck/util"; const pagination = new Pagination(); pagination.size; // -> 20
# Pagination.index
获取当前页码- 类型:
number - 示例:
点击查看代码示例
import { Pagination } from "@goodluck/util"; const pagination = new Pagination(); pagination.index; // -> 1
# Pagination.isFirst
获取当前是否是首页- 类型:
boolean - 示例:
点击查看代码示例
import { Pagination } from "@goodluck/util"; const pagination = new Pagination(); pagination.isFirst; // -> true
# Pagination.isLast
获取当前是否是尾页- 类型:
boolean - 示例:
点击查看代码示例
import { Pagination } from "@goodluck/util"; const pagination = new Pagination(); pagination.isLast; // -> false
# Pagination.prev
跳转至上一页- 类型:
function - 返回值:
void - 示例:
点击查看代码示例
import { Pagination } from "@goodluck/util"; const pagination = new Pagination(); pagination.prev();
# Pagination.next
跳转至下一页- 类型:
function - 返回值:
void - 示例:
点击查看代码示例
import { Pagination } from "@goodluck/util"; const pagination = new Pagination(); pagination.next();
# Pagination.toPage
跳转至某一页- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 index number否 - 页码 - 返回值:
void - 示例:
点击查看代码示例
import { Pagination } from "@goodluck/util"; const pagination = new Pagination(); pagination.toPage(10);
# Pagination.toFirst
跳转至首页- 类型:
function - 返回值:
void - 示例:
点击查看代码示例
import { Pagination } from "@goodluck/util"; const pagination = new Pagination(); pagination.toFirst();
# Pagination.toLast
跳转至尾页- 类型:
function - 返回值:
void - 示例:
点击查看代码示例
import { Pagination } from "@goodluck/util"; const pagination = new Pagination(); pagination.toLast();
# Pagination.setLast
手动设置是否已经尾页- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 status boolean否 true是否尾页 - 返回值:
void - 示例:
点击查看代码示例
import { Pagination } from "@goodluck/util"; const pagination = new Pagination(); pagination.setLast(false);
# Pagination.recover
所有数值还原到初始状态- 类型:
function - 返回值:
void - 示例:
点击查看代码示例
import { Pagination } from "@goodluck/util"; const pagination = new Pagination(); pagination.recover();
- 类型:
# debounce
节流函数
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 handler Function否 - 回调函数 time number否 - 频率间隔时间(毫秒) - 返回值:
string - 示例:
点击查看代码示例
import { debounce } from "@goodluck/util"; window.onscroll = debounce(() => { // ... }, 100);
# throttle
节流函数
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 handler Function否 - 回调函数 time number否 - 频率间隔时间(毫秒) - 返回值:
string - 示例:
点击查看代码示例
import { throttle } from "@goodluck/util"; window.onscroll = throttle(() => { // ... }, 100);
# array
# arrayDifference
数组比较
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 a array否 - 数组A b array否 - 数组B - 返回值:
array - 示例:
点击查看代码示例
import { arrayDifference } from "@goodluck/util"; arrayDifference([1, 2, 3], [1, 2]); // -> [3]
# arrayIntersection
数组交集
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 a array否 - 数组A b array否 - 数组B - 返回值:
array - 示例:
点击查看代码示例
import { arrayIntersection } from "@goodluck/util"; arrayIntersection([1, 2, 3], [4, 3, 2]); // -> [2, 3]
# arrayUnion
数组合集
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 ...arrs any否 - 数组 - 返回值:
array - 示例:
点击查看代码示例
import { arrayUnion } from "@goodluck/util"; arrayUnion([1, 2, 3], [4, 3, 2]); // -> [1, 2, 3, 4]
# arrayPowerset
数组幂集,获取数组所有可能的排列组合
提示
如需获取字符串的排列组合可以使用 stringAnagrams
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 数组 - 返回值:
array - 示例:
点击查看代码示例
import { arrayPowerset } from "@goodluck/util"; arrayPowerset([1, 2]); // -> [[], [1], [2], [2, 1]] arrayPowerset(["a", "b"]); // -> [[], ["a"], ["b"], ["b", "a"]]
# arrayWithout
数组排除
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 数组 ...args string、number否 - 需要排除的值 - 返回值:
array - 示例:
点击查看代码示例
import { arrayWithout } from "@goodluck/util"; arrayWithout([2, 1, 2, 3], 1, 2); // -> [3] arrayWithout(["a", "b", "c", "c", "d", "f", "f"], "a", "c"); // -> ["b", "d", "f", "f"]
# arrayUnique
数组去重
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 数组 - 返回值:
array - 示例:
点击查看代码示例
import { arrayUnique } from "@goodluck/util"; arrayUnique([1, 2, 2, 3, 4, 4, 5]); // -> [1, 2, 3, 4, 5] arrayUnique([1, "b", "b", 3, "d", "d", 5]); // -> [1, "b", 3, "d", 5]
# arrayEquals
数组是否相等,只判断一级数组,且不可为引用类型
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 a array否 - 数组A b array否 - 数组B - 返回值:
boolean - 示例:
点击查看代码示例
import { arrayEquals } from "@goodluck/util"; arrayEquals([2, 3, 6], [2, 3, 6]); // -> true arrayEquals(["a", "b", "c"], ["a", "d", "c"]); // -> false
# arraySample
数组取样,从数组中随机获取一个元素
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 数组 - 返回值:
any - 示例:
点击查看代码示例
import { arraySample } from "@goodluck/util"; arraySample([3, 7, 9, 11]); // -> 9
# arrayZip
数组分组
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 ...args array否 - 需要分组的数组 - 返回值:
array - 示例:
点击查看代码示例
import { arrayZip } from "@goodluck/util"; arrayZip(['a', 'b'], [1, 2], [true, false]); // -> [['a', 1, true], ['b', 2, false]]
# arrayChunk
数组分块
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 需要分块的数组 size number否 - 分块数量 - 返回值:
array - 示例:
点击查看代码示例
import { arrayChunk } from "@goodluck/util"; arrayChunk([1, 2, 3, 4, 5], 2); // -> [[1, 2], [3, 4], [5]] arrayChunk([1, "b", 3, "d", 5], 2); // -> [[1, "b"], [3, "d"], [5]]
# arrayCompact
过滤数组中的所有假值元素
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 需要过滤的数组 - 返回值:
array - 示例:
点击查看代码示例
import { arrayCompact } from "@goodluck/util"; arrayCompact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]); // -> [1, 2, 3, 'a', 's', 34]
# arrayCountOccurrences
计算数组中某个值的出现次数,只可计算一维数组,并且值不可为引用类型
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 需要计算的数组 value any否 - 查找的值 - 返回值:
number - 示例:
点击查看代码示例
import { arrayCountOccurrences } from "@goodluck/util"; arrayCountOccurrences([1,1,2,1,2,3], 1); // -> 3
# arrayFill
数组填充
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 需要填充的数组 value any否 - 填充的值 start number是 0填充起始位置 end number是 arr.length填充结束位置 - 返回值:
array - 示例:
点击查看代码示例
import { arrayFill } from "@goodluck/util"; arrayFill([1,2,3,4], '8', 1, 3); // -> [1, '8', '8', 4]
# arraySupplement
数组补全
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 需要补全的数组 length number否 - 数组需要补全的长度 value any否 - 补全填充值 - 返回值:
array - 示例:
点击查看代码示例
import { arraySupplement } from "@goodluck/util"; arraySupplement([6, 5, 3, 6, 7], 10, 0); // -> [6, 5, 3, 6, 7, 0, 0, 0, 0, 0] arraySupplement(["a", "b", "c"], 4, "d"); // -> ["a", "b", "c", "d"]
# arrayFilterNonUnique
过滤数组中的非唯一值
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 需要过滤的数组 - 返回值:
array - 示例:
点击查看代码示例
import { arrayFilterNonUnique } from "@goodluck/util"; arrayFilterNonUnique([1, 2, 2, 3, 4, 4, 5]); // -> [1, 3, 5] arrayFilterNonUnique(["a", "b", "b", "c", "d", "d", "e"]); // -> ["a", "c", "e"]
# arrayLast
获取数组最后一个元素
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 数组 - 返回值:
any - 示例:
点击查看代码示例
import { arrayLast } from "@goodluck/util"; arrayLast([1, 2, 3]); // -> 3
# arrayInitial
排除数组最后一个元素
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 数组 - 返回值:
array - 示例:
点击查看代码示例
import { arrayInitial } from "@goodluck/util"; arrayInitial([1, 2, 3]); // -> [1, 2]
# arrayTail
排除数组第一个元素
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 数组 - 返回值:
array - 示例:
点击查看代码示例
import { arrayTail } from "@goodluck/util"; arrayTail([1, 2, 3]); // -> [2, 3]
# arrayInitRange
初始化特定范围的数组
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 end number否 - 填充结束数值(不包含) start number是 0填充开始数值 - 返回值:
array - 示例:
点击查看代码示例
import { arrayInitRange } from "@goodluck/util"; arrayInitRange(5); // -> [0, 1, 2, 3, 4] arrayInitRange(10, 5); // -> [5, 6, 7, 8, 9]
# arrayInit
初始化特定范围和值的数组
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 length number否 - 数组长度 value any否 - 填充值 - 返回值:
array - 示例:
点击查看代码示例
import { arrayInit } from "@goodluck/util"; arrayInit(3, "a"); // -> ["a", "a", "a"] arrayInit(3, 6); // -> [6, 6, 6] arrayInit(3, (val, index, arr) => { if(index === 1) { return "a"; } return index; }); // -> [0, "a", 2]
# arrayShuffle
数组随机排列
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 数组 - 返回值:
array - 示例:
点击查看代码示例
import { arrayShuffle } from "@goodluck/util"; arrayShuffle([1, 2, 3]);
# arrayTake
从指定位置开始获取指定长度的数组,正序(从左到右)
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 需要获取的原数组 length number是 1获取的长度 - 返回值:
array - 示例:
点击查看代码示例
import { arrayTake } from "@goodluck/util"; arrayTake([1, 2, 3], 2); // -> [1, 2]
# arrayTakeRight
从指定位置开始获取指定长度的数组,倒序(从右到左)
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 需要获取的原数组 length number是 1获取的长度 - 返回值:
array - 示例:
点击查看代码示例
import { arrayTakeRight } from "@goodluck/util"; arrayTakeRight([1, 2, 3], 2); // -> [2, 3]
# arrayDeepClone
数组深拷贝,只可拷贝基础数据类型,数组不可含有引用类型
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 数组 - 返回值:
array - 示例:
点击查看代码示例
import { arrayDeepClone } from "@goodluck/util"; arrayDeepClone([1, 2, 3]);
# ArrayIteratorLoop
迭代循环器
类型:
class参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 需要迭代循环的数组 公有方法、属性:
# ArrayIteratorLoop.next
下一条,迭代一次
- 类型:
function - 返回值:
any - 示例:
点击查看代码示例
import { ArrayIteratorLoop } from "@goodluck/util"; const iteratorLoop = new ArrayIteratorLoop([1, 3, 5, 7]); iteratorLoop.next(); // -> 3 iteratorLoop.next(); // -> 5 iteratorLoop.next(); // -> 7 iteratorLoop.next(); // -> 1 iteratorLoop.next(); // -> 3
- 类型:
# color
# colorHexToRgb
16进制色值转256色值
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 hex string、number否 - 16进制色值,格式:#f0f、#34fe5b、0x33ee33 format string、boolean是 false是否格式化返回值,不格式化则返回256色值rgb数组 参数
format可传入模板字符串,模板字符串可包含占位符$R、$G、$B,分别代表返回值红绿蓝的位置(传入true时默认模板字符串为"$R,$G,$B"") - 返回值:
string、array - 示例:
点击查看代码示例
import { colorHexToRgb } from "@goodluck/util"; colorHexToRgb("#ff56fe", "rgb($R,$G,$B)"); // -> rgb(255,86,254) colorHexToRgb(0xff00ff); // -> [255, 0, 255]
# colorRgbToHex
256色值转16进制色值
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 red number否 - 红 green number否 - 绿 blue number否 - 蓝 prefix boolean是 false是否带#号前缀 如需返回值是整数,可通过 parseInt (opens new window) 转换。
- 返回值:
string - 示例:
点击查看代码示例
import { colorRgbToHex } from "@goodluck/util"; colorRgbToHex(255, 0, 255); // -> "ff00ff" colorRgbToHex(255, 0, 255, true); // -> "#ff00ff" parseInt(colorRgbToHex(255, 0, 255), 16); // -> 16711935
# colorGradient
颜色渐变
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 colorStart array否 - 起始256色值rgb数组 colorEnd array否 - 结束256色值rgb数组 amount number否 - 渐变程度,取值0-1之间 outText boolean是 false输出格式是否转为字符串 outText输出格式:true- 转为字符串,false- 256色值rgb数组 - 返回值:
string、array - 示例:
点击查看代码示例
import { colorGradient } from "@goodluck/util"; colorGradient([255, 255, 255], [100, 100, 100], 0.68); // -> [150, 150, 150] colorGradient([255, 255, 255], [100, 100, 100], 0.68, true); // -> "150,150,150"
# colorLighten
颜色减淡
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 color array否 - 256色值rgb数组 amount number否 - 减淡程度,取值0-1之间 outHex boolean是 false是否输出16进制色值,否则输出256色值rgb数组 - 返回值:
string、array - 示例:
点击查看代码示例
import { colorLighten } from "@goodluck/util"; colorLighten([25, 190, 107], .5); // -> [140, 222, 181] colorLighten([25, 190, 107], .5, true); // -> "#8cdeb5"
# colorDarken
颜色加深
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 color array否 - 256色值rgb数组 amount number否 - 加深程度,取值0-1之间 outHex boolean是 false是否输出16进制色值,否则输出256色值rgb数组 - 返回值:
string、array - 示例:
点击查看代码示例
import { colorDarken } from "@goodluck/util"; colorDarken([25, 190, 107], .5); // -> [12, 95, 53] colorDarken([25, 190, 107], .5, true); // -> "#0c5f35"
# colorRandom
随机色值
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 prefix boolean是 - 是否添加#号前缀 - 返回值:
string - 示例:
点击查看代码示例
import { colorRandom } from "@goodluck/util"; colorRandom(); // -> "a4dcf8" colorRandom(true); // -> "#0c5f35"
# date-time
# dateParse
日期类型解析
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 date Date、string、number是 - 需要解析的日期时间戳或字符串 - 返回值:
Date - 示例:
点击查看代码示例
import { dateParse } from "@goodluck/util"; dateParse("2022-01-25 11:24:05"); // -> Tue Jan 25 2022 11:24:05 GMT+0800 (中国标准时间)
# dateDeepClone
日期时间类型深拷贝
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 date Date否 - 需要深拷贝的日期时间 - 返回值:
Date - 示例:
点击查看代码示例
import { dateDeepClone } from "@goodluck/util"; dateDeepClone(new Date());
# dateFormat
日期时间格式化
类型:
function参数:
参数名 类型 是否可选 默认值 说明 date Date否 - 需要格式化的日期时间 formatStr string是 "hh:mm:ss"格式化规则 格式化规则:y-年、M-月、d-日、h-时、m-分、s-秒、S-毫秒、W-星期、q-季度、E-季节
占位符限制:
1-4位:y-年
1-2位:M-月、d-日、h-时、m-分、s-秒、q-季度
1位:S-毫秒、W-星期、E-季节若需要对字符串的日期时间格式化,只需用
new Date()转换为Date类型后调用。返回值:
string示例:
点击查看代码示例
import { dateFormat } from "@goodluck/util"; dateFormat(new Date(1466381344423), "yyyy-MM-dd hh:mm:ss.S 周W E"); // -> "2016-06-20 08:09:04.423 周一 夏" dateFormat(new Date(1466381344423), "yyyy-M-d h:m:s.S"); // -> "2016-6-20 8:9:4.423"
# timestampToCountdown
时间戳转倒计时格式化,毫秒数转格式化倒计时
类型:
function参数:
参数名 类型 是否可选 默认值 说明 timestamp string、number否 - 时间戳(毫秒数) formatStr string是 "hh:mm:ss"格式化规则 formatAdditional string是 formatStr格式化规则补充 格式化规则:d-日、h-时、m-分、s-秒、S-毫秒
占位符限制:
1-2位:d-日、h-时、m-分、s-秒
1位:S-毫秒formatAdditional默认与formatStr是相同的,只是当formatStr有需要格式化d-日时,若d=0的情况,会返回使用formatAdditional格式化后的数据。返回值:
string示例:
点击查看代码示例
import { timestampToCountdown } from "@goodluck/util"; timestampToCountdown(1568412); // -> "00:26:08" timestampToCountdown(15684126, "d天 hh:mm:ss", "hh:mm:ss"); // -> "04:21:24" timestampToCountdown(156841267, "d天h小时m分钟s秒", "hh:mm:ss"); // -> "1天19小时34分钟1秒"
# dateDiffBetween
获取两个日期之间相差的毫秒数
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 dateInitial Date否 - 开始时间 dateFinal Date否 - 结束时间 - 返回值:
number - 示例:
点击查看代码示例
import { dateDiffBetween } from "@goodluck/util"; dateDiffBetween(new Date("2020-05-20"), new Date("2020-05-25")); // -> 432000000 dateDiffBetween(new Date("2020-05-25"), new Date("2020-05-20")); // -> -432000000
# daysDiffBetweenDates
获取两个日期之间相差的天数
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 dateInitial Date否 - 开始时间 dateFinal Date否 - 结束时间 decimals number是 2精确小数位 - 返回值:
number - 示例:
点击查看代码示例
import { daysDiffBetweenDates } from "@goodluck/util"; daysDiffBetweenDates(new Date("2020-05-20"), new Date("2020-05-25")); // -> 5 daysDiffBetweenDates(new Date("2020-05-25"), new Date("2020-05-20")); // -> -5 daysDiffBetweenDates(new Date("2020-05-20"), new Date("2020-05-25 20:36:18")); // -> 5.53
# dateRange
判断一个日期时间是否在指定范围内
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 date Date否 - 需要判断的时间 range DateRangeOptions否 - 范围参数 # DateRangeOptions
参数名 类型 是否可选 默认值 说明 start Date是 undefiend开始时间 end Date是 undefiend结束时间 start、end可以只传入其中之一,那么只会判断传入的条件。 - 返回值:
boolean - 示例:
点击查看代码示例
import { dateRange } from "@goodluck/util"; dateRange(new Date("2020-09-16"), { start: new Date("2020-09-10") }); // -> true dateRange(new Date("2020-09-16"), { end: new Date("2020-09-20") }); // -> true dateRange(new Date("2020-09-16"), { start: new Date("2020-09-10"), end: new Date("2020-09-20") }); // -> true dateRange(new Date("2020-08-10"), { start: new Date("2020-09-10"), end: new Date("2020-09-20") }); // -> false
# timestampBeautify
倒计时时间戳格式美化
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 timestamp string、number否 - 时间戳(毫秒数) timestamp传入正数表示之前,传入负数表示之后 - 返回值:
string - 示例:
点击查看代码示例
import { timestampBeautify } from "@goodluck/util"; timestampBeautify(12345); // -> "12秒前" timestampBeautify(-12345); // -> "12秒后" timestampBeautify(65484126964); // -> "2年前"
# math
# mathFactorial
求阶乘
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 num number否 - 非负整数 - 返回值:
number - 示例:
点击查看代码示例
import { mathFactorial } from "@goodluck/util"; mathFactorial(5); // -> 120
# mathPercentile
计算数组中小于等于某个值的占比
若要计算大于的数量,直接数组长度减去返回值即可。
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 数字数组 val number否 - 界定值 - 返回值:
number
返回百分比0-1之间。
- 示例:
点击查看代码示例
import { mathPercentile } from "@goodluck/util"; mathPercentile([3, 600, 7, 456, 700], 500); // -> 0.6
# mathClosest
获取数组中与某数字最近的值
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 需要计算的数组 num number否 - 求相近的数字 isLess boolean是 false是否获取小于的,否则大于 - 返回值:
number - 示例:
点击查看代码示例
import { mathClosest } from "@goodluck/util"; mathClosest([12, 5, 50, 28, 39], 20); // -> 28 mathClosest([12, 5, 50, 28, 39], 20, true); // -> 12
# mathAverage
求平均数
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 数字数组 - 返回值:
number - 示例:
点击查看代码示例
import { mathAverage } from "@goodluck/util"; mathAverage([1, 2, 3]); // -> 2 mathAverage([9, 10, 20]); // -> 13
# mathMedian
获取数字数组的中间值,将数字从小到大排序后取的中值
如果数组长度是奇数,则返回排序后的中间值,否则是返回两个中间值的平均值
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 数字数组 - 返回值:
number - 示例:
点击查看代码示例
import { mathMedian } from "@goodluck/util"; mathMedian([12, 3, 28, 41, 53]); // -> 28 mathMedian([6, 17, 24, 39, 45, 70]); // -> 31.5
# mathSum
数组求和
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 数字数组 key string是 - 键名,当是对象数组时匹配 - 返回值:
number - 示例:
点击查看代码示例
import { mathSum } from "@goodluck/util"; mathSum([1, 2, 3, 4]); // -> 10 mathSum([{a:1,b:2},{a:5,b:7},{a:3,b:6}], "a"); // -> 9
# mathRound
指定数字的小数位数
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 num number否 - 数字 decimals number是 0小数位数,为空则将被四舍五入为一个整数 - 返回值:
number - 示例:
点击查看代码示例
import { mathRound } from "@goodluck/util"; mathRound(1.15456445, 2); // -> 1.15 mathRound(2.36841541, 2); // -> 2.37 mathRound(36.4548541); // -> 36
# mathStandardDeviation
计算标准偏差
标准偏差是统计学中一种度量数据分布的分散程度标准,用以衡量数据值偏离算术平均值的程度。标准偏差越小,这些值偏离平均值就越少,反之亦然。标准偏差的大小可通过标准偏差与平均值的倍率关系来衡量。
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 arr array否 - 数字数组 usePopulation boolean是 false标准偏差类型 标准偏差类型:
true- 总体标准偏差、false- 样本标准偏差 - 返回值:
number - 示例:
点击查看代码示例
import { mathStandardDeviation } from "@goodluck/util"; mathStandardDeviation([10, 2, 38, 23, 38, 23, 21]); // -> 13.284434142114991 mathStandardDeviation([10, 2, 38, 23, 38, 23, 21], true); // -> 12.29899614287479
# mathDigitize
将数字拆分转换为数组
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 num number否 - 正整数 - 返回值:
array - 示例:
点击查看代码示例
import { mathDigitize } from "@goodluck/util"; mathDigitize(2334); // -> [2, 3, 3, 4]
# mathRandomInRange
在指定数字范围内生成一个随机数
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 min number否 - 最小值 max number否 - 最大值 - 返回值:
number - 示例:
点击查看代码示例
import { mathRandomInRange } from "@goodluck/util"; mathRandomInRange(2, 10);
# mathRandomIntegerInRange
在指定数字范围内生成一个随机整数
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 min number否 - 最小值 max number否 - 最大值 - 返回值:
number - 示例:
点击查看代码示例
import { mathRandomIntegerInRange } from "@goodluck/util"; mathRandomIntegerInRange(2, 10);
# mathCeil
数字向上取整
基于位数取整,例如625取整后700,4560取整后5000。
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 num number否 - 数字 - 返回值:
number - 示例:
点击查看代码示例
import { mathCeil } from "@goodluck/util"; mathCeil(625); // -> 700 mathCeil(4560); // -> 5000
# mathPrefixZero
数字前补零
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 num number、string否 - 需要补零的数字 length number是 2补零长度 补零长度是指需要补充到的位数,也就是补零后返回值的长度,例如68补零长度2结果还是68,只有补零长度3时才返回068。
- 返回值:
string - 示例:
点击查看代码示例
import { mathPrefixZero } from "@goodluck/util"; mathPrefixZero(2); // -> 02 mathPrefixZero(2, 4); // -> 0002 mathPrefixZero("68", 3); // -> 068
# object
# objectDeepCloneSimply
对象深拷贝
对象深拷贝是一个很复杂的问题,很多种方式之间都会有取舍,很难做到真正的深拷贝,这里只保证了对基础数据类型、方法、Set、Map的深拷贝,所以这是一个对象深拷贝的简单实现。
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 target object否 - 需要深拷贝的对象 - 返回值:
object - 示例:
点击查看代码示例
import { objectDeepCloneSimply } from "@goodluck/util"; objectDeepCloneSimply({a:1,b:2}); // -> {a:1,b:2}
# objectAssign
对象合并
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 target object否 - 被合并的源对象 ...objects object否 - 需要被合并的对象 - 返回值:
object - 示例:
点击查看代码示例
import { objectAssign } from "@goodluck/util"; objectAssign({a:1,b:2}, {a:3}); // -> {a:3,b:2} objectAssign({a:1,b:{w:2,d:[1,2,3]},c:9}, {a:2,d:5,b:{w:6,z:10}}, {o:"h"}); // -> {a:2,d:5,b:{w:6,d:[1,2,3],z:10},c:9,o:"h"}
# objectAssignDeep
对象深合并
和
objectAssign的区别是输出的对象是深拷贝的。
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 target object否 - 被合并的源对象 ...objects object否 - 需要被合并的对象 - 返回值:
object - 示例:
点击查看代码示例
import { objectAssignDeep } from "@goodluck/util"; objectAssignDeep({a:1,b:2}, {a:3}); // -> {a:3,b:2} objectAssignDeep({a:1,b:{w:2,d:[1,2,3]},c:9}, {a:2,d:5,b:{w:6,z:10}}, {o:"h"}); // -> {a:2,d:5,b:{w:6,d:[1,2,3],z:10},c:9,o:"h"}
# objectPick
对象提取
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 target object否 - 需要被提取的对象 arr array否 - 需要提取的键名数组 - 返回值:
object - 示例:
点击查看代码示例
import { objectPick } from "@goodluck/util"; objectPick({a:1,b:'h',c:3 }, ['a','c']); // -> {a:1,c:3}
# string
# stringAnagrams
字符串排列组合,拆解一个字符串的所有可能的排列组合
提示
如需获取数组的排列组合可以使用 arrayPowerset
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 字符串 - 返回值:
string[] - 示例:
点击查看代码示例
import { stringAnagrams } from "@goodluck/util"; stringAnagrams("a12"); // -> ["a12", "a21", "1a2", "12a", "2a1", "21a"] stringAnagrams("我是5"); // -> ["我是5", "我5是", "是我5", "是5我", "5我是", "5是我"]
# stringCapitalizeEveryWord
字符串中每个单词的首字母转大写
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 字符串 - 返回值:
string - 示例:
点击查看代码示例
import { stringCapitalizeEveryWord } from "@goodluck/util"; stringCapitalizeEveryWord("good"); // -> "Good" stringCapitalizeEveryWord("never fade out"); // -> "Never Fade Out"
# stringCapitalize
字符串首字母大写
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 字符串 lowerRest boolean是 false是否将其余部分都转为小写 - 返回值:
string - 示例:
点击查看代码示例
import { stringCapitalize } from "@goodluck/util"; stringCapitalize("good"); // -> "Good" stringCapitalize("never fade out"); // -> "Never fade out"
# stringPalindrome
检查回文
这里回文的意思是反向阅读与正向是一样的。
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 字符串 - 返回值:
boolean - 示例:
点击查看代码示例
import { stringPalindrome } from "@goodluck/util"; stringPalindrome("taco cat"); // -> true stringPalindrome("util"); // -> false
# stringReverse
字符串反转
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 字符串 - 返回值:
string - 示例:
点击查看代码示例
import { stringReverse } from "@goodluck/util"; stringReverse("foobar"); // -> "raboof" stringReverse("一生热爱回头太难"); // -> "难太头回爱热生一"
# stringSortInCharacters
按字母顺序排列字符串
兼容性
如果是汉字则会按拼音排序,但是在 NodeJS 环境会有点问题。
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 字符串 - 返回值:
string - 示例:
点击查看代码示例
import { stringSortInCharacters } from "@goodluck/util"; stringSortInCharacters("cabbage"); // -> "aabbceg"
# stringTruncate
字符串截断
截取所需长度的字符串,剩余部分用...附加到结尾,如果length大于等于字符串长度将会直接返回原始字符串。
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 字符串 length number否 - 需要截取的长度 - 返回值:
string - 示例:
点击查看代码示例
import { stringTruncate } from "@goodluck/util"; stringTruncate("boomerang", 4); // -> "boom..." stringTruncate("时间从未走远,在那些始终和时间赛跑的人身上。", 20); // -> "时间从未走远,在那些始终和时间赛跑..."
# stringByteSize
获取字符串的字节长度
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 字符串 - 返回值:
number - 示例:
点击查看代码示例
import { stringByteSize } from "@goodluck/util"; stringByteSize("a"); // -> 1 stringByteSize("啊"); // -> 3 stringByteSize("123"); // -> 3
# stringPhoneNumberHide
隐藏处理手机号
将手机号中间一部分数字转为星号。
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 phoneNumber string否 - 手机号 - 返回值:
string - 示例:
点击查看代码示例
import { stringPhoneNumberHide } from "@goodluck/util"; stringPhoneNumberHide("18865294269"); // -> "188****4269" stringPhoneNumberHide("6591475852"); // -> "659***5852" stringPhoneNumberHide("5028332"); // -> "50***32"
# stringReplace
替换文本中所有匹配关键字
提示
建议在有 polyfill 或无兼容问题的项目中直接使用 ES2021 的 String.prototype.replaceAll (opens new window)。
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 需要匹配的字符串 text string否 - 匹配关键字 replace string否 - 替换文本 - 返回值:
string - 示例:
点击查看代码示例
import { stringReplace } from "@goodluck/util"; stringReplace("only time", "time", "you"); // -> "only you" stringReplace("但愿忘记那忧伤忘记这迷惘", "忘记", "没有"); // -> "但愿没有那忧伤没有这迷惘"
# stringTrimAll
去除字符串中所有空格
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 字符串 - 返回值:
string - 示例:
点击查看代码示例
import { stringTrimAll } from "@goodluck/util"; stringTrimAll("Young For You"); // -> "YoungForYou" stringTrimAll(" Die Young "); // -> "DieYoung"
# stringSnakeToCamel
蛇形命名法转驼峰命名法
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 字符串 symbol string是 "-"蛇形命名法的连接符号 - 返回值:
string - 示例:
点击查看代码示例
import { stringSnakeToCamel } from "@goodluck/util"; stringSnakeToCamel("string-snake-to-camel"); // -> "stringSnakeToCamel" stringSnakeToCamel("string_snake_to_camel", "_"); // -> "stringSnakeToCamel"
# stringCamelToSnake
驼峰命名法转蛇形命名法
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 字符串 symbol string是 "-"蛇形命名法的连接符号 - 返回值:
string - 示例:
点击查看代码示例
import { stringCamelToSnake } from "@goodluck/util"; stringCamelToSnake("stringSnakeToCamel"); // -> "string-snake-to-camel" stringCamelToSnake("stringSnakeToCamel", "_"); // -> "string_snake_to_camel"
# stringLength
获取字符串长度
在
ECMAScript操作解释字符值的地方,每个元素都被解释为单个UTF-16单元,所以码点超出USC-2范围的字符长度length会被识别成2,使用这个方法可以确保获取到正确的字符串长度。
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 字符串 - 返回值:
number - 示例:
点击查看代码示例
import { stringLength } from "@goodluck/util"; stringLength("我𠮷"); // -> 2 stringLength("hello word!"); // -> 11
# stringThousands
数字千位格式化(每三位数加逗号)
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 num number、string否 - 数字 - 返回值:
string - 示例:
点击查看代码示例
import { stringThousands } from "@goodluck/util"; stringThousands(7345689); // -> '7,345,689'
# stringMid
取字符串中间文本
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 字符串 first string否 - 前置文本(传入空字符串表示从字符串首位开始截取) last string否 - 后置文本(传入空字符串表示截取至字符串尾部) start number是 0 起始位置 - 返回值:
string - 示例:
点击查看代码示例
import { stringMid } from "@goodluck/util"; stringMid("0123456789", "3", "7"); // -> '456' stringMid("0123456789", "6", ""); // -> '789' stringMid("0123456789", "", "3"); // -> '012' stringMid("今日天气【阴转多云】,明日天气【多云转晴】", "【", "】", 12); // -> '多云转晴'
# stringIncludes
查询字符串包含
- 类型:
function - 参数:
参数名 类型 是否可选 默认值 说明 str string否 - 字符串 searchString string、array否 - 查询字符串 matchAll boolean是 - 全部匹配 - 返回值:
boolean - 示例:
点击查看代码示例
import { stringIncludes } from "@goodluck/util"; stringIncludes("无论未来你们在哪里,愿我们脚下有风,各自灿烂!", ["他们", "哪里"]); // -> true stringIncludes("无论未来你们在哪里,愿我们脚下有风,各自灿烂!", ["他们", "现在"]); // -> false stringIncludes("无论未来你们在哪里,愿我们脚下有风,各自灿烂!", ["他们", "哪里"], true); // -> false stringIncludes("无论未来你们在哪里,愿我们脚下有风,各自灿烂!", ["你们", "哪里"], true); // -> true