@poppinss/utils 6.5.0-3 → 6.5.0-4

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.
Files changed (72) hide show
  1. package/README.md +2 -0
  2. package/build/index.d.ts +284 -19
  3. package/build/index.js +372 -23
  4. package/build/src/json/main.d.ts +15 -3
  5. package/build/src/json/main.js +5 -6
  6. package/build/src/string/main.d.ts +121 -15
  7. package/build/src/string/main.js +6 -42
  8. package/build/src/string_builder.d.ts +60 -1
  9. package/build/src/string_builder.js +148 -86
  10. package/build/src/types.d.ts +18 -8
  11. package/build/src/types.js +0 -1
  12. package/package.json +22 -7
  13. package/build/src/base64.d.ts +0 -17
  14. package/build/src/base64.js +0 -35
  15. package/build/src/compose.d.ts +0 -14
  16. package/build/src/compose.js +0 -3
  17. package/build/src/define_static_property.d.ts +0 -7
  18. package/build/src/define_static_property.js +0 -21
  19. package/build/src/exception.d.ts +0 -21
  20. package/build/src/exception.js +0 -40
  21. package/build/src/exceptions/invalid_arguments_exception.d.ts +0 -5
  22. package/build/src/exceptions/invalid_arguments_exception.js +0 -5
  23. package/build/src/exceptions/runtime_exception.d.ts +0 -5
  24. package/build/src/exceptions/runtime_exception.js +0 -5
  25. package/build/src/flatten.d.ts +0 -1
  26. package/build/src/flatten.js +0 -4
  27. package/build/src/fs_import_all.d.ts +0 -3
  28. package/build/src/fs_import_all.js +0 -28
  29. package/build/src/fs_read_all.d.ts +0 -3
  30. package/build/src/fs_read_all.js +0 -55
  31. package/build/src/import_default.d.ts +0 -3
  32. package/build/src/import_default.js +0 -15
  33. package/build/src/is_script_file.d.ts +0 -1
  34. package/build/src/is_script_file.js +0 -12
  35. package/build/src/json/safe_parse.d.ts +0 -2
  36. package/build/src/json/safe_parse.js +0 -7
  37. package/build/src/json/safe_stringify.d.ts +0 -2
  38. package/build/src/json/safe_stringify.js +0 -18
  39. package/build/src/message_builder.d.ts +0 -5
  40. package/build/src/message_builder.js +0 -38
  41. package/build/src/natural_sort.d.ts +0 -1
  42. package/build/src/natural_sort.js +0 -3
  43. package/build/src/object_builder.d.ts +0 -18
  44. package/build/src/object_builder.js +0 -32
  45. package/build/src/safe_equal.d.ts +0 -7
  46. package/build/src/safe_equal.js +0 -14
  47. package/build/src/slash.d.ts +0 -1
  48. package/build/src/slash.js +0 -1
  49. package/build/src/string/bytes.d.ts +0 -6
  50. package/build/src/string/bytes.js +0 -12
  51. package/build/src/string/change_case.d.ts +0 -13
  52. package/build/src/string/change_case.js +0 -75
  53. package/build/src/string/excerpt.d.ts +0 -4
  54. package/build/src/string/excerpt.js +0 -10
  55. package/build/src/string/interpolate.d.ts +0 -1
  56. package/build/src/string/interpolate.js +0 -19
  57. package/build/src/string/milliseconds.d.ts +0 -5
  58. package/build/src/string/milliseconds.js +0 -16
  59. package/build/src/string/ordinal.d.ts +0 -1
  60. package/build/src/string/ordinal.js +0 -21
  61. package/build/src/string/pluralize.d.ts +0 -12
  62. package/build/src/string/pluralize.js +0 -12
  63. package/build/src/string/random.d.ts +0 -1
  64. package/build/src/string/random.js +0 -7
  65. package/build/src/string/seconds.d.ts +0 -5
  66. package/build/src/string/seconds.js +0 -16
  67. package/build/src/string/sentence.d.ts +0 -5
  68. package/build/src/string/sentence.js +0 -13
  69. package/build/src/string/slugify.d.ts +0 -2
  70. package/build/src/string/slugify.js +0 -2
  71. package/build/src/string/truncate.d.ts +0 -4
  72. package/build/src/string/truncate.js +0 -10
package/README.md CHANGED
@@ -557,6 +557,7 @@ string.bytes.format(1024 * 1024 * 1000, { thousandsSeparator: ',' }) // 1,000MB
557
557
  ```
558
558
 
559
559
  ### String builder
560
+
560
561
  The string builder offers a fluent API for applying a set of transforms on a string value. You can create an instance of the string builder as follows.
561
562
 
562
563
  ```ts
@@ -938,6 +939,7 @@ await Promise.all(
938
939
  ```
939
940
 
940
941
  #### importDefault
942
+
941
943
  A helper function that assert a lazy import function output to have a `default export`, otherwise raises an exception.
942
944
 
943
945
  We use dynamic default exports a lot in AdonisJS apps, so extracting the check to a helper function.
package/build/index.d.ts CHANGED
@@ -1,19 +1,284 @@
1
- /// <reference types="@types/node" resolution-mode="require"/>
2
- export { base64 } from './src/base64.js';
3
- export { compose } from './src/compose.js';
4
- export { importDefault } from './src/import_default.js';
5
- export { defineStaticProperty } from './src/define_static_property.js';
6
- export { Exception, createError } from './src/exception.js';
7
- export { flatten } from './src/flatten.js';
8
- export { fsImportAll } from './src/fs_import_all.js';
9
- export { fsReadAll } from './src/fs_read_all.js';
10
- export { isScriptFile } from './src/is_script_file.js';
11
- export { MessageBuilder } from './src/message_builder.js';
12
- export { naturalSort } from './src/natural_sort.js';
13
- export { ObjectBuilder } from './src/object_builder.js';
14
- export { safeEqual } from './src/safe_equal.js';
15
- export { slash } from './src/slash.js';
16
- export { RuntimeException } from './src/exceptions/runtime_exception.js';
17
- export { InvalidArgumentsException } from './src/exceptions/invalid_arguments_exception.js';
18
- export declare function getDirname(url: string | URL): string;
19
- export declare function getFilename(url: string | URL): string;
1
+ import { Constructor as Constructor$1, ImportAllFilesOptions, ReadAllFilesOptions, OmitProperties } from './src/types.js';
2
+ export { default as slash } from 'slash';
3
+
4
+ /**
5
+ * Helper class to base64 encode/decode values with option
6
+ * for url encoding and decoding
7
+ */
8
+ declare class Base64 {
9
+ /**
10
+ * Base64 encode Buffer or string
11
+ */
12
+ encode(arrayBuffer: ArrayBuffer | SharedArrayBuffer): string;
13
+ encode(data: string, encoding?: BufferEncoding): string;
14
+ /**
15
+ * Base64 decode a previously encoded string or Buffer.
16
+ */
17
+ decode(encode: string, encoding: BufferEncoding, strict: true): string;
18
+ decode(encode: string, encoding: undefined, strict: true): string;
19
+ decode(encode: string, encoding?: BufferEncoding, strict?: false): string | null;
20
+ decode(encode: Buffer, encoding?: BufferEncoding): string;
21
+ /**
22
+ * Base64 encode Buffer or string to be URL safe. (RFC 4648)
23
+ */
24
+ urlEncode(arrayBuffer: ArrayBuffer | SharedArrayBuffer): string;
25
+ urlEncode(data: string, encoding?: BufferEncoding): string;
26
+ /**
27
+ * Base64 URL decode a previously encoded string or Buffer. (RFC 4648)
28
+ */
29
+ urlDecode(encode: string, encoding: BufferEncoding, strict: true): string;
30
+ urlDecode(encode: string, encoding: undefined, strict: true): string;
31
+ urlDecode(encode: string, encoding?: BufferEncoding, strict?: false): string | null;
32
+ urlDecode(encode: Buffer, encoding?: BufferEncoding): string;
33
+ }
34
+ declare const base64: Base64;
35
+
36
+ interface UnaryFunction<T, R> {
37
+ (source: T): R;
38
+ }
39
+ /**
40
+ * Compose a class by applying mixins to it.
41
+ * The code is inspired by https://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/, its
42
+ * just that I have added the support for static types too.
43
+ */
44
+ declare function compose<T extends Constructor$1, A>(superclass: T, mixin: UnaryFunction<T, A>): A;
45
+ declare function compose<T extends Constructor$1, A, B>(superclass: T, mixin: UnaryFunction<T, A>, mixinB: UnaryFunction<A, B>): B;
46
+ declare function compose<T extends Constructor$1, A, B, C>(superclass: T, mixin: UnaryFunction<T, A>, mixinB: UnaryFunction<A, B>, mixinC: UnaryFunction<B, C>): C;
47
+ declare function compose<T extends Constructor$1, A, B, C, D>(superclass: T, mixin: UnaryFunction<T, A>, mixinB: UnaryFunction<A, B>, mixinC: UnaryFunction<B, C>, mixinD: UnaryFunction<C, D>): D;
48
+ declare function compose<T extends Constructor$1, A, B, C, D, E>(superclass: T, mixin: UnaryFunction<T, A>, mixinB: UnaryFunction<A, B>, mixinC: UnaryFunction<B, C>, mixinD: UnaryFunction<C, D>, mixinE: UnaryFunction<D, E>): E;
49
+ declare function compose<T extends Constructor$1, A, B, C, D, E, F>(superclass: T, mixin: UnaryFunction<T, A>, mixinB: UnaryFunction<A, B>, mixinC: UnaryFunction<B, C>, mixinD: UnaryFunction<C, D>, mixinF: UnaryFunction<E, F>): F;
50
+ declare function compose<T extends Constructor$1, A, B, C, D, E, F, G>(superclass: T, mixin: UnaryFunction<T, A>, mixinB: UnaryFunction<A, B>, mixinC: UnaryFunction<B, C>, mixinD: UnaryFunction<C, D>, mixinF: UnaryFunction<E, F>, mixinG: UnaryFunction<F, G>): G;
51
+ declare function compose<T extends Constructor$1, A, B, C, D, E, F, G, H>(superclass: T, mixin: UnaryFunction<T, A>, mixinB: UnaryFunction<A, B>, mixinC: UnaryFunction<B, C>, mixinD: UnaryFunction<C, D>, mixinF: UnaryFunction<E, F>, mixinG: UnaryFunction<F, G>, mixinH: UnaryFunction<G, H>): H;
52
+ declare function compose<T extends Constructor$1, A, B, C, D, E, F, G, H, I>(superclass: T, mixin: UnaryFunction<T, A>, mixinB: UnaryFunction<A, B>, mixinC: UnaryFunction<B, C>, mixinD: UnaryFunction<C, D>, mixinF: UnaryFunction<E, F>, mixinG: UnaryFunction<F, G>, mixinH: UnaryFunction<G, H>, mixinI: UnaryFunction<H, I>): I;
53
+
54
+ /**
55
+ * Dynamically import a module and ensure it has a default export
56
+ */
57
+ declare function importDefault<T extends object>(importFn: () => Promise<T>, filePath?: string): Promise<T extends {
58
+ default: infer A;
59
+ } ? A : never>;
60
+
61
+ type Constructor = new (...args: any[]) => any;
62
+ type AbstractConstructor = abstract new (...args: any[]) => any;
63
+ /**
64
+ * Define static properties on a class with inheritance in play.
65
+ */
66
+ declare function defineStaticProperty<T extends Constructor | AbstractConstructor, Prop extends keyof T>(self: T, propertyName: Prop, { initialValue, strategy, }: {
67
+ initialValue: T[Prop];
68
+ strategy: 'inherit' | 'define' | ((value: T[Prop]) => T[Prop]);
69
+ }): void;
70
+
71
+ /**
72
+ * Extended Error object with the option to set error `status` and `code`.
73
+ * At AdonisJs, we prefer exceptions with proper error codes to handle
74
+ * them without relying on message pattern matching.
75
+ *
76
+ * ```js
77
+ * new Exception('message', 500, 'E_RUNTIME_EXCEPTION')
78
+ * ```
79
+ */
80
+ declare class Exception extends Error {
81
+ /**
82
+ * Static properties to defined on the exception once
83
+ * and then re-use them
84
+ */
85
+ static help?: string;
86
+ static code?: string;
87
+ static status?: number;
88
+ static message?: string;
89
+ /**
90
+ * Name of the class that raised the exception.
91
+ */
92
+ name: string;
93
+ /**
94
+ * Optional help description for the error. You can use it to define additional
95
+ * human readable information for the error.
96
+ */
97
+ help?: string;
98
+ /**
99
+ * A machine readable error code. This will allow the error handling logic
100
+ * to narrow down exceptions based upon the error code.
101
+ */
102
+ code?: string;
103
+ /**
104
+ * A status code for the error. Usually helpful when converting errors
105
+ * to HTTP responses.
106
+ */
107
+ status: number;
108
+ constructor(message?: string, options?: ErrorOptions & {
109
+ code?: string;
110
+ status?: number;
111
+ });
112
+ get [Symbol.toStringTag](): string;
113
+ toString(): string;
114
+ }
115
+ /**
116
+ * Helper to create anonymous error classes
117
+ */
118
+ declare function createError<T extends any[] = never>(message: string, code: string, status?: number): typeof Exception & T extends never ? {
119
+ new (args?: any, options?: ErrorOptions): Exception;
120
+ } : {
121
+ new (args: T, options?: ErrorOptions): Exception;
122
+ };
123
+
124
+ /**
125
+ * Recursively flatten an object/array.
126
+ */
127
+ declare function flatten<X = Record<string, any>, Y = unknown>(input: Y, glue?: string, keepNullish?: boolean): X;
128
+
129
+ /**
130
+ * Returns an array of file paths from the given location. You can
131
+ * optionally filter and sort files by passing relevant options
132
+ *
133
+ * ```ts
134
+ * await fsReadAll(new URL('./', import.meta.url))
135
+ *
136
+ * await fsReadAll(new URL('./', import.meta.url), {
137
+ * filter: (filePath) => filePath.endsWith('.js')
138
+ * })
139
+
140
+ * await fsReadAll(new URL('./', import.meta.url), {
141
+ * absolute: true,
142
+ * unixPaths: true
143
+ * })
144
+ * ```
145
+ */
146
+ declare function fsImportAll(location: string | URL, options?: ImportAllFilesOptions): Promise<any>;
147
+
148
+ /**
149
+ * Returns an array of file paths from the given location. You can
150
+ * optionally filter and sort files by passing relevant options
151
+ *
152
+ * ```ts
153
+ * await fsReadAll(new URL('./', import.meta.url))
154
+ *
155
+ * await fsReadAll(new URL('./', import.meta.url), {
156
+ * filter: (filePath) => filePath.endsWith('.js')
157
+ * })
158
+
159
+ * await fsReadAll(new URL('./', import.meta.url), {
160
+ * absolute: true,
161
+ * unixPaths: true
162
+ * })
163
+ * ```
164
+ */
165
+ declare function fsReadAll(location: string | URL, options?: ReadAllFilesOptions): Promise<string[]>;
166
+
167
+ /**
168
+ * Returns `true` when file ends with `.js`, `.json` or
169
+ * `.ts` but not `.d.ts`.
170
+ */
171
+ declare function isScriptFile(filePath: string): boolean;
172
+
173
+ /**
174
+ * Message builder exposes an API to "JSON.stringify" values by
175
+ * encoding purpose and expiry date inside them.
176
+ *
177
+ * The return value must be further encrypted to prevent tempering.
178
+ */
179
+ declare class MessageBuilder {
180
+ #private;
181
+ /**
182
+ * Builds a message by encoding expiry date and purpose inside it.
183
+ */
184
+ build(message: any, expiresIn?: string | number, purpose?: string): string;
185
+ /**
186
+ * Verifies the message for expiry and purpose.
187
+ */
188
+ verify<T extends any>(message: any, purpose?: string): null | T;
189
+ }
190
+
191
+ /**
192
+ * Perform natural sorting with "Array.sort()" method
193
+ */
194
+ declare function naturalSort(current: string, next: string): number;
195
+
196
+ /**
197
+ * A simple class to build an object incrementally. It is helpful when you
198
+ * want to add properties to the object conditionally.
199
+ *
200
+ * Instead of writing
201
+ * ```
202
+ * const obj = {
203
+ * ...(user.id ? { id: user.id } : {}),
204
+ * ...(user.firstName && user.lastName ? { name: `${user.firstName} ${user.lastName}` } : {}),
205
+ * }
206
+ * ```
207
+ *
208
+ * You can write
209
+ *
210
+ * const obj = new ObjectBuilder()
211
+ * .add('id', user.id)
212
+ * .add(
213
+ * 'fullName',
214
+ * user.firstName && user.lastName ? `${user.firstName} ${user.lastName}` : undefined
215
+ * )
216
+ * .toObject()
217
+ */
218
+ declare class ObjectBuilder<ReturnType extends Record<string, any>, IgnoreNull extends boolean = false> {
219
+ #private;
220
+ values: ReturnType;
221
+ constructor(initialValue: ReturnType, ignoreNull?: IgnoreNull);
222
+ /**
223
+ * Add a key-value pair to the object
224
+ *
225
+ * - Undefined values are ignored
226
+ * - Null values are ignored, when `ignoreNull` is set to true
227
+ */
228
+ add<Prop extends string>(key: Prop, value: undefined): this;
229
+ add<Prop extends string, Value>(key: Prop, value: Value): ObjectBuilder<ReturnType & {
230
+ [P in Prop]: Value;
231
+ }, IgnoreNull>;
232
+ /**
233
+ * Remove key from the object
234
+ */
235
+ remove<K extends keyof ReturnType>(key: K): this;
236
+ /**
237
+ * Find if a value exists
238
+ */
239
+ has<K extends keyof ReturnType>(key: K): boolean;
240
+ /**
241
+ * Get the existing value for a given key
242
+ */
243
+ get<K extends keyof ReturnType>(key: K): ReturnType[K];
244
+ /**
245
+ * Get the underlying constructed object
246
+ */
247
+ toObject(): IgnoreNull extends true ? {
248
+ [K in keyof OmitProperties<ReturnType, null>]: ReturnType[K];
249
+ } : {
250
+ [K in keyof ReturnType]: ReturnType[K];
251
+ };
252
+ }
253
+
254
+ type BufferSafeValue = ArrayBuffer | SharedArrayBuffer | number[] | string | {
255
+ valueOf(): string | object;
256
+ } | {
257
+ [Symbol.toPrimitive](hint: 'string'): string;
258
+ };
259
+ /**
260
+ * Compare two values to see if they are equal. The comparison is done in
261
+ * a way to avoid timing-attacks.
262
+ */
263
+ declare function safeEqual<T extends BufferSafeValue, U extends BufferSafeValue>(trustedValue: T, userInput: U): boolean;
264
+
265
+ declare class RuntimeException extends Exception {
266
+ static code: string;
267
+ static status: number;
268
+ }
269
+
270
+ declare class InvalidArgumentsException extends Exception {
271
+ static code: string;
272
+ static status: number;
273
+ }
274
+
275
+ /**
276
+ * Get dirname for a given file path URL
277
+ */
278
+ declare function getDirname(url: string | URL): string;
279
+ /**
280
+ * Get filename for a given file path URL
281
+ */
282
+ declare function getFilename(url: string | URL): string;
283
+
284
+ export { Exception, InvalidArgumentsException, MessageBuilder, ObjectBuilder, RuntimeException, base64, compose, createError, defineStaticProperty, flatten, fsImportAll, fsReadAll, getDirname, getFilename, importDefault, isScriptFile, naturalSort, safeEqual };