@ditojs/utils 1.12.0 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ditojs/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Dito.js Utility Functions – Dito.js is a declarative and modern web framework, based on Objection.js, Koa.js and Vue.js",
|
|
6
6
|
"repository": "https://github.com/ditojs/dito/tree/master/packages/utils",
|
|
@@ -11,8 +11,12 @@
|
|
|
11
11
|
".": "./src/index.js"
|
|
12
12
|
},
|
|
13
13
|
"files": [
|
|
14
|
-
"src/"
|
|
14
|
+
"src/",
|
|
15
|
+
"types/"
|
|
15
16
|
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"types": "tsc --noEmit types/index.d.ts"
|
|
19
|
+
},
|
|
16
20
|
"engines": {
|
|
17
21
|
"node": ">= 18.0.0",
|
|
18
22
|
"yarn": ">= 1.0.0"
|
|
@@ -27,5 +31,9 @@
|
|
|
27
31
|
"dependencies": {
|
|
28
32
|
"punycode": "^2.1.1"
|
|
29
33
|
},
|
|
30
|
-
"
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"typescript": "^4.8.4"
|
|
36
|
+
},
|
|
37
|
+
"types": "types",
|
|
38
|
+
"gitHead": "b65d7d15871b0d11579565df92780536ef2926da"
|
|
31
39
|
}
|
|
@@ -19,7 +19,7 @@ describe('format()', () => {
|
|
|
19
19
|
it('should use the en-US locale by default', () => {
|
|
20
20
|
expect(format(integer)).toBe('123,456,789')
|
|
21
21
|
expect(format(float)).toBe('123,456.789')
|
|
22
|
-
expect(format(date)).toBe('June 9, 2012
|
|
22
|
+
expect(format(date)).toBe('June 9, 2012 at 10:45:30 PM')
|
|
23
23
|
})
|
|
24
24
|
|
|
25
25
|
it('should format numbers with different locale and default options', () => {
|
|
@@ -28,7 +28,7 @@ describe('format()', () => {
|
|
|
28
28
|
})
|
|
29
29
|
|
|
30
30
|
it('should format dates with different locale and default options', () => {
|
|
31
|
-
expect(format(date, { locale: 'de-DE' })).toBe('9. Juni 2012
|
|
31
|
+
expect(format(date, { locale: 'de-DE' })).toBe('9. Juni 2012 um 22:45:30')
|
|
32
32
|
})
|
|
33
33
|
|
|
34
34
|
it('should format string as numbers if told so', () => {
|
|
@@ -46,7 +46,7 @@ describe('format()', () => {
|
|
|
46
46
|
).toBe('2. Januar 1970')
|
|
47
47
|
expect(
|
|
48
48
|
format(integer, { locale: 'de-DE', date: true, time: true })
|
|
49
|
-
).toBe('2. Januar 1970
|
|
49
|
+
).toBe('2. Januar 1970 um 11:17:36')
|
|
50
50
|
})
|
|
51
51
|
|
|
52
52
|
it('should omit time when `options.date = true`', () => {
|
|
@@ -188,7 +188,7 @@ describe('format()', () => {
|
|
|
188
188
|
).toBe('May 24, 2016')
|
|
189
189
|
expect(
|
|
190
190
|
format('2016-05-24T15:54:14.876Z', { date: true, time: true })
|
|
191
|
-
).toBe('May 24, 2016
|
|
191
|
+
).toBe('May 24, 2016 at 05:54:14 PM')
|
|
192
192
|
expect(format('123456789', { number: true })).toBe('123,456,789')
|
|
193
193
|
expect(format('123456.789', { number: true })).toBe('123,456.789')
|
|
194
194
|
expect(format('Hello World')).toBe('Hello World')
|
|
@@ -8,20 +8,21 @@ describe('formatDate()', () => {
|
|
|
8
8
|
})
|
|
9
9
|
|
|
10
10
|
it('should use the en-US locale by default', () => {
|
|
11
|
-
expect(formatDate(date)).toBe('June 9, 2012
|
|
12
|
-
expect(formatDate(date, {})).toBe('June 9, 2012
|
|
11
|
+
expect(formatDate(date)).toBe('June 9, 2012 at 10:45:30 PM')
|
|
12
|
+
expect(formatDate(date, {})).toBe('June 9, 2012 at 10:45:30 PM')
|
|
13
13
|
})
|
|
14
14
|
|
|
15
15
|
it('should format dates with different locale and default options', () => {
|
|
16
|
-
expect(formatDate(date, { locale: 'de-DE' }))
|
|
16
|
+
expect(formatDate(date, { locale: 'de-DE' }))
|
|
17
|
+
.toBe('9. Juni 2012 um 22:45:30')
|
|
17
18
|
})
|
|
18
19
|
|
|
19
20
|
it('should format numbers or strings as dates', () => {
|
|
20
21
|
expect(formatDate(0, { locale: 'de-DE' }))
|
|
21
|
-
.toBe('1. Januar 1970
|
|
22
|
+
.toBe('1. Januar 1970 um 01:00:00')
|
|
22
23
|
expect(formatDate(1000000000000, { locale: 'de-DE' }))
|
|
23
|
-
.toBe('9. September 2001
|
|
24
|
+
.toBe('9. September 2001 um 03:46:40')
|
|
24
25
|
expect(formatDate('04 Dec 1995 00:12:00 GMT', { locale: 'de-DE' }))
|
|
25
|
-
.toBe('4. Dezember 1995
|
|
26
|
+
.toBe('4. Dezember 1995 um 01:12:00')
|
|
26
27
|
})
|
|
27
28
|
})
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,638 @@
|
|
|
1
|
+
// Type definitions for Dito.js utils
|
|
2
|
+
// Project: <https://github.com/ditojs/dito/>
|
|
3
|
+
|
|
4
|
+
/* ---------------------------------- base ---------------------------------- */
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Determines whether both supplied values are the same value, using the
|
|
8
|
+
* SameValue algorithm.
|
|
9
|
+
* @see {@link http://ecma-international.org/ecma-262/5.1/#sec-9.12 The SameValue Algorithm}
|
|
10
|
+
*/
|
|
11
|
+
export function is(value1: any, value2: any): boolean
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Determines whether the supplied value is a plain object.
|
|
15
|
+
*/
|
|
16
|
+
export function isPlainObject(arg: any): boolean
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Determines whether the supplied value is an object.
|
|
20
|
+
*/
|
|
21
|
+
export function isObject(arg: any): boolean
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Determines whether the supplied value is a function.
|
|
25
|
+
*/
|
|
26
|
+
export function isFunction(arg: any): arg is (...args: any[]) => any
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Determines whether the supplied value is a number.
|
|
30
|
+
*/
|
|
31
|
+
export function isNumber(arg: any): arg is number
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Determines whether the supplied value is a string.
|
|
35
|
+
*/
|
|
36
|
+
export function isString(arg: any): arg is string | String
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Determines whether the supplied value is a boolean.
|
|
40
|
+
*/
|
|
41
|
+
export function isBoolean(arg: any): arg is boolean
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Determines whether the supplied value is a Date object.
|
|
45
|
+
*/
|
|
46
|
+
export function isDate(arg: any): arg is Date
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Determines whether the supplied value is a regular expression (RegExp).
|
|
50
|
+
*/
|
|
51
|
+
export function isRegExp(arg: any): arg is RegExp
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Determines whether the supplied value is a Promise.
|
|
55
|
+
*/
|
|
56
|
+
export function isPromise(arg: any): arg is Promise<any>
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Determines whether the supplied value is an integer.
|
|
60
|
+
*/
|
|
61
|
+
export function isInteger(arg: any): boolean
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Determines whether the supplied value is an async function.
|
|
65
|
+
*/
|
|
66
|
+
export function isAsync(arg: any): boolean
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Determines whether the supplied value is array like, i.e. it has a length
|
|
70
|
+
• property between `0` and `Number.MAX_SAFE_INTEGER` and is not a function.
|
|
71
|
+
*/
|
|
72
|
+
export function isArrayLike(arg: any): arg is any[]
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Determines whether the supplied value can be considered empty,
|
|
76
|
+
* i.e. undefined, null, an empty string, or an object without properties.
|
|
77
|
+
*/
|
|
78
|
+
export function isEmpty(o: any): boolean
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Returns the supplied value as an object. The most frequent use case is to
|
|
82
|
+
* add properties to a primitive. As primitives are immutable, they need to be
|
|
83
|
+
* converted to an object in order to do so.
|
|
84
|
+
*
|
|
85
|
+
* @see {@link https://2ality.com/2011/04/javascript-converting-any-value-to.html JavaScript: converting any value to an object}
|
|
86
|
+
*/
|
|
87
|
+
export function asObject<O extends {}, T extends any>(arg: T): T & O
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns the supplied value as an array.
|
|
91
|
+
*
|
|
92
|
+
* When the supplied value is an array, the original array is returned. When
|
|
93
|
+
* the supplied value is undefined, an empty array is returned. Otherwise an
|
|
94
|
+
* array is returned containing the supplied value as its only element.
|
|
95
|
+
*/
|
|
96
|
+
export function asArray<T extends any>(o: T): T extends any[] ? T : T[]
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Returns the supplied value as a function.
|
|
100
|
+
*
|
|
101
|
+
* When the supplied value is a function, the original function is returned.
|
|
102
|
+
* Otherwise a function is returned that returns the value when called.
|
|
103
|
+
*/
|
|
104
|
+
export function asFunction<T extends any>(o: T): T extends Function ? T : () => T
|
|
105
|
+
|
|
106
|
+
/* --------------------------------- object --------------------------------- */
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Performs a deep (recursive) clone on the supplied value.
|
|
110
|
+
*/
|
|
111
|
+
export function clone<T extends any>(arg: T, iteratee?: (arg: any) => void): T
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Determines whether the supplied values can be considered equal through
|
|
115
|
+
* recursive comparison of object properties and array elements. Non-objects
|
|
116
|
+
* and non-arrays are compared using the SameValue algorithm.
|
|
117
|
+
*/
|
|
118
|
+
export function equals(arg1: any, arg2: any): boolean
|
|
119
|
+
|
|
120
|
+
// TODO: document groupBy
|
|
121
|
+
export function groupBy<T, K extends string | number | symbol>(
|
|
122
|
+
list: T[], callback: (item: T) => K
|
|
123
|
+
): Record<K, T[]>
|
|
124
|
+
|
|
125
|
+
// TODO: document merge
|
|
126
|
+
export function merge<ArgA, ArgB>(a: ArgA, b: ArgB): ArgA & ArgB
|
|
127
|
+
export function merge<ArgA, ArgB, ArgC>(a: ArgA, b: ArgB, c: ArgC): ArgA & ArgB & ArgC
|
|
128
|
+
export function merge<ArgA, ArgB, ArgC, ArgD>(
|
|
129
|
+
a: ArgA,
|
|
130
|
+
b: ArgB,
|
|
131
|
+
c: ArgC,
|
|
132
|
+
d: ArgD
|
|
133
|
+
): ArgA & ArgB & ArgC & ArgD
|
|
134
|
+
export function merge<ArgA, ArgB, ArgC, ArgD, ArgE>(
|
|
135
|
+
a: ArgA,
|
|
136
|
+
b: ArgB,
|
|
137
|
+
c: ArgC,
|
|
138
|
+
d: ArgD,
|
|
139
|
+
e: ArgE
|
|
140
|
+
): ArgA & ArgB & ArgC & ArgD & ArgE
|
|
141
|
+
export function merge(...args: any[]): any
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Returns the first argument which is not undefined.
|
|
145
|
+
*/
|
|
146
|
+
export function pick<ArgA, ArgB>(a: ArgA, b: ArgB): ArgA | ArgB
|
|
147
|
+
export function pick<ArgA, ArgB, ArgC>(a: ArgA, b: ArgB, c: ArgC): ArgA | ArgB | ArgC
|
|
148
|
+
export function pick<ArgA, ArgB, ArgC, ArgD>(
|
|
149
|
+
a: ArgA,
|
|
150
|
+
b: ArgB,
|
|
151
|
+
c: ArgC,
|
|
152
|
+
d: ArgD
|
|
153
|
+
): ArgA | ArgB | ArgC | ArgD
|
|
154
|
+
export function pick<ArgA, ArgB, ArgC, ArgD, ArgE>(
|
|
155
|
+
a: ArgA,
|
|
156
|
+
b: ArgB,
|
|
157
|
+
c: ArgC,
|
|
158
|
+
d: ArgD,
|
|
159
|
+
e: ArgE
|
|
160
|
+
): ArgA | ArgB | ArgC | ArgD | ArgE
|
|
161
|
+
export function pick(...args: any[]): any
|
|
162
|
+
/**
|
|
163
|
+
* Creates an object composed of the object properties predicate returns
|
|
164
|
+
* truthy for.
|
|
165
|
+
* @param object The source object.
|
|
166
|
+
* @param callback Callback invoked with three arguments: (value, key, item).
|
|
167
|
+
*/
|
|
168
|
+
export function pickBy<T extends Dictionary<any>>(
|
|
169
|
+
object: T,
|
|
170
|
+
callback?: (value: T[keyof T], key: keyof T, object: T) => any
|
|
171
|
+
): Partial<T>
|
|
172
|
+
|
|
173
|
+
export function mapKeys<T extends Dictionary<any>, K extends keyof any>(
|
|
174
|
+
object: T,
|
|
175
|
+
callback?: (key: keyof T, value: T[keyof T], object: T) => K
|
|
176
|
+
): Record<K, T[keyof T]>
|
|
177
|
+
|
|
178
|
+
export function mapValues<T extends Dictionary<any>, K>(
|
|
179
|
+
object: T,
|
|
180
|
+
callback?: (value: T[keyof T], key: keyof T, object: T) => K
|
|
181
|
+
): Record<keyof T, K>
|
|
182
|
+
|
|
183
|
+
/* --------------------------------- string --------------------------------- */
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Converts a string seperated by spaces, dashes and underscores to camel-case
|
|
187
|
+
* (`'camelCase'`) or optionally pascal-case (`'CamelCase'`).
|
|
188
|
+
*/
|
|
189
|
+
export function camelize(str: string, pascalCase?: boolean): string
|
|
190
|
+
/**
|
|
191
|
+
* Capitalizes words in a string.
|
|
192
|
+
*/
|
|
193
|
+
export function capitalize(str: string): string
|
|
194
|
+
/**
|
|
195
|
+
* Convert a string from camel-case to a string seperated by spaces or a
|
|
196
|
+
* supplied seperator string.
|
|
197
|
+
*
|
|
198
|
+
* @param str The string to decamelize.
|
|
199
|
+
* @param {string} [sep=' '] - The string to seperate the decamelized words with.
|
|
200
|
+
*/
|
|
201
|
+
export function decamelize(str: string, sep?: string): string
|
|
202
|
+
/**
|
|
203
|
+
* Converts a camelized string to a string seperated by dashes.
|
|
204
|
+
*/
|
|
205
|
+
export function hyphenate(str: string): string
|
|
206
|
+
/**
|
|
207
|
+
* Converts a camelized string to a string seperated by underscores.
|
|
208
|
+
*/
|
|
209
|
+
export function underscore(str: string): string
|
|
210
|
+
/**
|
|
211
|
+
* ES6 string tag that strips indentation from multi-line strings.
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* const str = deindent`Such a long string that you need to break it
|
|
215
|
+
* over multiple lines. Such a long string that
|
|
216
|
+
* you need to break it over multiple lines.`
|
|
217
|
+
* console.log(str)
|
|
218
|
+
*/
|
|
219
|
+
export function deindent(strings: OrArrayOf<string>, ...values: Array<string>): string
|
|
220
|
+
/**
|
|
221
|
+
* Returns the longest prefix string that is common to both supplied strings.
|
|
222
|
+
*/
|
|
223
|
+
export function getCommonPrefix(str1: string, str2: string): string
|
|
224
|
+
/**
|
|
225
|
+
* Returns the offset of the longest prefix string that is common to both
|
|
226
|
+
* supplied strings.
|
|
227
|
+
*/
|
|
228
|
+
export function getCommonOffset(str1: string, str2: string): number
|
|
229
|
+
/**
|
|
230
|
+
* Determines whether the supplied string is an absolute url.
|
|
231
|
+
*
|
|
232
|
+
* A URL is considered absolute if it begins with "<scheme>://" or "//"
|
|
233
|
+
* (protocol-relative URL). RFC 3986 defines scheme name as a sequence of
|
|
234
|
+
* characters beginning with a letter and followed by any combination of
|
|
235
|
+
* letters, digits, plus, period, or hyphen.
|
|
236
|
+
*/
|
|
237
|
+
export function isAbsoluteUrl(str: string): boolean
|
|
238
|
+
/**
|
|
239
|
+
* Determines whether the supplied string is a valid creditcard number.
|
|
240
|
+
*/
|
|
241
|
+
export function isCreditCard(str: string): boolean
|
|
242
|
+
/**
|
|
243
|
+
* Determines whether the supplied string is a valid email address.
|
|
244
|
+
*/
|
|
245
|
+
export function isEmail(str: string): boolean
|
|
246
|
+
/**
|
|
247
|
+
* Determines whether the string is a valid hostname.
|
|
248
|
+
*/
|
|
249
|
+
export function isHostname(str: string): boolean
|
|
250
|
+
/**
|
|
251
|
+
* Determines whether the string is a valid url.
|
|
252
|
+
*/
|
|
253
|
+
export function isUrl(str: string): boolean
|
|
254
|
+
/**
|
|
255
|
+
* Expands hyphenated, underscored and camel-cased strings to title case.
|
|
256
|
+
*/
|
|
257
|
+
export function labelize(str: string | undefined): string
|
|
258
|
+
|
|
259
|
+
/* ---------------------------------- array --------------------------------- */
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Recursively flattens a nested array.
|
|
263
|
+
*
|
|
264
|
+
* @param array The array to recursively flatten.
|
|
265
|
+
* @param [maxDepth] The maximum recursion depth.
|
|
266
|
+
* @return Returns the new flattened array.
|
|
267
|
+
*/
|
|
268
|
+
export function flatten<T>(array: RecursiveArray<T>, maxDepth?: number): T[]
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Shuffle an array using the Fisher-Yates (aka Knuth) Shuffle.
|
|
272
|
+
*
|
|
273
|
+
* @param array The array to shuffle.
|
|
274
|
+
* @returns Returns the newly shuffled array.
|
|
275
|
+
*/
|
|
276
|
+
export function shuffle<T>(array: T[]): T[]
|
|
277
|
+
|
|
278
|
+
/* ---------------------------------- date ---------------------------------- */
|
|
279
|
+
|
|
280
|
+
export interface TimeFormat {
|
|
281
|
+
/**
|
|
282
|
+
* @defaultValue `'2-digit'`
|
|
283
|
+
*/
|
|
284
|
+
hour?: boolean | 'numeric' | '2-digit'
|
|
285
|
+
/**
|
|
286
|
+
* @defaultValue `'2-digit'`
|
|
287
|
+
*/
|
|
288
|
+
minute?: boolean | 'numeric' | '2-digit'
|
|
289
|
+
/**
|
|
290
|
+
* @defaultValue `'2-digit'`
|
|
291
|
+
*/
|
|
292
|
+
second?: boolean | 'numeric' | '2-digit'
|
|
293
|
+
|
|
294
|
+
format?: (
|
|
295
|
+
value: string,
|
|
296
|
+
type: Intl.DateTimeFormatPartTypes,
|
|
297
|
+
options: Omit<TimeFormat, 'format'>
|
|
298
|
+
) => string
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export interface DateFormat {
|
|
302
|
+
/**
|
|
303
|
+
* @defaultValue `'numeric'`
|
|
304
|
+
*/
|
|
305
|
+
day?: boolean | 'numeric' | '2-digit'
|
|
306
|
+
/**
|
|
307
|
+
* @defaultValue `'long'`
|
|
308
|
+
*/
|
|
309
|
+
month?: boolean | 'numeric' | '2-digit' | 'long' | 'short' | 'narrow'
|
|
310
|
+
/**
|
|
311
|
+
* @defaultValue `'numeric'`
|
|
312
|
+
*/
|
|
313
|
+
year?: boolean | 'numeric' | '2-digit'
|
|
314
|
+
/**
|
|
315
|
+
* Format date in a custom way.
|
|
316
|
+
*/
|
|
317
|
+
format?: (
|
|
318
|
+
value: string,
|
|
319
|
+
type: Intl.DateTimeFormatPartTypes,
|
|
320
|
+
options: Omit<DateFormat, 'format'>
|
|
321
|
+
) => string
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export interface NumberFormat extends Intl.NumberFormatOptions {
|
|
325
|
+
format?: (
|
|
326
|
+
value: string,
|
|
327
|
+
type: Intl.NumberFormatPartTypes,
|
|
328
|
+
options: Intl.NumberFormatOptions
|
|
329
|
+
) => string | undefined
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Formats the value as a string.
|
|
334
|
+
*/
|
|
335
|
+
export function format(
|
|
336
|
+
value: Date,
|
|
337
|
+
options?: {
|
|
338
|
+
/**
|
|
339
|
+
* @default 'en-US'
|
|
340
|
+
*/
|
|
341
|
+
locale?: string
|
|
342
|
+
date?: boolean | DateFormat
|
|
343
|
+
time?: boolean | TimeFormat
|
|
344
|
+
number?: boolean | NumberFormat
|
|
345
|
+
}
|
|
346
|
+
): string
|
|
347
|
+
/* -------------------------------- function -------------------------------- */
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Creates a debounced function that delays invoking func until after wait
|
|
351
|
+
* milliseconds have elapsed since the last time the debounced function was
|
|
352
|
+
* invoked. The debounced function comes with a cancel method to cancel
|
|
353
|
+
* delayed invocations. Provide an options object to indicate that func
|
|
354
|
+
* should be invoked on the leading edge of the wait timeout. Subsequent
|
|
355
|
+
* calls to the debounced function return the result of the last func
|
|
356
|
+
* invocation.
|
|
357
|
+
*
|
|
358
|
+
* @param func The function to debounce.
|
|
359
|
+
* @param options The options object or the number of milliseconds to delay.
|
|
360
|
+
* @param options.delay The number of milliseconds to delay.
|
|
361
|
+
* @param options.immediate Specify invoking on the leading edge of the timeout.
|
|
362
|
+
* @return Returns the new debounced function.
|
|
363
|
+
*/
|
|
364
|
+
export function debounce<T extends (...args: any[]) => any>(
|
|
365
|
+
func: T,
|
|
366
|
+
options:
|
|
367
|
+
| number
|
|
368
|
+
| {
|
|
369
|
+
delay: number
|
|
370
|
+
immediate?: boolean
|
|
371
|
+
}
|
|
372
|
+
): T & { cancel: () => boolean }
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Creates a debounced function that delays invoking func until after wait
|
|
376
|
+
* milliseconds have elapsed since the last time the debounced function was
|
|
377
|
+
* invoked.
|
|
378
|
+
*
|
|
379
|
+
* Each time the debounced function is called, it returns a promise
|
|
380
|
+
* which will resolve to the value that the debounced async function resolves
|
|
381
|
+
* to. If the async function throws an error, the last waiting promise is
|
|
382
|
+
* rejected and all others are resolved with undefined.
|
|
383
|
+
*
|
|
384
|
+
* The debounced function comes with a cancel method to cancel
|
|
385
|
+
* delayed invocations. Provide an options object to indicate that func
|
|
386
|
+
* should be invoked on the leading edge of the wait timeout.
|
|
387
|
+
*
|
|
388
|
+
* @param func The function to debounce.
|
|
389
|
+
* @param options The options object or the number of milliseconds to delay.
|
|
390
|
+
* @param options.delay The number of milliseconds to delay.
|
|
391
|
+
* @param options.immediate Specify invoking on the leading edge of the timeout.
|
|
392
|
+
* @return Returns the new debounced function.
|
|
393
|
+
*/
|
|
394
|
+
export function debounceAsync<T extends (...args: any[]) => Promise<any>>(
|
|
395
|
+
func: T,
|
|
396
|
+
options:
|
|
397
|
+
| number
|
|
398
|
+
| {
|
|
399
|
+
delay: number
|
|
400
|
+
immediate?: boolean
|
|
401
|
+
}
|
|
402
|
+
): T & { cancel: () => boolean }
|
|
403
|
+
|
|
404
|
+
// Adjusted version of @types/node's promisify (which includes support for overloaded
|
|
405
|
+
// node function types), excluding the custom promisified function
|
|
406
|
+
// functionality, which toAsync does not support:
|
|
407
|
+
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/fa723dee727e433add7e700420523f0f90e87a01/types/node/util.d.ts#L86
|
|
408
|
+
export interface CustomPromisifyLegacy<TCustom extends Function> extends Function {
|
|
409
|
+
__promisify__: TCustom
|
|
410
|
+
}
|
|
411
|
+
type CustomToAsync<TCustom extends Function> = CustomPromisifyLegacy<TCustom>
|
|
412
|
+
/**
|
|
413
|
+
* Takes a function following the common error-first callback style, i.e.
|
|
414
|
+
* taking an (err, value) => ... callback as the last argument, and returns a
|
|
415
|
+
* version that returns promises.
|
|
416
|
+
*
|
|
417
|
+
* @param fn function following the error-first callback style
|
|
418
|
+
*/
|
|
419
|
+
export function toAsync<TCustom extends Function>(fn: CustomToAsync<TCustom>): TCustom
|
|
420
|
+
export function toAsync<R>(
|
|
421
|
+
fn: (callback: (err: any, result: R) => void) => void
|
|
422
|
+
): () => Promise<R>
|
|
423
|
+
export function toAsync(
|
|
424
|
+
fn: (callback: (err?: any) => void) => void
|
|
425
|
+
): () => Promise<void>
|
|
426
|
+
export function toAsync<T1, R>(
|
|
427
|
+
fn: (arg1: T1, callback: (err: any, result: R) => void) => void
|
|
428
|
+
): (arg1: T1) => Promise<R>
|
|
429
|
+
export function toAsync<T1>(
|
|
430
|
+
fn: (arg1: T1, callback: (err?: any) => void) => void
|
|
431
|
+
): (arg1: T1) => Promise<void>
|
|
432
|
+
export function toAsync<T1, T2, R>(
|
|
433
|
+
fn: (arg1: T1, arg2: T2, callback: (err: any, result: R) => void) => void
|
|
434
|
+
): (arg1: T1, arg2: T2) => Promise<R>
|
|
435
|
+
export function toAsync<T1, T2>(
|
|
436
|
+
fn: (arg1: T1, arg2: T2, callback: (err?: any) => void) => void
|
|
437
|
+
): (arg1: T1, arg2: T2) => Promise<void>
|
|
438
|
+
export function toAsync<T1, T2, T3, R>(
|
|
439
|
+
fn: (
|
|
440
|
+
arg1: T1,
|
|
441
|
+
arg2: T2,
|
|
442
|
+
arg3: T3,
|
|
443
|
+
callback: (err: any, result: R) => void
|
|
444
|
+
) => void
|
|
445
|
+
): (arg1: T1, arg2: T2, arg3: T3) => Promise<R>
|
|
446
|
+
export function toAsync<T1, T2, T3>(
|
|
447
|
+
fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err?: any) => void) => void
|
|
448
|
+
): (arg1: T1, arg2: T2, arg3: T3) => Promise<void>
|
|
449
|
+
export function toAsync<T1, T2, T3, T4, R>(
|
|
450
|
+
fn: (
|
|
451
|
+
arg1: T1,
|
|
452
|
+
arg2: T2,
|
|
453
|
+
arg3: T3,
|
|
454
|
+
arg4: T4,
|
|
455
|
+
callback: (err: any, result: R) => void
|
|
456
|
+
) => void
|
|
457
|
+
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<R>
|
|
458
|
+
export function toAsync<T1, T2, T3, T4>(
|
|
459
|
+
fn: (
|
|
460
|
+
arg1: T1,
|
|
461
|
+
arg2: T2,
|
|
462
|
+
arg3: T3,
|
|
463
|
+
arg4: T4,
|
|
464
|
+
callback: (err?: any) => void
|
|
465
|
+
) => void
|
|
466
|
+
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<void>
|
|
467
|
+
export function toAsync<T1, T2, T3, T4, T5, R>(
|
|
468
|
+
fn: (
|
|
469
|
+
arg1: T1,
|
|
470
|
+
arg2: T2,
|
|
471
|
+
arg3: T3,
|
|
472
|
+
arg4: T4,
|
|
473
|
+
arg5: T5,
|
|
474
|
+
callback: (err: any, result: R) => void
|
|
475
|
+
) => void
|
|
476
|
+
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise<R>
|
|
477
|
+
export function toAsync<T1, T2, T3, T4, T5>(
|
|
478
|
+
fn: (
|
|
479
|
+
arg1: T1,
|
|
480
|
+
arg2: T2,
|
|
481
|
+
arg3: T3,
|
|
482
|
+
arg4: T4,
|
|
483
|
+
arg5: T5,
|
|
484
|
+
callback: (err?: any) => void
|
|
485
|
+
) => void
|
|
486
|
+
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise<void>
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Takes an async function (or a function that returns a Promise) and returns
|
|
490
|
+
* a function following the error-first callback style, i.e. taking an
|
|
491
|
+
* (err, value) => ... callback as the last argument.
|
|
492
|
+
*/
|
|
493
|
+
export function toCallback(
|
|
494
|
+
fn: () => Promise<void>
|
|
495
|
+
): (callback: (err: any) => void) => void
|
|
496
|
+
export function toCallback<R>(
|
|
497
|
+
fn: () => Promise<R>
|
|
498
|
+
): (callback: (err: any, result: R) => void) => void
|
|
499
|
+
export function toCallback<T1>(
|
|
500
|
+
fn: (arg1: T1) => Promise<void>
|
|
501
|
+
): (arg1: T1, callback: (err: any) => void) => void
|
|
502
|
+
export function toCallback<T1, R>(
|
|
503
|
+
fn: (arg1: T1) => Promise<R>
|
|
504
|
+
): (arg1: T1, callback: (err: any, result: R) => void) => void
|
|
505
|
+
export function toCallback<T1, T2>(
|
|
506
|
+
fn: (arg1: T1, arg2: T2) => Promise<void>
|
|
507
|
+
): (arg1: T1, arg2: T2, callback: (err: any) => void) => void
|
|
508
|
+
export function toCallback<T1, T2, R>(
|
|
509
|
+
fn: (arg1: T1, arg2: T2) => Promise<R>
|
|
510
|
+
): (arg1: T1, arg2: T2, callback: (err: any | null, result: R) => void) => void
|
|
511
|
+
export function toCallback<T1, T2, T3>(
|
|
512
|
+
fn: (arg1: T1, arg2: T2, arg3: T3) => Promise<void>
|
|
513
|
+
): (arg1: T1, arg2: T2, arg3: T3, callback: (err: any) => void) => void
|
|
514
|
+
export function toCallback<T1, T2, T3, R>(
|
|
515
|
+
fn: (arg1: T1, arg2: T2, arg3: T3) => Promise<R>
|
|
516
|
+
): (
|
|
517
|
+
arg1: T1,
|
|
518
|
+
arg2: T2,
|
|
519
|
+
arg3: T3,
|
|
520
|
+
callback: (err: any | null, result: R) => void
|
|
521
|
+
) => void
|
|
522
|
+
export function toCallback<T1, T2, T3, T4>(
|
|
523
|
+
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<void>
|
|
524
|
+
): (
|
|
525
|
+
arg1: T1,
|
|
526
|
+
arg2: T2,
|
|
527
|
+
arg3: T3,
|
|
528
|
+
arg4: T4,
|
|
529
|
+
callback: (err: any) => void
|
|
530
|
+
) => void
|
|
531
|
+
export function toCallback<T1, T2, T3, T4, R>(
|
|
532
|
+
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<R>
|
|
533
|
+
): (
|
|
534
|
+
arg1: T1,
|
|
535
|
+
arg2: T2,
|
|
536
|
+
arg3: T3,
|
|
537
|
+
arg4: T4,
|
|
538
|
+
callback: (err: any | null, result: R) => void
|
|
539
|
+
) => void
|
|
540
|
+
export function toCallback<T1, T2, T3, T4, T5>(
|
|
541
|
+
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise<void>
|
|
542
|
+
): (
|
|
543
|
+
arg1: T1,
|
|
544
|
+
arg2: T2,
|
|
545
|
+
arg3: T3,
|
|
546
|
+
arg4: T4,
|
|
547
|
+
arg5: T5,
|
|
548
|
+
callback: (err: any) => void
|
|
549
|
+
) => void
|
|
550
|
+
export function toCallback<T1, T2, T3, T4, T5, R>(
|
|
551
|
+
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise<R>
|
|
552
|
+
): (
|
|
553
|
+
arg1: T1,
|
|
554
|
+
arg2: T2,
|
|
555
|
+
arg3: T3,
|
|
556
|
+
arg4: T4,
|
|
557
|
+
arg5: T5,
|
|
558
|
+
callback: (err: any | null, result: R) => void
|
|
559
|
+
) => void
|
|
560
|
+
export function toCallback<T1, T2, T3, T4, T5, T6>(
|
|
561
|
+
fn: (
|
|
562
|
+
arg1: T1,
|
|
563
|
+
arg2: T2,
|
|
564
|
+
arg3: T3,
|
|
565
|
+
arg4: T4,
|
|
566
|
+
arg5: T5,
|
|
567
|
+
arg6: T6
|
|
568
|
+
) => Promise<void>
|
|
569
|
+
): (
|
|
570
|
+
arg1: T1,
|
|
571
|
+
arg2: T2,
|
|
572
|
+
arg3: T3,
|
|
573
|
+
arg4: T4,
|
|
574
|
+
arg5: T5,
|
|
575
|
+
arg6: T6,
|
|
576
|
+
callback: (err: any) => void
|
|
577
|
+
) => void
|
|
578
|
+
export function toCallback<T1, T2, T3, T4, T5, T6, R>(
|
|
579
|
+
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => Promise<R>
|
|
580
|
+
): (
|
|
581
|
+
arg1: T1,
|
|
582
|
+
arg2: T2,
|
|
583
|
+
arg3: T3,
|
|
584
|
+
arg4: T4,
|
|
585
|
+
arg5: T5,
|
|
586
|
+
arg6: T6,
|
|
587
|
+
callback: (err: any | null, result: R) => void
|
|
588
|
+
) => void
|
|
589
|
+
|
|
590
|
+
export function toPromiseCallback<T extends any, R extends any>(
|
|
591
|
+
resolve: (value: T) => void,
|
|
592
|
+
reject: (reason: R) => void
|
|
593
|
+
): (err: R, res: T) => void
|
|
594
|
+
|
|
595
|
+
/* -------------------------------- dataPath -------------------------------- */
|
|
596
|
+
|
|
597
|
+
export function getDataPath(
|
|
598
|
+
obj: any,
|
|
599
|
+
path: OrArrayOf<string>,
|
|
600
|
+
handleError?: (obj: any, part: string, index: number) => void
|
|
601
|
+
): any
|
|
602
|
+
|
|
603
|
+
export function normalizeDataPath(path: OrArrayOf<string>): string
|
|
604
|
+
|
|
605
|
+
export function parseDataPath(path: OrArrayOf<string>): string
|
|
606
|
+
|
|
607
|
+
export function setDataPath<O>(obj: O, path: OrArrayOf<string>, value: any): O
|
|
608
|
+
|
|
609
|
+
/* ---------------------------------- html ---------------------------------- */
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* Escapes quotes, ampersands, and smaller/greater than signs (`&<>'"`).
|
|
613
|
+
|
|
614
|
+
* @param html The html to escape.
|
|
615
|
+
* @returns The newly escaped html.
|
|
616
|
+
*/
|
|
617
|
+
export function escapeHtml(html: string): string
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* Strips HTML tags from the string.
|
|
621
|
+
* @param html The string to strip.
|
|
622
|
+
* @returns The newly stripped string.
|
|
623
|
+
*/
|
|
624
|
+
export function stripTags(html: string): string
|
|
625
|
+
|
|
626
|
+
/* -------------------------- typescript utilities -------------------------- */
|
|
627
|
+
type PropertyName = string | number | symbol
|
|
628
|
+
type NotVoid = {} | null | undefined
|
|
629
|
+
type List<T> = ArrayLike<T>
|
|
630
|
+
export interface ArrayLike<T> {
|
|
631
|
+
readonly length: number
|
|
632
|
+
readonly [n: number]: T
|
|
633
|
+
}
|
|
634
|
+
export interface Dictionary<T> {
|
|
635
|
+
[index: string]: T
|
|
636
|
+
}
|
|
637
|
+
export interface RecursiveArray<T> extends Array<T | RecursiveArray<T>> {}
|
|
638
|
+
type OrArrayOf<T> = T | T[]
|