@ditojs/utils 1.12.0 → 1.13.1

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.12.0",
3
+ "version": "1.13.1",
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
- "gitHead": "3707045b72d84db4b5174d2efeb8cf62e2065ffa"
34
+ "devDependencies": {
35
+ "typescript": "^4.8.4"
36
+ },
37
+ "types": "types",
38
+ "gitHead": "0c6a508e04ca915691c1fb0f3706827c8ecfe0ab"
31
39
  }
@@ -78,6 +78,5 @@ exports[`index > exports all symbols 1`] = `
78
78
  "toCallback": [Function],
79
79
  "toPromiseCallback": [Function],
80
80
  "underscore": [Function],
81
- Symbol(Symbol.toStringTag): "Module",
82
81
  }
83
82
  `;
@@ -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, 10:45:30 PM')
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, 22:45:30')
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, 11:17:36')
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, 05:54:14 PM')
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, 10:45:30 PM')
12
- expect(formatDate(date, {})).toBe('June 9, 2012, 10:45:30 PM')
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' })).toBe('9. Juni 2012, 22:45:30')
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, 01:00:00')
22
+ .toBe('1. Januar 1970 um 01:00:00')
22
23
  expect(formatDate(1000000000000, { locale: 'de-DE' }))
23
- .toBe('9. September 2001, 03:46:40')
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, 01:12:00')
26
+ .toBe('4. Dezember 1995 um 01:12:00')
26
27
  })
27
28
  })
@@ -0,0 +1,656 @@
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>(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>(
105
+ o: T
106
+ ): T extends Function ? T : () => T
107
+
108
+ /* --------------------------------- object --------------------------------- */
109
+
110
+ /**
111
+ * Performs a deep (recursive) clone on the supplied value.
112
+ */
113
+ export function clone<T>(arg: T, iteratee?: (arg: any) => void): T
114
+
115
+ /**
116
+ * Determines whether the supplied values can be considered equal through
117
+ * recursive comparison of object properties and array elements. Non-objects
118
+ * and non-arrays are compared using the SameValue algorithm.
119
+ */
120
+ export function equals(arg1: any, arg2: any): boolean
121
+
122
+ // TODO: document groupBy
123
+ export function groupBy<T, K extends string | number | symbol>(
124
+ list: T[],
125
+ callback: (item: T) => K
126
+ ): Record<K, T[]>
127
+
128
+ // TODO: document merge
129
+ export function merge<ArgA, ArgB>(a: ArgA, b: ArgB): ArgA & ArgB
130
+ export function merge<ArgA, ArgB, ArgC>(
131
+ a: ArgA,
132
+ b: ArgB,
133
+ c: ArgC
134
+ ): ArgA & ArgB & ArgC
135
+ export function merge<ArgA, ArgB, ArgC, ArgD>(
136
+ a: ArgA,
137
+ b: ArgB,
138
+ c: ArgC,
139
+ d: ArgD
140
+ ): ArgA & ArgB & ArgC & ArgD
141
+ export function merge<ArgA, ArgB, ArgC, ArgD, ArgE>(
142
+ a: ArgA,
143
+ b: ArgB,
144
+ c: ArgC,
145
+ d: ArgD,
146
+ e: ArgE
147
+ ): ArgA & ArgB & ArgC & ArgD & ArgE
148
+ export function merge(...args: any[]): any
149
+
150
+ /**
151
+ * Returns the first argument which is not undefined.
152
+ */
153
+ export function pick<ArgA, ArgB>(a: ArgA, b: ArgB): ArgA | ArgB
154
+ export function pick<ArgA, ArgB, ArgC>(
155
+ a: ArgA,
156
+ b: ArgB,
157
+ c: ArgC
158
+ ): ArgA | ArgB | ArgC
159
+ export function pick<ArgA, ArgB, ArgC, ArgD>(
160
+ a: ArgA,
161
+ b: ArgB,
162
+ c: ArgC,
163
+ d: ArgD
164
+ ): ArgA | ArgB | ArgC | ArgD
165
+ export function pick<ArgA, ArgB, ArgC, ArgD, ArgE>(
166
+ a: ArgA,
167
+ b: ArgB,
168
+ c: ArgC,
169
+ d: ArgD,
170
+ e: ArgE
171
+ ): ArgA | ArgB | ArgC | ArgD | ArgE
172
+ export function pick(...args: any[]): any
173
+ /**
174
+ * Creates an object composed of the object properties predicate returns
175
+ * truthy for.
176
+ * @param object The source object.
177
+ * @param callback Callback invoked with three arguments: (value, key, item).
178
+ */
179
+ export function pickBy<T extends Dictionary<any>>(
180
+ object: T,
181
+ callback?: (value: T[keyof T], key: keyof T, object: T) => any
182
+ ): Partial<T>
183
+
184
+ export function mapKeys<T extends Dictionary<any>, K extends keyof any>(
185
+ object: T,
186
+ callback?: (key: keyof T, value: T[keyof T], object: T) => K
187
+ ): Record<K, T[keyof T]>
188
+
189
+ export function mapValues<T extends Dictionary<any>, K>(
190
+ object: T,
191
+ callback?: (value: T[keyof T], key: keyof T, object: T) => K
192
+ ): Record<keyof T, K>
193
+
194
+ /* --------------------------------- string --------------------------------- */
195
+
196
+ /**
197
+ * Converts a string seperated by spaces, dashes and underscores to camel-case
198
+ * (`'camelCase'`) or optionally pascal-case (`'CamelCase'`).
199
+ */
200
+ export function camelize(str: string, pascalCase?: boolean): string
201
+ /**
202
+ * Capitalizes words in a string.
203
+ */
204
+ export function capitalize(str: string): string
205
+ /**
206
+ * Convert a string from camel-case to a string seperated by spaces or a
207
+ * supplied seperator string.
208
+ *
209
+ * @param str The string to decamelize.
210
+ * @param {string} [sep=' '] - The string to seperate the decamelized words
211
+ * with.
212
+ */
213
+ export function decamelize(str: string, sep?: string): string
214
+ /**
215
+ * Converts a camelized string to a string seperated by dashes.
216
+ */
217
+ export function hyphenate(str: string): string
218
+ /**
219
+ * Converts a camelized string to a string seperated by underscores.
220
+ */
221
+ export function underscore(str: string): string
222
+ /**
223
+ * ES6 string tag that strips indentation from multi-line strings.
224
+ *
225
+ * @example
226
+ * const str = deindent`Such a long string that you need to break it
227
+ * over multiple lines. Such a long string that
228
+ * you need to break it over multiple lines.`
229
+ * console.log(str)
230
+ */
231
+ export function deindent(
232
+ strings: OrArrayOf<string>,
233
+ ...values: Array<string>
234
+ ): string
235
+ /**
236
+ * Returns the longest prefix string that is common to both supplied strings.
237
+ */
238
+ export function getCommonPrefix(str1: string, str2: string): string
239
+ /**
240
+ * Returns the offset of the longest prefix string that is common to both
241
+ * supplied strings.
242
+ */
243
+ export function getCommonOffset(str1: string, str2: string): number
244
+ /**
245
+ * Determines whether the supplied string is an absolute url.
246
+ *
247
+ * A URL is considered absolute if it begins with "<scheme>://" or "//"
248
+ * (protocol-relative URL). RFC 3986 defines scheme name as a sequence of
249
+ * characters beginning with a letter and followed by any combination of
250
+ * letters, digits, plus, period, or hyphen.
251
+ */
252
+ export function isAbsoluteUrl(str: string): boolean
253
+ /**
254
+ * Determines whether the supplied string is a valid creditcard number.
255
+ */
256
+ export function isCreditCard(str: string): boolean
257
+ /**
258
+ * Determines whether the supplied string is a valid email address.
259
+ */
260
+ export function isEmail(str: string): boolean
261
+ /**
262
+ * Determines whether the string is a valid hostname.
263
+ */
264
+ export function isHostname(str: string): boolean
265
+ /**
266
+ * Determines whether the string is a valid url.
267
+ */
268
+ export function isUrl(str: string): boolean
269
+ /**
270
+ * Expands hyphenated, underscored and camel-cased strings to title case.
271
+ */
272
+ export function labelize(str: string | undefined): string
273
+
274
+ /* ---------------------------------- array --------------------------------- */
275
+
276
+ /**
277
+ * Recursively flattens a nested array.
278
+ *
279
+ * @param array The array to recursively flatten.
280
+ * @param [maxDepth] The maximum recursion depth.
281
+ * @return Returns the new flattened array.
282
+ */
283
+ export function flatten<T>(array: RecursiveArray<T>, maxDepth?: number): T[]
284
+
285
+ /**
286
+ * Shuffle an array using the Fisher-Yates (aka Knuth) Shuffle.
287
+ *
288
+ * @param array The array to shuffle.
289
+ * @returns Returns the newly shuffled array.
290
+ */
291
+ export function shuffle<T>(array: T[]): T[]
292
+
293
+ /* ---------------------------------- date ---------------------------------- */
294
+
295
+ export interface TimeFormat {
296
+ /**
297
+ * @defaultValue `'2-digit'`
298
+ */
299
+ hour?: boolean | 'numeric' | '2-digit'
300
+ /**
301
+ * @defaultValue `'2-digit'`
302
+ */
303
+ minute?: boolean | 'numeric' | '2-digit'
304
+ /**
305
+ * @defaultValue `'2-digit'`
306
+ */
307
+ second?: boolean | 'numeric' | '2-digit'
308
+
309
+ format?: (
310
+ value: string,
311
+ type: Intl.DateTimeFormatPartTypes,
312
+ options: Omit<TimeFormat, 'format'>
313
+ ) => string
314
+ }
315
+
316
+ export interface DateFormat {
317
+ /**
318
+ * @defaultValue `'numeric'`
319
+ */
320
+ day?: boolean | 'numeric' | '2-digit'
321
+ /**
322
+ * @defaultValue `'long'`
323
+ */
324
+ month?: boolean | 'numeric' | '2-digit' | 'long' | 'short' | 'narrow'
325
+ /**
326
+ * @defaultValue `'numeric'`
327
+ */
328
+ year?: boolean | 'numeric' | '2-digit'
329
+ /**
330
+ * Format date in a custom way.
331
+ */
332
+ format?: (
333
+ value: string,
334
+ type: Intl.DateTimeFormatPartTypes,
335
+ options: Omit<DateFormat, 'format'>
336
+ ) => string
337
+ }
338
+
339
+ export interface NumberFormat extends Intl.NumberFormatOptions {
340
+ format?: (
341
+ value: string,
342
+ type: Intl.NumberFormatPartTypes,
343
+ options: Intl.NumberFormatOptions
344
+ ) => string | undefined
345
+ }
346
+
347
+ /**
348
+ * Formats the value as a string.
349
+ */
350
+ export function format(
351
+ value: Date,
352
+ options?: {
353
+ /**
354
+ * @default 'en-US'
355
+ */
356
+ locale?: string
357
+ date?: boolean | DateFormat
358
+ time?: boolean | TimeFormat
359
+ number?: boolean | NumberFormat
360
+ }
361
+ ): string
362
+ /* -------------------------------- function -------------------------------- */
363
+
364
+ /**
365
+ * Creates a debounced function that delays invoking func until after wait
366
+ * milliseconds have elapsed since the last time the debounced function was
367
+ * invoked. The debounced function comes with a cancel method to cancel
368
+ * delayed invocations. Provide an options object to indicate that func
369
+ * should be invoked on the leading edge of the wait timeout. Subsequent
370
+ * calls to the debounced function return the result of the last func
371
+ * invocation.
372
+ *
373
+ * @param func The function to debounce.
374
+ * @param options The options object or the number of milliseconds to delay.
375
+ * @param options.delay The number of milliseconds to delay.
376
+ * @param options.immediate Specify invoking on the leading edge of the timeout.
377
+ * @return Returns the new debounced function.
378
+ */
379
+ export function debounce<T extends (...args: any[]) => any>(
380
+ func: T,
381
+ options:
382
+ | number
383
+ | {
384
+ delay: number
385
+ immediate?: boolean
386
+ }
387
+ ): T & { cancel: () => boolean }
388
+
389
+ /**
390
+ * Creates a debounced function that delays invoking func until after wait
391
+ * milliseconds have elapsed since the last time the debounced function was
392
+ * invoked.
393
+ *
394
+ * Each time the debounced function is called, it returns a promise
395
+ * which will resolve to the value that the debounced async function resolves
396
+ * to. If the async function throws an error, the last waiting promise is
397
+ * rejected and all others are resolved with undefined.
398
+ *
399
+ * The debounced function comes with a cancel method to cancel
400
+ * delayed invocations. Provide an options object to indicate that func
401
+ * should be invoked on the leading edge of the wait timeout.
402
+ *
403
+ * @param func The function to debounce.
404
+ * @param options The options object or the number of milliseconds to delay.
405
+ * @param options.delay The number of milliseconds to delay.
406
+ * @param options.immediate Specify invoking on the leading edge of the timeout.
407
+ * @return Returns the new debounced function.
408
+ */
409
+ export function debounceAsync<T extends (...args: any[]) => Promise<any>>(
410
+ func: T,
411
+ options:
412
+ | number
413
+ | {
414
+ delay: number
415
+ immediate?: boolean
416
+ }
417
+ ): T & { cancel: () => boolean }
418
+
419
+ // Adjusted version of @types/node's promisify (which includes support for
420
+ // overloaded node function types), excluding the custom promisified function
421
+ // functionality, which toAsync does not support:
422
+ // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/fa723dee727e433add7e700420523f0f90e87a01/types/node/util.d.ts#L86
423
+ interface CustomPromisifyLegacy<TCustom extends Function>
424
+ extends Function {
425
+ __promisify__: TCustom
426
+ }
427
+ type CustomToAsync<TCustom extends Function> = CustomPromisifyLegacy<TCustom>
428
+ /**
429
+ * Takes a function following the common error-first callback style, i.e.
430
+ * taking an (err, value) => ... callback as the last argument, and returns a
431
+ * version that returns promises.
432
+ *
433
+ * @param fn function following the error-first callback style
434
+ */
435
+ export function toAsync<TCustom extends Function>(
436
+ fn: CustomToAsync<TCustom>
437
+ ): TCustom
438
+ export function toAsync<R>(
439
+ fn: (callback: (err: any, result: R) => void) => void
440
+ ): () => Promise<R>
441
+ export function toAsync(
442
+ fn: (callback: (err?: any) => void) => void
443
+ ): () => Promise<void>
444
+ export function toAsync<T1, R>(
445
+ fn: (arg1: T1, callback: (err: any, result: R) => void) => void
446
+ ): (arg1: T1) => Promise<R>
447
+ export function toAsync<T1>(
448
+ fn: (arg1: T1, callback: (err?: any) => void) => void
449
+ ): (arg1: T1) => Promise<void>
450
+ export function toAsync<T1, T2, R>(
451
+ fn: (arg1: T1, arg2: T2, callback: (err: any, result: R) => void) => void
452
+ ): (arg1: T1, arg2: T2) => Promise<R>
453
+ export function toAsync<T1, T2>(
454
+ fn: (arg1: T1, arg2: T2, callback: (err?: any) => void) => void
455
+ ): (arg1: T1, arg2: T2) => Promise<void>
456
+ export function toAsync<T1, T2, T3, R>(
457
+ fn: (
458
+ arg1: T1,
459
+ arg2: T2,
460
+ arg3: T3,
461
+ callback: (err: any, result: R) => void
462
+ ) => void
463
+ ): (arg1: T1, arg2: T2, arg3: T3) => Promise<R>
464
+ export function toAsync<T1, T2, T3>(
465
+ fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err?: any) => void) => void
466
+ ): (arg1: T1, arg2: T2, arg3: T3) => Promise<void>
467
+ export function toAsync<T1, T2, T3, T4, R>(
468
+ fn: (
469
+ arg1: T1,
470
+ arg2: T2,
471
+ arg3: T3,
472
+ arg4: T4,
473
+ callback: (err: any, result: R) => void
474
+ ) => void
475
+ ): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<R>
476
+ export function toAsync<T1, T2, T3, T4>(
477
+ fn: (
478
+ arg1: T1,
479
+ arg2: T2,
480
+ arg3: T3,
481
+ arg4: T4,
482
+ callback: (err?: any) => void
483
+ ) => void
484
+ ): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<void>
485
+ export function toAsync<T1, T2, T3, T4, T5, R>(
486
+ fn: (
487
+ arg1: T1,
488
+ arg2: T2,
489
+ arg3: T3,
490
+ arg4: T4,
491
+ arg5: T5,
492
+ callback: (err: any, result: R) => void
493
+ ) => void
494
+ ): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise<R>
495
+ export function toAsync<T1, T2, T3, T4, T5>(
496
+ fn: (
497
+ arg1: T1,
498
+ arg2: T2,
499
+ arg3: T3,
500
+ arg4: T4,
501
+ arg5: T5,
502
+ callback: (err?: any) => void
503
+ ) => void
504
+ ): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise<void>
505
+
506
+ /**
507
+ * Takes an async function (or a function that returns a Promise) and returns
508
+ * a function following the error-first callback style, i.e. taking an
509
+ * (err, value) => ... callback as the last argument.
510
+ */
511
+ export function toCallback(
512
+ fn: () => Promise<void>
513
+ ): (callback: (err: any) => void) => void
514
+ export function toCallback<R>(
515
+ fn: () => Promise<R>
516
+ ): (callback: (err: any, result: R) => void) => void
517
+ export function toCallback<T1>(
518
+ fn: (arg1: T1) => Promise<void>
519
+ ): (arg1: T1, callback: (err: any) => void) => void
520
+ export function toCallback<T1, R>(
521
+ fn: (arg1: T1) => Promise<R>
522
+ ): (arg1: T1, callback: (err: any, result: R) => void) => void
523
+ export function toCallback<T1, T2>(
524
+ fn: (arg1: T1, arg2: T2) => Promise<void>
525
+ ): (arg1: T1, arg2: T2, callback: (err: any) => void) => void
526
+ export function toCallback<T1, T2, R>(
527
+ fn: (arg1: T1, arg2: T2) => Promise<R>
528
+ ): (arg1: T1, arg2: T2, callback: (err: any | null, result: R) => void) => void
529
+ export function toCallback<T1, T2, T3>(
530
+ fn: (arg1: T1, arg2: T2, arg3: T3) => Promise<void>
531
+ ): (arg1: T1, arg2: T2, arg3: T3, callback: (err: any) => void) => void
532
+ export function toCallback<T1, T2, T3, R>(
533
+ fn: (arg1: T1, arg2: T2, arg3: T3) => Promise<R>
534
+ ): (
535
+ arg1: T1,
536
+ arg2: T2,
537
+ arg3: T3,
538
+ callback: (err: any | null, result: R) => void
539
+ ) => void
540
+ export function toCallback<T1, T2, T3, T4>(
541
+ fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<void>
542
+ ): (
543
+ arg1: T1,
544
+ arg2: T2,
545
+ arg3: T3,
546
+ arg4: T4,
547
+ callback: (err: any) => void
548
+ ) => void
549
+ export function toCallback<T1, T2, T3, T4, R>(
550
+ fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<R>
551
+ ): (
552
+ arg1: T1,
553
+ arg2: T2,
554
+ arg3: T3,
555
+ arg4: T4,
556
+ callback: (err: any | null, result: R) => void
557
+ ) => void
558
+ export function toCallback<T1, T2, T3, T4, T5>(
559
+ fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise<void>
560
+ ): (
561
+ arg1: T1,
562
+ arg2: T2,
563
+ arg3: T3,
564
+ arg4: T4,
565
+ arg5: T5,
566
+ callback: (err: any) => void
567
+ ) => void
568
+ export function toCallback<T1, T2, T3, T4, T5, R>(
569
+ fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise<R>
570
+ ): (
571
+ arg1: T1,
572
+ arg2: T2,
573
+ arg3: T3,
574
+ arg4: T4,
575
+ arg5: T5,
576
+ callback: (err: any | null, result: R) => void
577
+ ) => void
578
+ export function toCallback<T1, T2, T3, T4, T5, T6>(
579
+ fn: (
580
+ arg1: T1,
581
+ arg2: T2,
582
+ arg3: T3,
583
+ arg4: T4,
584
+ arg5: T5,
585
+ arg6: T6
586
+ ) => Promise<void>
587
+ ): (
588
+ arg1: T1,
589
+ arg2: T2,
590
+ arg3: T3,
591
+ arg4: T4,
592
+ arg5: T5,
593
+ arg6: T6,
594
+ callback: (err: any) => void
595
+ ) => void
596
+ export function toCallback<T1, T2, T3, T4, T5, T6, R>(
597
+ fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => Promise<R>
598
+ ): (
599
+ arg1: T1,
600
+ arg2: T2,
601
+ arg3: T3,
602
+ arg4: T4,
603
+ arg5: T5,
604
+ arg6: T6,
605
+ callback: (err: any | null, result: R) => void
606
+ ) => void
607
+
608
+ export function toPromiseCallback<T, R>(
609
+ resolve: (value: T) => void,
610
+ reject: (reason: R) => void
611
+ ): (err: R, res: T) => void
612
+
613
+ /* -------------------------------- dataPath -------------------------------- */
614
+
615
+ export function getDataPath(
616
+ obj: any,
617
+ path: OrArrayOf<string>,
618
+ handleError?: (obj: any, part: string, index: number) => void
619
+ ): any
620
+
621
+ export function normalizeDataPath(path: OrArrayOf<string>): string
622
+
623
+ export function parseDataPath(path: OrArrayOf<string>): string
624
+
625
+ export function setDataPath<O>(obj: O, path: OrArrayOf<string>, value: any): O
626
+
627
+ /* ---------------------------------- html ---------------------------------- */
628
+
629
+ /**
630
+ * Escapes quotes, ampersands, and smaller/greater than signs (`&<>'"`).
631
+
632
+ * @param html The html to escape.
633
+ * @returns The newly escaped html.
634
+ */
635
+ export function escapeHtml(html: string): string
636
+
637
+ /**
638
+ * Strips HTML tags from the string.
639
+ * @param html The string to strip.
640
+ * @returns The newly stripped string.
641
+ */
642
+ export function stripTags(html: string): string
643
+
644
+ /* -------------------------- typescript utilities -------------------------- */
645
+ type PropertyName = string | number | symbol
646
+ type NotVoid = {} | null | undefined
647
+ type List<T> = ArrayLike<T>
648
+ export interface ArrayLike<T> {
649
+ readonly length: number
650
+ readonly [n: number]: T
651
+ }
652
+ export interface Dictionary<T> {
653
+ [index: string]: T
654
+ }
655
+ export type RecursiveArray<T> = Array<T | RecursiveArray<T>>
656
+ type OrArrayOf<T> = T | T[]