index.d.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. export { debounce, throttle } from 'throttle-debounce';
  2. /**
  3. * Promise, or maybe not
  4. */
  5. declare type Awaitable<T> = T | PromiseLike<T>;
  6. /**
  7. * Null or whatever
  8. */
  9. declare type Nullable<T> = T | null | undefined;
  10. /**
  11. * Array, or not yet
  12. */
  13. declare type Arrayable<T> = T | Array<T>;
  14. /**
  15. * Function
  16. */
  17. declare type Fn<T = void> = () => T;
  18. /**
  19. * Constructor
  20. */
  21. declare type Constructor<T = void> = new (...args: any[]) => T;
  22. /**
  23. * Infers the element type of an array
  24. */
  25. declare type ElementOf<T> = T extends (infer E)[] ? E : never;
  26. /**
  27. * Defines an intersection type of all union items.
  28. *
  29. * @param U Union of any types that will be intersected.
  30. * @returns U items intersected
  31. * @see https://stackoverflow.com/a/50375286/9259330
  32. */
  33. declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
  34. /**
  35. * Infers the arguments type of a function
  36. */
  37. declare type ArgumentsType<T> = T extends ((...args: infer A) => any) ? A : never;
  38. declare type MergeInsertions<T> = T extends object ? {
  39. [K in keyof T]: MergeInsertions<T[K]>;
  40. } : T;
  41. declare type DeepMerge<F, S> = MergeInsertions<{
  42. [K in keyof F | keyof S]: K extends keyof S & keyof F ? DeepMerge<F[K], S[K]> : K extends keyof S ? S[K] : K extends keyof F ? F[K] : never;
  43. }>;
  44. /**
  45. * Convert `Arrayable<T>` to `Array<T>`
  46. *
  47. * @category Array
  48. */
  49. declare function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T>;
  50. /**
  51. * Convert `Arrayable<T>` to `Array<T>` and flatten it
  52. *
  53. * @category Array
  54. */
  55. declare function flattenArrayable<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T>;
  56. /**
  57. * Use rest arguments to merge arrays
  58. *
  59. * @category Array
  60. */
  61. declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
  62. declare type PartitionFilter<T> = (i: T, idx: number, arr: readonly T[]) => any;
  63. /**
  64. * Divide an array into two parts by a filter function
  65. *
  66. * @category Array
  67. * @example const [odd, even] = partition([1, 2, 3, 4], i => i % 2 != 0)
  68. */
  69. declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>): [T[], T[]];
  70. declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>): [T[], T[], T[]];
  71. declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>): [T[], T[], T[], T[]];
  72. declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>, f4: PartitionFilter<T>): [T[], T[], T[], T[], T[]];
  73. declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>, f4: PartitionFilter<T>, f5: PartitionFilter<T>): [T[], T[], T[], T[], T[], T[]];
  74. declare function partition<T>(array: readonly T[], f1: PartitionFilter<T>, f2: PartitionFilter<T>, f3: PartitionFilter<T>, f4: PartitionFilter<T>, f5: PartitionFilter<T>, f6: PartitionFilter<T>): [T[], T[], T[], T[], T[], T[], T[]];
  75. /**
  76. * Unique an Array
  77. *
  78. * @category Array
  79. */
  80. declare function uniq<T>(array: readonly T[]): T[];
  81. /**
  82. * Get last item
  83. *
  84. * @category Array
  85. */
  86. declare function last(array: readonly []): undefined;
  87. declare function last<T>(array: readonly T[]): T;
  88. /**
  89. * Remove an item from Array
  90. *
  91. * @category Array
  92. */
  93. declare function remove<T>(array: T[], value: T): boolean;
  94. /**
  95. * Get nth item of Array. Negative for backward
  96. *
  97. * @category Array
  98. */
  99. declare function at(array: readonly [], index: number): undefined;
  100. declare function at<T>(array: readonly T[], index: number): T;
  101. /**
  102. * Genrate a range array of numbers. The `stop` is exclusive.
  103. *
  104. * @category Array
  105. */
  106. declare function range(stop: number): number[];
  107. declare function range(start: number, stop: number, step?: number): number[];
  108. /**
  109. * Move element in an Array
  110. *
  111. * @category Array
  112. * @param arr
  113. * @param from
  114. * @param to
  115. */
  116. declare function move<T>(arr: T[], from: number, to: number): T[];
  117. /**
  118. * Clamp a number to the index ranage of an array
  119. *
  120. * @category Array
  121. */
  122. declare function clampArrayRange(n: number, arr: readonly unknown[]): number;
  123. declare const assert: (condition: boolean, message: string) => asserts condition;
  124. declare const toString: (v: any) => string;
  125. declare const noop: () => void;
  126. /**
  127. * Type guard to filter out null-ish values
  128. *
  129. * @category Guards
  130. * @example array.filter(notNullish)
  131. */
  132. declare function notNullish<T>(v: T | null | undefined): v is NonNullable<T>;
  133. /**
  134. * Type guard to filter out null values
  135. *
  136. * @category Guards
  137. * @example array.filter(noNull)
  138. */
  139. declare function noNull<T>(v: T | null): v is Exclude<T, null>;
  140. /**
  141. * Type guard to filter out null-ish values
  142. *
  143. * @category Guards
  144. * @example array.filter(notUndefined)
  145. */
  146. declare function notUndefined<T>(v: T): v is Exclude<T, undefined>;
  147. /**
  148. * Type guard to filter out falsy values
  149. *
  150. * @category Guards
  151. * @example array.filter(isTruthy)
  152. */
  153. declare function isTruthy<T>(v: T): v is NonNullable<T>;
  154. declare const isDef: <T = any>(val?: T | undefined) => val is T;
  155. declare const isBoolean: (val: any) => val is boolean;
  156. declare const isFunction: <T extends Function>(val: any) => val is T;
  157. declare const isNumber: (val: any) => val is number;
  158. declare const isString: (val: unknown) => val is string;
  159. declare const isObject: (val: any) => val is object;
  160. declare const isWindow: (val: any) => boolean;
  161. declare const isBrowser: boolean;
  162. declare function clamp(n: number, min: number, max: number): number;
  163. declare function sum(...args: number[] | number[][]): number;
  164. /**
  165. * Replace backslash to slash
  166. *
  167. * @category String
  168. */
  169. declare function slash(str: string): string;
  170. /**
  171. * Ensure prefix of a string
  172. *
  173. * @category String
  174. */
  175. declare function ensurePrefix(prefix: string, str: string): string;
  176. /**
  177. * Dead simple template engine, just like Python's `.format()`
  178. *
  179. * @example
  180. * ```
  181. * const result = template(
  182. * 'Hello {0}! My name is {1}.',
  183. * 'Inès',
  184. * 'Anthony'
  185. * ) // Hello Inès! My name is Anthony.
  186. * ```
  187. */
  188. declare function template(str: string, ...args: any[]): string;
  189. declare const timestamp: () => number;
  190. /**
  191. * Call every function in an array
  192. */
  193. declare function batchInvoke(functions: Nullable<Fn>[]): void;
  194. /**
  195. * Call the function
  196. */
  197. declare function invoke(fn: Fn): void;
  198. /**
  199. * Pass the value through the callback, and return the value
  200. *
  201. * @example
  202. * ```
  203. * function createUser(name: string): User {
  204. * return tap(new User, user => {
  205. * user.name = name
  206. * })
  207. * }
  208. * ```
  209. */
  210. declare function tap<T>(value: T, callback: (value: T) => void): T;
  211. /**
  212. * Map key/value pairs for an object, and construct a new one
  213. *
  214. *
  215. * @category Object
  216. *
  217. * Transform:
  218. * @example
  219. * ```
  220. * objectMap({ a: 1, b: 2 }, (k, v) => [k.toString().toUpperCase(), v.toString()])
  221. * // { A: '1', B: '2' }
  222. * ```
  223. *
  224. * Swap key/value:
  225. * @example
  226. * ```
  227. * objectMap({ a: 1, b: 2 }, (k, v) => [v, k])
  228. * // { 1: 'a', 2: 'b' }
  229. * ```
  230. *
  231. * Filter keys:
  232. * @example
  233. * ```
  234. * objectMap({ a: 1, b: 2 }, (k, v) => k === 'a' ? undefined : [k, v])
  235. * // { b: 2 }
  236. * ```
  237. */
  238. declare function objectMap<K extends string, V, NK = K, NV = V>(obj: Record<K, V>, fn: (key: K, value: V) => [NK, NV] | undefined): Record<K, V>;
  239. /**
  240. * Type guard for any key, `k`.
  241. * Marks `k` as a key of `T` if `k` is in `obj`.
  242. *
  243. * @category Object
  244. * @param obj object to query for key `k`
  245. * @param k key to check existence in `obj`
  246. */
  247. declare function isKeyOf<T extends object>(obj: T, k: keyof any): k is keyof T;
  248. /**
  249. * Strict typed `Object.keys`
  250. *
  251. * @category Object
  252. */
  253. declare function objectKeys<T extends object>(obj: T): (keyof T)[];
  254. /**
  255. * Strict typed `Object.entries`
  256. *
  257. * @category Object
  258. */
  259. declare function objectEntries<T extends object>(obj: T): [keyof T, T[keyof T]][];
  260. /**
  261. * Deep merge :P
  262. *
  263. * @category Object
  264. */
  265. declare function deepMerge<T extends object = object, S extends object = T>(target: T, ...sources: S[]): DeepMerge<T, S>;
  266. /**
  267. * Create a new subset object by giving keys
  268. *
  269. * @category Object
  270. */
  271. declare function objectPick<O, T extends keyof O>(obj: O, keys: T[], omitUndefined?: boolean): Pick<O, T>;
  272. /**
  273. * Clear undefined fields from an object. It mutates the object
  274. *
  275. * @category Object
  276. */
  277. declare function clearUndefined<T extends object>(obj: T): T;
  278. /**
  279. * Determines whether an object has a property with the specified name
  280. *
  281. * @see https://eslint.org/docs/rules/no-prototype-builtins
  282. * @category Object
  283. */
  284. declare function hasOwnProperty<T>(obj: T, v: PropertyKey): boolean;
  285. interface SingletonPromiseReturn<T> {
  286. (): Promise<T>;
  287. /**
  288. * Reset current staled promise.
  289. * Await it to have proper shutdown.
  290. */
  291. reset: () => Promise<void>;
  292. }
  293. /**
  294. * Create singleton promise function
  295. *
  296. * @example
  297. * ```
  298. * const promise = createSingletonPromise(async () => { ... })
  299. *
  300. * await promise()
  301. * await promise() // all of them will be bind to a single promise instance
  302. * await promise() // and be resolved together
  303. * ```
  304. */
  305. declare function createSingletonPromise<T>(fn: () => Promise<T>): SingletonPromiseReturn<T>;
  306. /**
  307. * Promised `setTimeout`
  308. */
  309. declare function sleep(ms: number, callback?: Fn<any>): Promise<void>;
  310. /**
  311. * Create a promise lock
  312. *
  313. * @example
  314. * ```
  315. * const lock = createPromiseLock()
  316. *
  317. * lock.run(async () => {
  318. * await doSomething()
  319. * })
  320. *
  321. * // in anther context:
  322. * await lock.wait() // it will wait all tasking finished
  323. * ```
  324. */
  325. declare function createPromiseLock(): {
  326. run<T = void>(fn: () => Promise<T>): Promise<T>;
  327. wait(): Promise<void>;
  328. isWaiting(): boolean;
  329. clear(): void;
  330. };
  331. export { ArgumentsType, Arrayable, Awaitable, Constructor, DeepMerge, ElementOf, Fn, MergeInsertions, Nullable, PartitionFilter, SingletonPromiseReturn, UnionToIntersection, assert, at, batchInvoke, clamp, clampArrayRange, clearUndefined, createPromiseLock, createSingletonPromise, deepMerge, ensurePrefix, flattenArrayable, hasOwnProperty, invoke, isBoolean, isBrowser, isDef, isFunction, isKeyOf, isNumber, isObject, isString, isTruthy, isWindow, last, mergeArrayable, move, noNull, noop, notNullish, notUndefined, objectEntries, objectKeys, objectMap, objectPick, partition, range, remove, slash, sleep, sum, tap, template, timestamp, toArray, toString, uniq };