小巧实用的 JavaScript 工具类库。
https://fjc0k.github.io/vtils/
特性
- 源于日常项目实践,更实用
- 使用 TypeScript 编写,类型友好
- 支持摇树优化(Tree Shaking),只引入使用到的工具
- 浏览器、Node、小程序多端兼容
说明
vtils
自身并不包括一些已有成熟库的工具,如时间处理、网络请求等,在此做下推荐:
安装
yarn add vtils
npm i vtils --save
你也可通过 CDN 安装,然后使用全局变量 vtils
访问相关工具:
<script src="https://cdn.jsdelivr.net/npm/vtils@2.26.1-beta.2/lib/index.umd.min.js" crossorigin="anonymous"></script>
<script>
if (vtils.inBrowser()) {
alert('您在浏览器中...')
}
</script>
使用
在线体验:https://stackblitz.com/edit/vtils
import { inBrowser, shuffle } from 'vtils'
if (inBrowser()) {
alert('您在浏览器中...')
}
alert(shuffle([1, 2, 3, 4]))
Type aliases
AnyFunction
AnyFunction: function
AnyObject
AnyObject: Record<keyof any, any>
AsyncOrSync
AsyncOrSync<T>: PromiseLike<T> | T
Type parameters
AsyncReturnType
AsyncReturnType<T>: T extends (...args: any[]) => Promise<infer R> ? R : any
Type parameters
Brand
Brand<T, B>: T & object
Type parameters
Defined
Defined<T>: Exclude<T, undefined>
Type parameters
DisposerItem
DisposerItem: function
Type declaration
-
-
Returns void
|
Promise<void>
EasyValidatorData
EasyValidatorData: Record<keyof any, any>
EasyValidatorRules
Type parameters
EventBusUnsubscribe
EventBusUnsubscribe: function
GetTypeReturn
Get
TypeReturn: LiteralUnion<"Undefined" | "Null" | "Array" | "String" | "Arguments" | "Function" | "Error" | "Boolean" | "Number" | "Date" | "RegExp" | "Object" | "JSON" | "Math" | "Symbol" | "Map" | "Set" | "WeakMap" | "WeakSet", string>
If
If<Condition, Then, Else>: If<Condition, Then, Else>
Type parameters
IsNever
IsNever<T>: IsNever<T>
Type parameters
LiteralUnion
Literal
Union<L, B>: L | Brand<B, never>
Type parameters
MemoizeReturn
MemoizeReturn<T>: T & object
Type parameters
Merge
Merge<M, N>
: Omit<M, Extract<keyof M, keyof N>> & N
Type parameters
Omit
Omit<T, K>: Pick<T, Exclude<keyof T, K>>
Type parameters
OneOrMore
OneOrMore<T>: T | T[]
Type parameters
OrderByRule
OrderByRule<T>: object
Type parameters
PartialBy
Partial
By<T, K>: Omit<T, K> & Partial<Pick<T, K>>
Type parameters
ValueOf
ValueOf<T>: T[keyof T]
Type parameters
WechatErrorCallback
WechatErrorCallback: function
WechatJsApi
WechatJsApi: "checkJsApi" | "updateAppMessageShareData" | "updateTimelineShareData" | "onMenuShareTimeline" | "onMenuShareAppMessage" | "onMenuShareQQ" | "onMenuShareQZone" | "startRecord" | "stopRecord" | "onVoiceRecordEnd" | "playVoice" | "pauseVoice" | "stopVoice" | "onVoicePlayEnd" | "uploadVoice" | "downloadVoice" | "chooseImage" | "previewImage" | "uploadImage" | "downloadImage" | "translateVoice" | "getNetworkType" | "openLocation" | "getLocation" | "hideOptionMenu" | "showOptionMenu" | "hideMenuItems" | "showMenuItems" | "hideAllNonBaseMenuItem" | "showAllNonBaseMenuItem" | "closeWindow" | "scanQRCode" | "chooseWXPay" | "openProductSpecificView" | "addCard" | "chooseCard" | "openCard"
WechatNonBaseMenuItem
WechatNonBaseMenuItem: "menuItem:share:appMessage" | "menuItem:share:timeline" | "menuItem:share:qq" | "menuItem:share:weiboApp" | "menuItem:favorite" | "menuItem:share:facebook" | "menuItem:share:QZone" | "menuItem:editTag" | "menuItem:delete" | "menuItem:copyUrl" | "menuItem:originPage" | "menuItem:readMode" | "menuItem:openWithQQBrowser" | "menuItem:openWithSafari" | "menuItem:share:email" | "menuItem:share:brand"
Functions
assign
- assign<T>(target: T, ...sources: object[]): T
-
Type parameters
Parameters
-
target: T
-
Rest ...sources: object[]
Returns T
返回扩展后的目标对象
base64Decode
- base64Decode(input: string): string
-
Parameters
Returns string
返回解码后的字符串
base64Encode
- base64Encode(input: string): string
-
Parameters
Returns string
返回编码后的 base64
字符串
base64UrlDecode
- base64UrlDecode(input: string): string
-
Parameters
Returns string
返回解码后的字符串
base64UrlEncode
- base64UrlEncode(input: string): string
-
Parameters
Returns string
返回编码后的 base64url
字符串
castArray
- castArray<T>(value: T | T[]): T[]
-
Type parameters
Parameters
Returns T[]
返回转换后的数组
chunk
- chunk<T>(arr: T[], size: number, filler?: undefined | function): T[][]
-
Type parameters
Parameters
-
arr: T[]
-
size: number
-
Optional filler: undefined | function
Returns T[][]
返回拆分后的新数组
clamp
- clamp(value: number, min: number, max: number): number
-
Parameters
-
value: number
-
min: number
-
max: number
Returns number
返回结果值
combine
- combine<K, V>(keys: K[], values: V[]): object
-
Type parameters
Parameters
Returns object
返回合并出的对象
createURIQuery
- createURIQuery(parameters: AnyObject): string
-
Parameters
Returns string
返回 URI 查询字符串
debounce
- debounce<T>(fn: T, wait: number): function
-
Type parameters
Parameters
Returns function
返回去抖后的函数
-
- (...args: Parameters<T>): void
-
Parameters
-
Rest ...args: Parameters<T>
Returns void
dedent
- dedent(literals: TemplateStringsArray, ...interpolations: any[]): string
- dedent(text: string): string
-
Parameters
-
literals: TemplateStringsArray
-
Rest ...interpolations: any[]
Returns string
返回结果
-
Parameters
Returns string
返回操作后的文本
defaultTo
- defaultTo<T>(value: T, defaultValue: T, extraPredictions?: any[]): T
-
Type parameters
Parameters
-
value: T
-
defaultValue: T
-
Optional extraPredictions: any[]
Returns T
返回结果值
endsWith
- endsWith(str: string, needle: string): boolean
entries
- entries<T>(data: T): Array<[keyof T, T[keyof T]]>
-
Type parameters
Parameters
Returns Array<[keyof T, T[keyof T]]>
返回键值对数组
escapeRegExp
- escapeRegExp(str: string): string
-
Parameters
Returns string
返回转换后的字符串
expectType
- expectType<T>(value: T): void
-
Type parameters
Parameters
Returns void
fill
- fill<T>(arr: T[], returnValue: FillReturnValue<T>, start?: number, end?: number): T[]
-
Type parameters
Parameters
-
arr: T[]
-
-
Default value start: number = 0
-
Default value end: number = arr.length
Returns T[]
返回填充改变后的数组
flat
-
Type parameters
Parameters
Returns T[]
返回新数组
forOwn
-
Type parameters
Parameters
Returns void
groupBy
-
Type parameters
Parameters
Returns object
返回分组结果
has
- has<T>(obj: T, key: LiteralUnion<T extends any[] ? never : keyof T, string | number | symbol>): boolean
ii
- ii<F>(fn: F): ReturnType<F>
-
Type parameters
Parameters
Returns ReturnType<F>
返回被调用函数的返回值
inAndroid
- inAndroid(callback?: undefined | function): boolean
inBrowser
- inBrowser(callback?: undefined | function): boolean
inIOS
- inIOS(callback?: undefined | function): boolean
inNode
- inNode(callback?: undefined | function): boolean
inWechatMiniProgram
- inWechatMiniProgram(callback?: undefined | function): boolean
inWechatWebview
- inWechatWebview(callback?: undefined | function): boolean
includes
- includes<T>(arr: T[], value: T): boolean
- includes<T>(obj: T, value: ValueOf<Omit<T, symbol>>): boolean
- includes(str: string, value: string): boolean
-
Type parameters
Parameters
Returns boolean
value
在 arr
中返回 true
,否则返回 false
-
Type parameters
Parameters
Returns boolean
value
在 obj
中返回 true
,否则返回 false
-
Parameters
-
str: string
-
value: string
Returns boolean
value
在 str
中返回 true
,否则返回 false
indent
- indent(literals: TemplateStringsArray, ...interpolations: any[]): string
- indent(text: string, leadingString: string): string
- indent(text: string, callback: function): string
-
Parameters
-
literals: TemplateStringsArray
-
Rest ...interpolations: any[]
Returns string
返回结果
-
Parameters
-
text: string
-
leadingString: string
Returns string
返回结果
-
Parameters
-
text: string
-
callback: function
-
- (lineString: string, lineIndex: number): string
-
Parameters
-
lineString: string
-
lineIndex: number
Returns string
Returns string
返回结果
isArguments
- isArguments(value: any): boolean
isArray
- isArray(value: any): boolean
isBoolean
- isBoolean(value: any): boolean
isChineseIDCardNumber
- isChineseIDCardNumber(value: string): boolean
isDate
- isDate(value: any): boolean
isEmail
- isEmail(value: string): boolean
isEmpty
- isEmpty(value: any): boolean
isEqualArray
- isEqualArray(...arrs: any[][]): boolean
isFinite
- isFinite(value: any): boolean
isFunction
- isFunction(value: any): boolean
isHan
- isHan(value: string): boolean
isInteger
- isInteger(value: any): boolean
isNaN
- isNaN(value: any): boolean
isNegativeInteger
- isNegativeInteger(value: any): boolean
isNil
- isNil(value: any): boolean
isNull
- isNull(value: any): boolean
isNumber
- isNumber(value: any): boolean
isNumeric
- isNumeric(value: any): boolean
isObject
- isObject(value: any): boolean
isPlainObject
- isPlainObject(value: any): boolean
isPositiveInteger
- isPositiveInteger(value: any): boolean
isPossibleChineseMobilePhoneNumber
- isPossibleChineseMobilePhoneNumber(number: number | string): boolean
isPossibleChineseName
- isPossibleChineseName(value: string): boolean
isPromiseLike
- isPromiseLike(value: any): boolean
isRegExp
- isRegExp(value: any): boolean
isString
- isString(value: any): boolean
isUndefined
- isUndefined(value: any): boolean
isUrl
- isUrl(value: string): boolean
jestExpectEqual
- jestExpectEqual<T>(actual: T, ...expected: T[]): void
-
Type parameters
Parameters
-
actual: T
-
Rest ...expected: T[]
Returns void
keyBy
-
Type parameters
Parameters
Returns object
返回分组结果
keys
- keys<T>(obj: T): (string & object | number & object | Extract<keyof T, string | number>)[]
-
Type parameters
Parameters
Returns (string & object | number & object | Extract<keyof T, string | number>)[]
返回结果数组
last
- last<T>(arr: T[]): If<IsNever<T>, undefined, T | undefined>
-
Type parameters
Parameters
Returns If<IsNever<T>, undefined, T | undefined>
返回数组的最后一项
loop
- loop(interval: number, callback: function): (Anonymous function)
-
Parameters
-
interval: number
-
callback: function
Returns (Anonymous function)
返回停止执行的函数
mapValues
-
Type parameters
Parameters
Returns object
返回映射后的新对象
memoize
-
Type parameters
Parameters
返回缓存化后的函数
noop
- noop(...args: any[]): any
omit
- omit<T, K>(obj: T, props: K[]): object
-
Type parameters
-
-
K: Extract<keyof T, string | number>
Parameters
Returns object
返回结果对象
orderBy
-
Type parameters
Parameters
Returns T[]
返回排序后的数据
padEnd
- padEnd(str: string, length: number, chars?: string): string
-
Parameters
-
str: string
-
length: number
-
Default value chars: string = " "
Returns string
返回填充后的字符串
padStart
- padStart(str: string, length: number, chars?: string): string
-
Parameters
-
str: string
-
length: number
-
Default value chars: string = " "
Returns string
返回填充后的字符串
parallel
- parallel<T, R>(tasks: T[]): Promise<R[]>
-
Type parameters
-
-
R: T extends (...args: any) => Promise<infer X> ? X : T extends (...args: any) => infer Y ? Y : any
Parameters
Returns Promise<R[]>
返回全部任务执行结果组成的数组
parseCSSValue
- parseCSSValue(value: string | number, defaultUnit?: string): CSSValue
-
Parameters
-
value: string | number
-
Default value defaultUnit: string = "px"
返回解析结果
parseURIQuery
-
Type parameters
Parameters
Returns T
返回 URI 查询参数
partial
- partial<T>(obj: T): Partial<T>
partialBy
- partialBy<T, K>(obj: T, keys: K[]): PartialBy<T, K>
pick
- pick<T, K>(obj: T, props: K[]): object
-
Type parameters
-
-
K: Extract<keyof T, string | number>
Parameters
Returns object
返回结果对象
placeKitten
- placeKitten(size: number): string
- placeKitten(width: number, height: number): string
-
Parameters
Returns string
返回占位图片链接
-
Parameters
-
width: number
-
height: number
Returns string
返回占位图片链接
pluck
- pluck<T, V>(data: T[], getValueIteratee: PluckGetValueIteratee<T, V>): V[]
- pluck<T, V, K>(data: T[], getValueIteratee: PluckGetValueIteratee<T, V>, getKeyIteratee: PluckGetKeyIteratee<T, K>): Record<K, V>
-
Type parameters
Parameters
Returns V[]
返回结果数组
-
Type parameters
Parameters
Returns Record<K, V>
返回结果对象
randomString
-
Returns string
返回生成的随机字符串
range
- range(start: number, end: number, step?: number): number[]
-
Parameters
-
start: number
-
end: number
-
Default value step: number = 1
Returns number[]
返回开始到结束范围内数字组成的数组
remove
- remove<T>(data: T, index: number): T
-
Type parameters
Parameters
Returns T
返回传入的数组
removeByValue
- removeByValue<V>(data: V[], value: V): V[]
-
Type parameters
Parameters
Returns V[]
返回传入的数组
repeat
- repeat(str: string, n: number): string
-
Parameters
Returns string
返回结果字符串
result
- result<T>(action: T): Promise<[any, T extends PromiseLike<infer R> ? R : any]>
- result<T, R>(action: T): Promise<[any, R extends PromiseLike<any> ? R extends PromiseLike<infer RR> ? RR : any : R]>
-
Type parameters
Parameters
Returns Promise<[any, T extends PromiseLike<infer R> ? R : any]>
返回 [错误信息, 结果数据]
,错误信息为 null
表示没有错误
-
Type parameters
-
T: function
-
R: ReturnType<T>
Parameters
Returns Promise<[any, R extends PromiseLike<any> ? R extends PromiseLike<infer RR> ? RR : any : R]>
返回 [错误信息, 结果数据]
,错误信息为 null
表示没有错误
round
- round(number: number, precision?: number): number
-
Parameters
-
number: number
-
Default value precision: number = 0
Returns number
返回结果
roundDown
- roundDown(number: number, precision?: number): number
-
Parameters
-
number: number
-
Default value precision: number = 0
Returns number
返回结果
roundUp
- roundUp(number: number, precision?: number): number
-
Parameters
-
number: number
-
Default value precision: number = 0
Returns number
返回结果
safeGet
- safeGet<T, K0>(obj: T, k0: K0): T[K0]
- safeGet<T, K0, K1>(obj: T, k0: K0, k1: K1): T[K0][K1]
- safeGet<T, K0, K1, K2>(obj: T, k0: K0, k1: K1, k2: K2): T[K0][K1][K2]
- safeGet<T, K0, K1, K2, K3>(obj: T, k0: K0, k1: K1, k2: K2, k3: K3): T[K0][K1][K2][K3]
- safeGet<T, K0, K1, K2, K3, K4>(obj: T, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4): T[K0][K1][K2][K3][K4]
- safeGet<T, K0, K1, K2, K3, K4, K5>(obj: T, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4, k5: K5): T[K0][K1][K2][K3][K4][K5]
- safeGet<T, K0, K1, K2, K3, K4, K5, K6>(obj: T, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6): T[K0][K1][K2][K3][K4][K5][K6]
- safeGet<T, K0, K1, K2, K3, K4, K5, K6, K7>(obj: T, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6, k7: K7): T[K0][K1][K2][K3][K4][K5][K6][K7]
- safeGet<T, K0, K1, K2, K3, K4, K5, K6, K7, K8>(obj: T, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6, k7: K7, k8: K8): T[K0][K1][K2][K3][K4][K5][K6][K7][K8]
- safeGet<T, K0, K1, K2, K3, K4, K5, K6, K7, K8, K9>(obj: T, k0: K0, k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6, k7: K7, k8: K8, k9: K9): T[K0][K1][K2][K3][K4][K5][K6][K7][K8][K9]
-
Type parameters
Parameters
Returns T[K0]
-
Type parameters
-
-
K0: keyof T
-
K1: keyof T[K0]
Parameters
Returns T[K0][K1]
-
Type parameters
-
-
K0: keyof T
-
K1: keyof T[K0]
-
K2: keyof T[K0][K1]
Parameters
-
obj: T
-
k0: K0
-
k1: K1
-
k2: K2
Returns T[K0][K1][K2]
-
Type parameters
-
-
K0: keyof T
-
K1: keyof T[K0]
-
K2: keyof T[K0][K1]
-
K3: keyof T[K0][K1][K2]
Parameters
-
obj: T
-
k0: K0
-
k1: K1
-
k2: K2
-
k3: K3
Returns T[K0][K1][K2][K3]
-
Type parameters
-
-
K0: keyof T
-
K1: keyof T[K0]
-
K2: keyof T[K0][K1]
-
K3: keyof T[K0][K1][K2]
-
K4: keyof T[K0][K1][K2][K3]
Parameters
-
obj: T
-
k0: K0
-
k1: K1
-
k2: K2
-
k3: K3
-
k4: K4
Returns T[K0][K1][K2][K3][K4]
-
Type parameters
-
-
K0: keyof T
-
K1: keyof T[K0]
-
K2: keyof T[K0][K1]
-
K3: keyof T[K0][K1][K2]
-
K4: keyof T[K0][K1][K2][K3]
-
K5: keyof T[K0][K1][K2][K3][K4]
Parameters
-
obj: T
-
k0: K0
-
k1: K1
-
k2: K2
-
k3: K3
-
k4: K4
-
k5: K5
Returns T[K0][K1][K2][K3][K4][K5]
-
Type parameters
-
-
K0: keyof T
-
K1: keyof T[K0]
-
K2: keyof T[K0][K1]
-
K3: keyof T[K0][K1][K2]
-
K4: keyof T[K0][K1][K2][K3]
-
K5: keyof T[K0][K1][K2][K3][K4]
-
K6: keyof T[K0][K1][K2][K3][K4][K5]
Parameters
-
obj: T
-
k0: K0
-
k1: K1
-
k2: K2
-
k3: K3
-
k4: K4
-
k5: K5
-
k6: K6
Returns T[K0][K1][K2][K3][K4][K5][K6]
-
Type parameters
-
-
K0: keyof T
-
K1: keyof T[K0]
-
K2: keyof T[K0][K1]
-
K3: keyof T[K0][K1][K2]
-
K4: keyof T[K0][K1][K2][K3]
-
K5: keyof T[K0][K1][K2][K3][K4]
-
K6: keyof T[K0][K1][K2][K3][K4][K5]
-
K7: keyof T[K0][K1][K2][K3][K4][K5][K6]
Parameters
-
obj: T
-
k0: K0
-
k1: K1
-
k2: K2
-
k3: K3
-
k4: K4
-
k5: K5
-
k6: K6
-
k7: K7
Returns T[K0][K1][K2][K3][K4][K5][K6][K7]
-
Type parameters
-
-
K0: keyof T
-
K1: keyof T[K0]
-
K2: keyof T[K0][K1]
-
K3: keyof T[K0][K1][K2]
-
K4: keyof T[K0][K1][K2][K3]
-
K5: keyof T[K0][K1][K2][K3][K4]
-
K6: keyof T[K0][K1][K2][K3][K4][K5]
-
K7: keyof T[K0][K1][K2][K3][K4][K5][K6]
-
K8: keyof T[K0][K1][K2][K3][K4][K5][K6][K7]
Parameters
-
obj: T
-
k0: K0
-
k1: K1
-
k2: K2
-
k3: K3
-
k4: K4
-
k5: K5
-
k6: K6
-
k7: K7
-
k8: K8
Returns T[K0][K1][K2][K3][K4][K5][K6][K7][K8]
-
Type parameters
-
-
K0: keyof T
-
K1: keyof T[K0]
-
K2: keyof T[K0][K1]
-
K3: keyof T[K0][K1][K2]
-
K4: keyof T[K0][K1][K2][K3]
-
K5: keyof T[K0][K1][K2][K3][K4]
-
K6: keyof T[K0][K1][K2][K3][K4][K5]
-
K7: keyof T[K0][K1][K2][K3][K4][K5][K6]
-
K8: keyof T[K0][K1][K2][K3][K4][K5][K6][K7]
-
K9: keyof T[K0][K1][K2][K3][K4][K5][K6][K7][K8]
Parameters
-
obj: T
-
k0: K0
-
k1: K1
-
k2: K2
-
k3: K3
-
k4: K4
-
k5: K5
-
k6: K6
-
k7: K7
-
k8: K8
-
k9: K9
Returns T[K0][K1][K2][K3][K4][K5][K6][K7][K8][K9]
sample
- sample<T>(arr: T[]): T
- sample<T>(obj: T): Extract<T[keyof T], string | number>
-
Type parameters
Parameters
Returns T
返回随机获取的元素
-
Type parameters
Parameters
Returns Extract<T[keyof T], string | number>
返回随机获取的可枚举属性的值
sequential
- sequential<T, R>(tasks: T[]): Promise<unknown>
-
Type parameters
-
T: function
-
R: T extends (...args: any) => Promise<infer X> ? X : T extends (...args: any) => infer Y ? Y : any
Parameters
Returns Promise<unknown>
返回全部任务执行结果组成的数组
shuffle
- shuffle<T>(arr: T[]): T[]
-
Type parameters
Parameters
Returns T[]
返回打乱后的数组
startsWith
- startsWith(str: string, needle: string): boolean
sum
- sum(...numbers: Array<number | number[]>): number
-
Parameters
-
Rest ...numbers: Array<number | number[]>
Returns number
返回传入值的总和
sumBy
-
Type parameters
Parameters
Returns number
返回总和
throttle
- throttle<T>(fn: T, wait: number): function
-
Type parameters
Parameters
Returns function
返回节流后的函数
-
- (...args: Parameters<T>): void
-
Parameters
-
Rest ...args: Parameters<T>
Returns void
times
- times<T>(n: number, fn: T): Array<ReturnType<T>>
-
Type parameters
Parameters
Returns Array<ReturnType<T>>
返回每次的调用结果组成的数组
tryGet
-
Type parameters
Parameters
Returns R
返回结果值
-
Type parameters
Parameters
Returns R
|
undefined
返回结果值
unique
-
Type parameters
Parameters
Returns T[]
返回去重后的数组
values
- values<T>(obj: T): T[Extract<keyof T, string | number>][]
-
Type parameters
Parameters
Returns T[Extract<keyof T, string | number>][]
返回结果数组
任意函数类型。