@kogs/utils
Advanced tools
+7
-0
@@ -8,2 +8,8 @@ /// <reference types="node" /> | ||
| /** | ||
| * Generates a new error class with the given name. | ||
| * @param name - Name of the error class. | ||
| * @returns A new error class with the given name. | ||
| */ | ||
| export declare function errorClass(name: string): new () => Error; | ||
| /** | ||
| * Recursively scans the given directory and returns an array of file paths. | ||
@@ -49,2 +55,3 @@ * @param dir - Directory to be scanned. | ||
| collectFiles: typeof collectFiles; | ||
| errorClass: typeof errorClass; | ||
| arrayToStream: typeof arrayToStream; | ||
@@ -51,0 +58,0 @@ streamToArray: typeof streamToArray; |
+19
-0
@@ -5,2 +5,20 @@ import stream from 'node:stream'; | ||
| /** | ||
| * Generates a new error class with the given name. | ||
| * @param name - Name of the error class. | ||
| * @returns A new error class with the given name. | ||
| */ | ||
| export function errorClass(name) { | ||
| return class extends Error { | ||
| /** | ||
| * Creates a new error instance with the given message and options. | ||
| * @param message - Error message. | ||
| * @param options - Additional options. | ||
| */ | ||
| constructor(message, options) { | ||
| super(message, options); | ||
| this.name = name; | ||
| } | ||
| }; | ||
| } | ||
| /** | ||
| * Recursively scans the given directory and returns an array of file paths. | ||
@@ -108,2 +126,3 @@ * @param dir - Directory to be scanned. | ||
| collectFiles, | ||
| errorClass, | ||
| arrayToStream, | ||
@@ -110,0 +129,0 @@ streamToArray, |
+1
-1
| { | ||
| "name": "@kogs/utils", | ||
| "version": "1.2.7", | ||
| "version": "1.3.7", | ||
| "type": "module", | ||
@@ -5,0 +5,0 @@ "description": "A collection of standalone utility functions.", |
+29
-1
| # @kogs/utils | ||
|  [](LICENSE) | ||
| `@kogs/util` is a [Node.js](https://nodejs.org/en/) package that provides a collection of utility functions. | ||
| `@kogs/utils` is a [Node.js](https://nodejs.org/en/) package that provides a collection of utility functions. | ||
@@ -57,2 +57,30 @@ - Provides ["pure functions"](https://en.wikipedia.org/wiki/Pure_function). | ||
| ### errorClass | ||
| `errorClass(name: string): new() => Error` | ||
| This method accepts a name and returns a new error class that can be used to create new errors. | ||
| ```js | ||
| const MyCustomError = errorClass('MyCustomError'); | ||
| throw new MyCustomError('Something went wrong!'); | ||
| // error.name === 'MyCustomError' | ||
| // error.message === 'Something went wrong!' | ||
| ``` | ||
| The purpose of this factory is to reduce the amount of boilerplate required to create custom errors. | ||
| ```js | ||
| // Common boilerplate: | ||
| class MyCustomError extends Error { | ||
| constructor(message) { | ||
| super(message); | ||
| this.name = 'MyCustomError'; | ||
| } | ||
| } | ||
| // Equivalent to: | ||
| const MyCustomError = errorClass('MyCustomError'); | ||
| ``` | ||
| ### arrayToStream | ||
@@ -59,0 +87,0 @@ `arrayToStream(input: Array<ReadableChunk>, objectMode: boolean = true): stream.Readable` |
15914
10.24%192
15.66%191
17.18%