get-or-throw 1.5.1 → 2.0.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/.github/workflows/ci.yml +9 -7
- package/README.md +10 -6
- package/dist/index.cjs +2 -11
- package/dist/index.d.cts +6 -6
- package/dist/index.d.ts +6 -6
- package/dist/index.js +2 -11
- package/package.json +1 -1
- package/src/get-or-throw.test.ts +7 -5
- package/src/get-or-throw.ts +18 -18
package/.github/workflows/ci.yml
CHANGED
|
@@ -4,7 +4,11 @@ on:
|
|
|
4
4
|
push:
|
|
5
5
|
branches: [main]
|
|
6
6
|
pull_request:
|
|
7
|
-
branches: [
|
|
7
|
+
branches: ["**"]
|
|
8
|
+
|
|
9
|
+
concurrency:
|
|
10
|
+
group: ${{ github.workflow }}-${{ github.ref }}
|
|
11
|
+
cancel-in-progress: true
|
|
8
12
|
|
|
9
13
|
jobs:
|
|
10
14
|
verify:
|
|
@@ -22,12 +26,10 @@ jobs:
|
|
|
22
26
|
- name: Setup Node.js
|
|
23
27
|
uses: actions/setup-node@v4
|
|
24
28
|
with:
|
|
25
|
-
node-version:
|
|
29
|
+
node-version: 22
|
|
26
30
|
|
|
27
|
-
- name:
|
|
28
|
-
|
|
29
|
-
with:
|
|
30
|
-
version: 8
|
|
31
|
+
- name: Enable corepack
|
|
32
|
+
run: corepack enable pnpm
|
|
31
33
|
|
|
32
34
|
- name: Get pnpm store directory
|
|
33
35
|
id: pnpm-cache
|
|
@@ -48,7 +50,7 @@ jobs:
|
|
|
48
50
|
${{ runner.os }}-pnpm-store-
|
|
49
51
|
|
|
50
52
|
- name: Install dependencies
|
|
51
|
-
run: pnpm install
|
|
53
|
+
run: pnpm install --frozen-lockfile
|
|
52
54
|
|
|
53
55
|
- name: Run ${{ matrix.command }}
|
|
54
56
|
run: pnpm ${{ matrix.command }}
|
package/README.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
A convenience function for safely accessing values in dynamic objects and
|
|
4
4
|
arrays. It gets the value at a specified key or index, and throws an error if
|
|
5
|
-
the resulting value is `undefined
|
|
6
|
-
|
|
5
|
+
the resulting value is `undefined`. Optionally, you can set a custom error
|
|
6
|
+
message.
|
|
7
7
|
|
|
8
8
|
This was created to make it easy to adhere to Typescript's
|
|
9
9
|
[noUncheckedIndexedAccess](https://www.typescriptlang.org/tsconfig/#noUncheckedIndexedAccess)
|
|
@@ -52,15 +52,19 @@ const value = got(obj, "d");
|
|
|
52
52
|
const key = "d";
|
|
53
53
|
const value = got(obj, key, `Failed to find ${key}`);
|
|
54
54
|
|
|
55
|
-
/**
|
|
55
|
+
/** Null is a valid value */
|
|
56
56
|
const arr = [1, null, 3];
|
|
57
|
-
const value = got(arr, 1);
|
|
57
|
+
const value = got(arr, 1); // Output: null
|
|
58
58
|
|
|
59
|
-
/** This will throw an error: "Value at index 1 is undefined
|
|
59
|
+
/** This will throw an error: "Value at index 1 is undefined." */
|
|
60
60
|
const arr = [1, undefined, 3];
|
|
61
61
|
const value = got(arr, 1);
|
|
62
62
|
|
|
63
|
-
/**
|
|
63
|
+
/** Null is a valid value */
|
|
64
|
+
const obj = { a: 1, b: null, c: 3 };
|
|
65
|
+
const value = got(obj, "b"); // Output: null
|
|
66
|
+
|
|
67
|
+
/** This will throw an error: "Value at key 'b' is undefined." */
|
|
64
68
|
const obj = { a: 1, b: undefined, c: 3 };
|
|
65
69
|
const value = got(obj, "b");
|
|
66
70
|
```
|
package/dist/index.cjs
CHANGED
|
@@ -39,10 +39,6 @@ function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
|
39
39
|
throw new Error(
|
|
40
40
|
errorMessage ?? `Value at index ${String(keyOrIndex)} is undefined.`
|
|
41
41
|
);
|
|
42
|
-
} else if (value === null) {
|
|
43
|
-
throw new Error(
|
|
44
|
-
errorMessage ?? `Value at index ${String(keyOrIndex)} is null.`
|
|
45
|
-
);
|
|
46
42
|
}
|
|
47
43
|
return value;
|
|
48
44
|
} else {
|
|
@@ -53,17 +49,12 @@ function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
|
53
49
|
} else {
|
|
54
50
|
if (keyOrIndex in objOrArr) {
|
|
55
51
|
const value = objOrArr[keyOrIndex];
|
|
56
|
-
if (value
|
|
57
|
-
return value;
|
|
58
|
-
} else if (value === void 0) {
|
|
52
|
+
if (value === void 0) {
|
|
59
53
|
throw new Error(
|
|
60
54
|
errorMessage ?? `Value at key "${String(keyOrIndex)}" is undefined.`
|
|
61
55
|
);
|
|
62
|
-
} else {
|
|
63
|
-
throw new Error(
|
|
64
|
-
errorMessage ?? `Value at key "${String(keyOrIndex)}" is null.`
|
|
65
|
-
);
|
|
66
56
|
}
|
|
57
|
+
return value;
|
|
67
58
|
} else {
|
|
68
59
|
throw new Error(
|
|
69
60
|
errorMessage ?? `Key "${String(keyOrIndex)}" does not exist in the object.`
|
package/dist/index.d.cts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Get a value from an object or array, and throw an error if the key or index
|
|
3
|
-
* does not exist or if the resulting value is undefined
|
|
3
|
+
* does not exist or if the resulting value is undefined.
|
|
4
4
|
*
|
|
5
5
|
* @param objOrArr The object or array to get the value from.
|
|
6
6
|
* @param keyOrIndex The key or index to get the value from.
|
|
7
7
|
* @param errorMessage Optional error message to include in the error thrown.
|
|
8
|
-
* @returns The value at the given key or index, guaranteed to be
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* value is undefined or null.
|
|
8
|
+
* @returns The value at the given key or index, guaranteed to be defined.
|
|
9
|
+
* @throws An error if the key or index does not exist, or if the value is
|
|
10
|
+
* undefined.
|
|
12
11
|
*/
|
|
13
|
-
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T
|
|
12
|
+
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T, keyOrIndex: K, errorMessage?: string): T[K];
|
|
13
|
+
declare function getOrThrow<T>(objOrArr: T[], keyOrIndex: number, errorMessage?: string): T;
|
|
14
14
|
|
|
15
15
|
export { getOrThrow, getOrThrow as got };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Get a value from an object or array, and throw an error if the key or index
|
|
3
|
-
* does not exist or if the resulting value is undefined
|
|
3
|
+
* does not exist or if the resulting value is undefined.
|
|
4
4
|
*
|
|
5
5
|
* @param objOrArr The object or array to get the value from.
|
|
6
6
|
* @param keyOrIndex The key or index to get the value from.
|
|
7
7
|
* @param errorMessage Optional error message to include in the error thrown.
|
|
8
|
-
* @returns The value at the given key or index, guaranteed to be
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* value is undefined or null.
|
|
8
|
+
* @returns The value at the given key or index, guaranteed to be defined.
|
|
9
|
+
* @throws An error if the key or index does not exist, or if the value is
|
|
10
|
+
* undefined.
|
|
12
11
|
*/
|
|
13
|
-
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T
|
|
12
|
+
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T, keyOrIndex: K, errorMessage?: string): T[K];
|
|
13
|
+
declare function getOrThrow<T>(objOrArr: T[], keyOrIndex: number, errorMessage?: string): T;
|
|
14
14
|
|
|
15
15
|
export { getOrThrow, getOrThrow as got };
|
package/dist/index.js
CHANGED
|
@@ -12,10 +12,6 @@ function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
|
12
12
|
throw new Error(
|
|
13
13
|
errorMessage ?? `Value at index ${String(keyOrIndex)} is undefined.`
|
|
14
14
|
);
|
|
15
|
-
} else if (value === null) {
|
|
16
|
-
throw new Error(
|
|
17
|
-
errorMessage ?? `Value at index ${String(keyOrIndex)} is null.`
|
|
18
|
-
);
|
|
19
15
|
}
|
|
20
16
|
return value;
|
|
21
17
|
} else {
|
|
@@ -26,17 +22,12 @@ function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
|
26
22
|
} else {
|
|
27
23
|
if (keyOrIndex in objOrArr) {
|
|
28
24
|
const value = objOrArr[keyOrIndex];
|
|
29
|
-
if (value
|
|
30
|
-
return value;
|
|
31
|
-
} else if (value === void 0) {
|
|
25
|
+
if (value === void 0) {
|
|
32
26
|
throw new Error(
|
|
33
27
|
errorMessage ?? `Value at key "${String(keyOrIndex)}" is undefined.`
|
|
34
28
|
);
|
|
35
|
-
} else {
|
|
36
|
-
throw new Error(
|
|
37
|
-
errorMessage ?? `Value at key "${String(keyOrIndex)}" is null.`
|
|
38
|
-
);
|
|
39
29
|
}
|
|
30
|
+
return value;
|
|
40
31
|
} else {
|
|
41
32
|
throw new Error(
|
|
42
33
|
errorMessage ?? `Key "${String(keyOrIndex)}" does not exist in the object.`
|
package/package.json
CHANGED
package/src/get-or-throw.test.ts
CHANGED
|
@@ -20,9 +20,9 @@ describe("get-or-throw", () => {
|
|
|
20
20
|
expect(() => got(arr, 3)).toThrow("Index 3 is out of bounds.");
|
|
21
21
|
});
|
|
22
22
|
|
|
23
|
-
it("should
|
|
23
|
+
it("should allow null values", () => {
|
|
24
24
|
const arr = [1, null, 3];
|
|
25
|
-
expect(
|
|
25
|
+
expect(got(arr, 1)).toBeNull();
|
|
26
26
|
});
|
|
27
27
|
|
|
28
28
|
it("should throw on undefined values", () => {
|
|
@@ -45,14 +45,16 @@ describe("get-or-throw", () => {
|
|
|
45
45
|
);
|
|
46
46
|
});
|
|
47
47
|
|
|
48
|
-
it("should
|
|
48
|
+
it("should allow null values", () => {
|
|
49
49
|
const obj = { a: 1, b: null, c: 3 };
|
|
50
|
-
expect(
|
|
50
|
+
expect(got(obj, "b")).toBeNull();
|
|
51
51
|
});
|
|
52
52
|
|
|
53
53
|
it("should throw on undefined values", () => {
|
|
54
54
|
const obj = { a: 1, b: undefined, c: 3 };
|
|
55
|
-
expect(() =>
|
|
55
|
+
expect(() => {
|
|
56
|
+
got(obj, "b");
|
|
57
|
+
}).toThrow('Value at key "b" is undefined.');
|
|
56
58
|
});
|
|
57
59
|
});
|
|
58
60
|
|
package/src/get-or-throw.ts
CHANGED
|
@@ -1,20 +1,29 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Get a value from an object or array, and throw an error if the key or index
|
|
3
|
-
* does not exist or if the resulting value is undefined
|
|
3
|
+
* does not exist or if the resulting value is undefined.
|
|
4
4
|
*
|
|
5
5
|
* @param objOrArr The object or array to get the value from.
|
|
6
6
|
* @param keyOrIndex The key or index to get the value from.
|
|
7
7
|
* @param errorMessage Optional error message to include in the error thrown.
|
|
8
|
-
* @returns The value at the given key or index, guaranteed to be
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* value is undefined or null.
|
|
8
|
+
* @returns The value at the given key or index, guaranteed to be defined.
|
|
9
|
+
* @throws An error if the key or index does not exist, or if the value is
|
|
10
|
+
* undefined.
|
|
12
11
|
*/
|
|
13
12
|
export function getOrThrow<T extends object, K extends keyof T>(
|
|
14
|
-
objOrArr: T
|
|
13
|
+
objOrArr: T,
|
|
14
|
+
keyOrIndex: K,
|
|
15
|
+
errorMessage?: string,
|
|
16
|
+
): T[K];
|
|
17
|
+
export function getOrThrow<T>(
|
|
18
|
+
objOrArr: T[],
|
|
19
|
+
keyOrIndex: number,
|
|
20
|
+
errorMessage?: string,
|
|
21
|
+
): T;
|
|
22
|
+
export function getOrThrow<T extends object, K extends keyof T>(
|
|
23
|
+
objOrArr: T | T[],
|
|
15
24
|
keyOrIndex: K | number,
|
|
16
25
|
errorMessage?: string,
|
|
17
|
-
):
|
|
26
|
+
): T[K] | T {
|
|
18
27
|
if (Array.isArray(objOrArr)) {
|
|
19
28
|
const length = objOrArr.length;
|
|
20
29
|
let index = keyOrIndex as number;
|
|
@@ -31,10 +40,6 @@ export function getOrThrow<T extends object, K extends keyof T>(
|
|
|
31
40
|
throw new Error(
|
|
32
41
|
errorMessage ?? `Value at index ${String(keyOrIndex)} is undefined.`,
|
|
33
42
|
);
|
|
34
|
-
} else if (value === null) {
|
|
35
|
-
throw new Error(
|
|
36
|
-
errorMessage ?? `Value at index ${String(keyOrIndex)} is null.`,
|
|
37
|
-
);
|
|
38
43
|
}
|
|
39
44
|
return value;
|
|
40
45
|
} else {
|
|
@@ -46,17 +51,12 @@ export function getOrThrow<T extends object, K extends keyof T>(
|
|
|
46
51
|
if (keyOrIndex in objOrArr) {
|
|
47
52
|
const value = objOrArr[keyOrIndex as K];
|
|
48
53
|
|
|
49
|
-
if (value
|
|
50
|
-
return value as NonNullable<T[K]>;
|
|
51
|
-
} else if (value === undefined) {
|
|
54
|
+
if (value === undefined) {
|
|
52
55
|
throw new Error(
|
|
53
56
|
errorMessage ?? `Value at key "${String(keyOrIndex)}" is undefined.`,
|
|
54
57
|
);
|
|
55
|
-
} else {
|
|
56
|
-
throw new Error(
|
|
57
|
-
errorMessage ?? `Value at key "${String(keyOrIndex)}" is null.`,
|
|
58
|
-
);
|
|
59
58
|
}
|
|
59
|
+
return value;
|
|
60
60
|
} else {
|
|
61
61
|
throw new Error(
|
|
62
62
|
errorMessage ??
|