get-or-throw 1.0.0 → 1.1.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/.prettierrc.json +5 -0
- package/README.md +6 -1
- package/dist/index.cjs +5 -3
- package/dist/index.d.cts +6 -3
- package/dist/index.d.ts +6 -3
- package/dist/index.js +5 -3
- package/package.json +7 -2
- package/src/get-or-throw.ts +18 -6
- package/tsconfig.json +1 -2
package/.prettierrc.json
ADDED
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# get-or-throw
|
|
2
2
|
|
|
3
|
-
A convenience function for adhering to Typescript's `noUncheckedIndexedAccess`
|
|
3
|
+
A convenience function for adhering to Typescript's `noUncheckedIndexedAccess`
|
|
4
|
+
setting.
|
|
4
5
|
|
|
5
6
|
## Installation
|
|
6
7
|
|
|
@@ -24,4 +25,8 @@ console.log(getOrThrow(obj, "b")); // Output: 2
|
|
|
24
25
|
|
|
25
26
|
/** This will throw an error: "Key "d" does not exist in the object." */
|
|
26
27
|
console.log(getOrThrow(obj, "d"));
|
|
28
|
+
|
|
29
|
+
/** This will throw an error: "Failed to find d" */
|
|
30
|
+
const key = "d";
|
|
31
|
+
console.log(getOrThrow(obj, key, `Failed to find ${key}`));
|
|
27
32
|
```
|
package/dist/index.cjs
CHANGED
|
@@ -25,19 +25,21 @@ __export(src_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(src_exports);
|
|
26
26
|
|
|
27
27
|
// src/get-or-throw.ts
|
|
28
|
-
function getOrThrow(objOrArr, keyOrIndex) {
|
|
28
|
+
function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
29
29
|
if (Array.isArray(objOrArr)) {
|
|
30
30
|
if (keyOrIndex in objOrArr) {
|
|
31
31
|
return objOrArr[keyOrIndex];
|
|
32
32
|
} else {
|
|
33
|
-
throw new Error(
|
|
33
|
+
throw new Error(
|
|
34
|
+
errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`
|
|
35
|
+
);
|
|
34
36
|
}
|
|
35
37
|
} else {
|
|
36
38
|
if (keyOrIndex in objOrArr) {
|
|
37
39
|
return objOrArr[keyOrIndex];
|
|
38
40
|
} else {
|
|
39
41
|
throw new Error(
|
|
40
|
-
`Key "${String(keyOrIndex)}" does not exist in the object.`
|
|
42
|
+
errorMessage ?? `Key "${String(keyOrIndex)}" does not exist in the object.`
|
|
41
43
|
);
|
|
42
44
|
}
|
|
43
45
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Get a value from an object or array, or throw an error if the key or index
|
|
2
|
+
* Get a value from an object or array, or throw an error if the key or index
|
|
3
|
+
* does not exist.
|
|
4
|
+
*
|
|
3
5
|
* @param objOrArr The object or array to get the value from.
|
|
4
6
|
* @param keyOrIndex The key or index to get the value from.
|
|
7
|
+
* @param errorMessage Optional error message to include in the error thrown.
|
|
5
8
|
* @returns The value at the given key or index.
|
|
6
9
|
* @throws An error if the key or index does not exist.
|
|
7
10
|
*/
|
|
8
|
-
declare function getOrThrow<T extends object, K extends keyof T>(obj: T, key: K): T[K];
|
|
9
|
-
declare function getOrThrow<T>(arr: T[], index: number): T;
|
|
11
|
+
declare function getOrThrow<T extends object, K extends keyof T>(obj: T, key: K, errorMessage?: string): T[K];
|
|
12
|
+
declare function getOrThrow<T>(arr: T[], index: number, errorMessage?: string): T;
|
|
10
13
|
|
|
11
14
|
export { getOrThrow };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Get a value from an object or array, or throw an error if the key or index
|
|
2
|
+
* Get a value from an object or array, or throw an error if the key or index
|
|
3
|
+
* does not exist.
|
|
4
|
+
*
|
|
3
5
|
* @param objOrArr The object or array to get the value from.
|
|
4
6
|
* @param keyOrIndex The key or index to get the value from.
|
|
7
|
+
* @param errorMessage Optional error message to include in the error thrown.
|
|
5
8
|
* @returns The value at the given key or index.
|
|
6
9
|
* @throws An error if the key or index does not exist.
|
|
7
10
|
*/
|
|
8
|
-
declare function getOrThrow<T extends object, K extends keyof T>(obj: T, key: K): T[K];
|
|
9
|
-
declare function getOrThrow<T>(arr: T[], index: number): T;
|
|
11
|
+
declare function getOrThrow<T extends object, K extends keyof T>(obj: T, key: K, errorMessage?: string): T[K];
|
|
12
|
+
declare function getOrThrow<T>(arr: T[], index: number, errorMessage?: string): T;
|
|
10
13
|
|
|
11
14
|
export { getOrThrow };
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
// src/get-or-throw.ts
|
|
2
|
-
function getOrThrow(objOrArr, keyOrIndex) {
|
|
2
|
+
function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
3
3
|
if (Array.isArray(objOrArr)) {
|
|
4
4
|
if (keyOrIndex in objOrArr) {
|
|
5
5
|
return objOrArr[keyOrIndex];
|
|
6
6
|
} else {
|
|
7
|
-
throw new Error(
|
|
7
|
+
throw new Error(
|
|
8
|
+
errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`
|
|
9
|
+
);
|
|
8
10
|
}
|
|
9
11
|
} else {
|
|
10
12
|
if (keyOrIndex in objOrArr) {
|
|
11
13
|
return objOrArr[keyOrIndex];
|
|
12
14
|
} else {
|
|
13
15
|
throw new Error(
|
|
14
|
-
`Key "${String(keyOrIndex)}" does not exist in the object.`
|
|
16
|
+
errorMessage ?? `Key "${String(keyOrIndex)}" does not exist in the object.`
|
|
15
17
|
);
|
|
16
18
|
}
|
|
17
19
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "get-or-throw",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "A convenience function for adhering to
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "A convenience function for adhering to Typescript's noUncheckedIndexedAccess rule",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -16,6 +16,9 @@
|
|
|
16
16
|
"url": "git+https://github.com/0x80/get-or-throw.git"
|
|
17
17
|
},
|
|
18
18
|
"keywords": [
|
|
19
|
+
"typescript",
|
|
20
|
+
"indexed",
|
|
21
|
+
"unchecked",
|
|
19
22
|
"noUncheckedIndexedAccess"
|
|
20
23
|
],
|
|
21
24
|
"author": "Thijs Koerselman",
|
|
@@ -26,6 +29,8 @@
|
|
|
26
29
|
"homepage": "https://github.com/0x80/get-or-throw#readme",
|
|
27
30
|
"devDependencies": {
|
|
28
31
|
"@codecompose/typescript-config": "^1.0.5",
|
|
32
|
+
"prettier": "^3.3.3",
|
|
33
|
+
"prettier-plugin-jsdoc": "^1.3.0",
|
|
29
34
|
"tsup": "^8.3.0",
|
|
30
35
|
"typescript": "^5.6.2"
|
|
31
36
|
},
|
package/src/get-or-throw.ts
CHANGED
|
@@ -1,31 +1,43 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Get a value from an object or array, or throw an error if the key or index
|
|
2
|
+
* Get a value from an object or array, or throw an error if the key or index
|
|
3
|
+
* does not exist.
|
|
4
|
+
*
|
|
3
5
|
* @param objOrArr The object or array to get the value from.
|
|
4
6
|
* @param keyOrIndex The key or index to get the value from.
|
|
7
|
+
* @param errorMessage Optional error message to include in the error thrown.
|
|
5
8
|
* @returns The value at the given key or index.
|
|
6
9
|
* @throws An error if the key or index does not exist.
|
|
7
10
|
*/
|
|
8
11
|
export function getOrThrow<T extends object, K extends keyof T>(
|
|
9
12
|
obj: T,
|
|
10
|
-
key: K
|
|
13
|
+
key: K,
|
|
14
|
+
errorMessage?: string
|
|
11
15
|
): T[K];
|
|
12
|
-
export function getOrThrow<T>(
|
|
16
|
+
export function getOrThrow<T>(
|
|
17
|
+
arr: T[],
|
|
18
|
+
index: number,
|
|
19
|
+
errorMessage?: string
|
|
20
|
+
): T;
|
|
13
21
|
export function getOrThrow<T extends object, K extends keyof T>(
|
|
14
22
|
objOrArr: T | T[],
|
|
15
|
-
keyOrIndex: K | number
|
|
23
|
+
keyOrIndex: K | number,
|
|
24
|
+
errorMessage?: string
|
|
16
25
|
): T[K] | T {
|
|
17
26
|
if (Array.isArray(objOrArr)) {
|
|
18
27
|
if (keyOrIndex in objOrArr) {
|
|
19
28
|
return objOrArr[keyOrIndex as number]!;
|
|
20
29
|
} else {
|
|
21
|
-
throw new Error(
|
|
30
|
+
throw new Error(
|
|
31
|
+
errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`
|
|
32
|
+
);
|
|
22
33
|
}
|
|
23
34
|
} else {
|
|
24
35
|
if (keyOrIndex in objOrArr) {
|
|
25
36
|
return objOrArr[keyOrIndex as K];
|
|
26
37
|
} else {
|
|
27
38
|
throw new Error(
|
|
28
|
-
|
|
39
|
+
errorMessage ??
|
|
40
|
+
`Key "${String(keyOrIndex)}" does not exist in the object.`
|
|
29
41
|
);
|
|
30
42
|
}
|
|
31
43
|
}
|