get-or-throw 1.5.0 → 2.0.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/.github/workflows/CODEOWNERS +1 -0
- package/.github/workflows/ci.yml +56 -0
- package/.prettierignore +1 -0
- package/.prettierrc.json +0 -1
- package/README.md +104 -19
- package/dist/index.cjs +4 -14
- package/dist/index.d.cts +6 -7
- package/dist/index.d.ts +6 -7
- package/dist/index.js +4 -14
- package/eslint.config.js +28 -0
- package/package.json +11 -4
- package/src/get-or-throw.test.ts +70 -0
- package/src/get-or-throw.ts +18 -29
- package/tsconfig.json +4 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* @0x80
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
concurrency:
|
|
10
|
+
group: ${{ github.workflow }}-${{ github.ref }}
|
|
11
|
+
cancel-in-progress: true
|
|
12
|
+
|
|
13
|
+
jobs:
|
|
14
|
+
verify:
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
strategy:
|
|
17
|
+
fail-fast: false
|
|
18
|
+
matrix:
|
|
19
|
+
command: [lint, type-check, test]
|
|
20
|
+
|
|
21
|
+
name: ${{ matrix.command }}
|
|
22
|
+
|
|
23
|
+
steps:
|
|
24
|
+
- uses: actions/checkout@v4
|
|
25
|
+
|
|
26
|
+
- name: Setup Node.js
|
|
27
|
+
uses: actions/setup-node@v4
|
|
28
|
+
with:
|
|
29
|
+
node-version: 22
|
|
30
|
+
|
|
31
|
+
- name: Enable corepack
|
|
32
|
+
run: corepack enable pnpm
|
|
33
|
+
|
|
34
|
+
- name: Get pnpm store directory
|
|
35
|
+
id: pnpm-cache
|
|
36
|
+
shell: bash
|
|
37
|
+
run: |
|
|
38
|
+
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
|
|
39
|
+
|
|
40
|
+
- name: Setup pnpm cache
|
|
41
|
+
uses: actions/cache@v4
|
|
42
|
+
with:
|
|
43
|
+
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
|
|
44
|
+
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
|
|
45
|
+
# First tries exact match with lock file hash. If not found,
|
|
46
|
+
# falls back to any cache starting with 'pnpm-store-'.
|
|
47
|
+
# This way we get exact cache on repeated runs, but can still
|
|
48
|
+
# use older cache as starting point when dependencies change.
|
|
49
|
+
restore-keys: |
|
|
50
|
+
${{ runner.os }}-pnpm-store-
|
|
51
|
+
|
|
52
|
+
- name: Install dependencies
|
|
53
|
+
run: pnpm install --frozen-lockfile
|
|
54
|
+
|
|
55
|
+
- name: Run ${{ matrix.command }}
|
|
56
|
+
run: pnpm ${{ matrix.command }}
|
package/.prettierignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
*.yml
|
package/.prettierrc.json
CHANGED
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Get-Or-Throw / Got
|
|
2
2
|
|
|
3
|
-
A
|
|
4
|
-
It gets
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
A convenience function for safely accessing values in dynamic objects and
|
|
4
|
+
arrays. It gets the value at a specified key or index, and throws an error if
|
|
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)
|
|
@@ -11,7 +11,7 @@ setting, which is recommended for strict type checking.
|
|
|
11
11
|
|
|
12
12
|
## Features
|
|
13
13
|
|
|
14
|
-
-
|
|
14
|
+
- Typescript assertions for type narrowing.
|
|
15
15
|
- Works with both objects and arrays.
|
|
16
16
|
- Supports negative indexing for arrays.
|
|
17
17
|
- Allows for custom error messages.
|
|
@@ -28,38 +28,123 @@ pnpm add get-or-throw
|
|
|
28
28
|
|
|
29
29
|
## Usage
|
|
30
30
|
|
|
31
|
+
The example code below uses the `got` alias but `getOrThrow` is also available
|
|
32
|
+
if you want to be more explicit.
|
|
33
|
+
|
|
31
34
|
```ts
|
|
32
35
|
const arr = [1, 2, 3];
|
|
33
|
-
|
|
36
|
+
const value = got(arr, 1); // Output: 2
|
|
34
37
|
|
|
35
38
|
/** Support for negative indexing */
|
|
36
39
|
const arr = [1, 2, 3];
|
|
37
|
-
|
|
40
|
+
const value = got(arr, -1); // Output: 3
|
|
38
41
|
|
|
39
42
|
/** This will throw an error: "Index 3 is out of bounds." */
|
|
40
|
-
|
|
43
|
+
const value = got(arr, 3);
|
|
41
44
|
|
|
42
45
|
const obj = { a: 1, b: 2, c: 3 };
|
|
43
|
-
|
|
46
|
+
const value = got(obj, "b"); // Output: 2
|
|
44
47
|
|
|
45
48
|
/** This will throw an error: "Key "d" does not exist in the object." */
|
|
46
|
-
|
|
49
|
+
const value = got(obj, "d");
|
|
47
50
|
|
|
48
51
|
/** This will throw an error: "Failed to find d" */
|
|
49
52
|
const key = "d";
|
|
50
|
-
|
|
53
|
+
const value = got(obj, key, `Failed to find ${key}`);
|
|
51
54
|
|
|
52
|
-
/**
|
|
55
|
+
/** Null is a valid value */
|
|
53
56
|
const arr = [1, null, 3];
|
|
54
|
-
|
|
57
|
+
const value = got(arr, 1); // Output: null
|
|
55
58
|
|
|
56
|
-
/** This will throw an error: "Value at index 1 is undefined
|
|
59
|
+
/** This will throw an error: "Value at index 1 is undefined." */
|
|
57
60
|
const arr = [1, undefined, 3];
|
|
58
|
-
|
|
61
|
+
const value = got(arr, 1);
|
|
62
|
+
|
|
63
|
+
/** Null is a valid value */
|
|
64
|
+
const obj = { a: 1, b: null, c: 3 };
|
|
65
|
+
const value = got(obj, "b"); // Output: null
|
|
59
66
|
|
|
60
|
-
/** This will throw an error: "Value at key 'b' is undefined
|
|
67
|
+
/** This will throw an error: "Value at key 'b' is undefined." */
|
|
61
68
|
const obj = { a: 1, b: undefined, c: 3 };
|
|
62
|
-
|
|
69
|
+
const value = got(obj, "b");
|
|
63
70
|
```
|
|
64
71
|
|
|
65
|
-
|
|
72
|
+
And here's the updated test file:
|
|
73
|
+
|
|
74
|
+
```typescript:src/get-or-throw.test.ts
|
|
75
|
+
import { describe, it, expect } from 'vitest';
|
|
76
|
+
import { got, getOrThrow } from './get-or-throw';
|
|
77
|
+
|
|
78
|
+
describe('get-or-throw', () => {
|
|
79
|
+
describe('array access', () => {
|
|
80
|
+
it('should get value at positive index', () => {
|
|
81
|
+
const arr = [1, 2, 3];
|
|
82
|
+
expect(got(arr, 1)).toBe(2);
|
|
83
|
+
expect(getOrThrow(arr, 1)).toBe(2);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should support negative indexing', () => {
|
|
87
|
+
const arr = [1, 2, 3];
|
|
88
|
+
expect(got(arr, -1)).toBe(3);
|
|
89
|
+
expect(got(arr, -2)).toBe(2);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should throw on out of bounds index', () => {
|
|
93
|
+
const arr = [1, 2, 3];
|
|
94
|
+
expect(() => got(arr, 3)).toThrow('Index 3 is out of bounds.');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should allow null values', () => {
|
|
98
|
+
const arr = [1, null, 3];
|
|
99
|
+
expect(got(arr, 1)).toBeNull();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should throw on undefined values', () => {
|
|
103
|
+
const arr = [1, undefined, 3];
|
|
104
|
+
expect(() => got(arr, 1)).toThrow('Value at index 1 is undefined.');
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe('object access', () => {
|
|
109
|
+
it('should get value at existing key', () => {
|
|
110
|
+
const obj = { a: 1, b: 2, c: 3 };
|
|
111
|
+
expect(got(obj, 'b')).toBe(2);
|
|
112
|
+
expect(getOrThrow(obj, 'b')).toBe(2);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should throw on non-existent key', () => {
|
|
116
|
+
const obj = { a: 1, b: 2, c: 3 };
|
|
117
|
+
expect(() => got(obj, 'd' as keyof typeof obj)).toThrow(
|
|
118
|
+
'Key "d" does not exist in the object.'
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should allow null values', () => {
|
|
123
|
+
const obj = { a: 1, b: null, c: 3 };
|
|
124
|
+
expect(got(obj, 'b')).toBeNull();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('should throw on undefined values', () => {
|
|
128
|
+
const obj = { a: 1, b: undefined, c: 3 };
|
|
129
|
+
expect(() => got(obj, 'b')).toThrow('Value at key "b" is undefined.');
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('custom error messages', () => {
|
|
134
|
+
it('should use custom error message when provided', () => {
|
|
135
|
+
const obj = { a: 1, b: 2, c: 3 };
|
|
136
|
+
const key = 'd';
|
|
137
|
+
expect(() => got(obj, key as keyof typeof obj, `Failed to find ${key}`)).toThrow(
|
|
138
|
+
'Failed to find d'
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
The key changes:
|
|
146
|
+
|
|
147
|
+
1. Updated documentation to clarify that only `undefined` values throw
|
|
148
|
+
2. Added examples showing that `null` is now a valid value
|
|
149
|
+
3. Updated tests to verify `null` values are allowed
|
|
150
|
+
4. Renamed test cases to reflect the new behavior
|
package/dist/index.cjs
CHANGED
|
@@ -35,17 +35,12 @@ function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
|
35
35
|
}
|
|
36
36
|
if (index >= 0 && index < length) {
|
|
37
37
|
const value = objOrArr[index];
|
|
38
|
-
if (value
|
|
39
|
-
return value;
|
|
40
|
-
} else if (value === void 0) {
|
|
38
|
+
if (value === void 0) {
|
|
41
39
|
throw new Error(
|
|
42
40
|
errorMessage ?? `Value at index ${String(keyOrIndex)} is undefined.`
|
|
43
41
|
);
|
|
44
|
-
} else {
|
|
45
|
-
throw new Error(
|
|
46
|
-
errorMessage ?? `Value at index ${String(keyOrIndex)} is null.`
|
|
47
|
-
);
|
|
48
42
|
}
|
|
43
|
+
return value;
|
|
49
44
|
} else {
|
|
50
45
|
throw new Error(
|
|
51
46
|
errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`
|
|
@@ -54,17 +49,12 @@ function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
|
54
49
|
} else {
|
|
55
50
|
if (keyOrIndex in objOrArr) {
|
|
56
51
|
const value = objOrArr[keyOrIndex];
|
|
57
|
-
if (value
|
|
58
|
-
return value;
|
|
59
|
-
} else if (value === void 0) {
|
|
52
|
+
if (value === void 0) {
|
|
60
53
|
throw new Error(
|
|
61
54
|
errorMessage ?? `Value at key "${String(keyOrIndex)}" is undefined.`
|
|
62
55
|
);
|
|
63
|
-
} else {
|
|
64
|
-
throw new Error(
|
|
65
|
-
errorMessage ?? `Value at key "${String(keyOrIndex)}" is null.`
|
|
66
|
-
);
|
|
67
56
|
}
|
|
57
|
+
return value;
|
|
68
58
|
} else {
|
|
69
59
|
throw new Error(
|
|
70
60
|
errorMessage ?? `Key "${String(keyOrIndex)}" does not exist in the object.`
|
package/dist/index.d.cts
CHANGED
|
@@ -1,16 +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, keyOrIndex: K, errorMessage?: string):
|
|
14
|
-
declare function getOrThrow<T>(objOrArr: T[], keyOrIndex: number, errorMessage?: string):
|
|
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;
|
|
15
14
|
|
|
16
15
|
export { getOrThrow, getOrThrow as got };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +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, keyOrIndex: K, errorMessage?: string):
|
|
14
|
-
declare function getOrThrow<T>(objOrArr: T[], keyOrIndex: number, errorMessage?: string):
|
|
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;
|
|
15
14
|
|
|
16
15
|
export { getOrThrow, getOrThrow as got };
|
package/dist/index.js
CHANGED
|
@@ -8,17 +8,12 @@ function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
|
8
8
|
}
|
|
9
9
|
if (index >= 0 && index < length) {
|
|
10
10
|
const value = objOrArr[index];
|
|
11
|
-
if (value
|
|
12
|
-
return value;
|
|
13
|
-
} else if (value === void 0) {
|
|
11
|
+
if (value === void 0) {
|
|
14
12
|
throw new Error(
|
|
15
13
|
errorMessage ?? `Value at index ${String(keyOrIndex)} is undefined.`
|
|
16
14
|
);
|
|
17
|
-
} else {
|
|
18
|
-
throw new Error(
|
|
19
|
-
errorMessage ?? `Value at index ${String(keyOrIndex)} is null.`
|
|
20
|
-
);
|
|
21
15
|
}
|
|
16
|
+
return value;
|
|
22
17
|
} else {
|
|
23
18
|
throw new Error(
|
|
24
19
|
errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`
|
|
@@ -27,17 +22,12 @@ function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
|
27
22
|
} else {
|
|
28
23
|
if (keyOrIndex in objOrArr) {
|
|
29
24
|
const value = objOrArr[keyOrIndex];
|
|
30
|
-
if (value
|
|
31
|
-
return value;
|
|
32
|
-
} else if (value === void 0) {
|
|
25
|
+
if (value === void 0) {
|
|
33
26
|
throw new Error(
|
|
34
27
|
errorMessage ?? `Value at key "${String(keyOrIndex)}" is undefined.`
|
|
35
28
|
);
|
|
36
|
-
} else {
|
|
37
|
-
throw new Error(
|
|
38
|
-
errorMessage ?? `Value at key "${String(keyOrIndex)}" is null.`
|
|
39
|
-
);
|
|
40
29
|
}
|
|
30
|
+
return value;
|
|
41
31
|
} else {
|
|
42
32
|
throw new Error(
|
|
43
33
|
errorMessage ?? `Key "${String(keyOrIndex)}" does not exist in the object.`
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import eslint from "@eslint/js";
|
|
2
|
+
import tseslint from "typescript-eslint";
|
|
3
|
+
|
|
4
|
+
export default tseslint.config(
|
|
5
|
+
{
|
|
6
|
+
ignores: [
|
|
7
|
+
"dist/**",
|
|
8
|
+
"node_modules/**",
|
|
9
|
+
/**
|
|
10
|
+
* Ignore all config files int the root. We should instead solve this with
|
|
11
|
+
* projectService setting so that these files can also be linted, but
|
|
12
|
+
* let's get this to work first
|
|
13
|
+
*/
|
|
14
|
+
"*.{js,ts}",
|
|
15
|
+
],
|
|
16
|
+
},
|
|
17
|
+
eslint.configs.recommended,
|
|
18
|
+
tseslint.configs.strictTypeChecked,
|
|
19
|
+
tseslint.configs.stylisticTypeChecked,
|
|
20
|
+
{
|
|
21
|
+
languageOptions: {
|
|
22
|
+
parserOptions: {
|
|
23
|
+
projectService: true,
|
|
24
|
+
tsconfigRootDir: import.meta.dirname,
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "get-or-throw",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "A convenience function for
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "A convenience function for safely getting values from dynamic objects and arrays",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -29,15 +29,22 @@
|
|
|
29
29
|
"homepage": "https://github.com/0x80/get-or-throw#readme",
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@codecompose/typescript-config": "^1.1.2",
|
|
32
|
+
"@eslint/js": "^9.22.0",
|
|
32
33
|
"del-cli": "^5.1.0",
|
|
34
|
+
"eslint": "^9.22.0",
|
|
33
35
|
"prettier": "^3.3.3",
|
|
34
36
|
"prettier-plugin-jsdoc": "^1.3.0",
|
|
35
37
|
"tsup": "^8.3.0",
|
|
36
|
-
"typescript": "^5.6.2"
|
|
38
|
+
"typescript": "^5.6.2",
|
|
39
|
+
"typescript-eslint": "^8.26.0",
|
|
40
|
+
"vitest": "^3.0.8"
|
|
37
41
|
},
|
|
38
42
|
"scripts": {
|
|
39
43
|
"build": "tsup",
|
|
40
44
|
"clean": "del dist tsconfig.tsbuildinfo",
|
|
41
|
-
"
|
|
45
|
+
"lint": "eslint .",
|
|
46
|
+
"type-check": "tsc --noEmit",
|
|
47
|
+
"test": "vitest",
|
|
48
|
+
"format": "prettier --write ."
|
|
42
49
|
}
|
|
43
50
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { getOrThrow, got } from "./get-or-throw";
|
|
3
|
+
|
|
4
|
+
describe("get-or-throw", () => {
|
|
5
|
+
describe("array access", () => {
|
|
6
|
+
it("should get value at positive index", () => {
|
|
7
|
+
const arr = [1, 2, 3];
|
|
8
|
+
expect(got(arr, 1)).toBe(2);
|
|
9
|
+
expect(getOrThrow(arr, 1)).toBe(2);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("should support negative indexing", () => {
|
|
13
|
+
const arr = [1, 2, 3];
|
|
14
|
+
expect(got(arr, -1)).toBe(3);
|
|
15
|
+
expect(got(arr, -2)).toBe(2);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("should throw on out of bounds index", () => {
|
|
19
|
+
const arr = [1, 2, 3];
|
|
20
|
+
expect(() => got(arr, 3)).toThrow("Index 3 is out of bounds.");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("should allow null values", () => {
|
|
24
|
+
const arr = [1, null, 3];
|
|
25
|
+
expect(got(arr, 1)).toBeNull();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should throw on undefined values", () => {
|
|
29
|
+
const arr = [1, undefined, 3];
|
|
30
|
+
expect(() => got(arr, 1)).toThrow("Value at index 1 is undefined.");
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("object access", () => {
|
|
35
|
+
it("should get value at existing key", () => {
|
|
36
|
+
const obj = { a: 1, b: 2, c: 3 };
|
|
37
|
+
expect(got(obj, "b")).toBe(2);
|
|
38
|
+
expect(getOrThrow(obj, "b")).toBe(2);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should throw on non-existent key", () => {
|
|
42
|
+
const obj = { a: 1, b: 2, c: 3 };
|
|
43
|
+
expect(() => got(obj, "d" as keyof typeof obj)).toThrow(
|
|
44
|
+
'Key "d" does not exist in the object.',
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should allow null values", () => {
|
|
49
|
+
const obj = { a: 1, b: null, c: 3 };
|
|
50
|
+
expect(got(obj, "b")).toBeNull();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should throw on undefined values", () => {
|
|
54
|
+
const obj = { a: 1, b: undefined, c: 3 };
|
|
55
|
+
expect(() => {
|
|
56
|
+
got(obj, "b");
|
|
57
|
+
}).toThrow('Value at key "b" is undefined.');
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("custom error messages", () => {
|
|
62
|
+
it("should use custom error message when provided", () => {
|
|
63
|
+
const obj = { a: 1, b: 2, c: 3 };
|
|
64
|
+
const key = "d";
|
|
65
|
+
expect(() =>
|
|
66
|
+
got(obj, key as keyof typeof obj, `Failed to find ${key}`),
|
|
67
|
+
).toThrow("Failed to find d");
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
});
|
package/src/get-or-throw.ts
CHANGED
|
@@ -1,30 +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
13
|
objOrArr: T,
|
|
15
14
|
keyOrIndex: K,
|
|
16
|
-
errorMessage?: string
|
|
17
|
-
):
|
|
15
|
+
errorMessage?: string,
|
|
16
|
+
): T[K];
|
|
18
17
|
export function getOrThrow<T>(
|
|
19
18
|
objOrArr: T[],
|
|
20
19
|
keyOrIndex: number,
|
|
21
|
-
errorMessage?: string
|
|
22
|
-
):
|
|
20
|
+
errorMessage?: string,
|
|
21
|
+
): T;
|
|
23
22
|
export function getOrThrow<T extends object, K extends keyof T>(
|
|
24
23
|
objOrArr: T | T[],
|
|
25
24
|
keyOrIndex: K | number,
|
|
26
|
-
errorMessage?: string
|
|
27
|
-
):
|
|
25
|
+
errorMessage?: string,
|
|
26
|
+
): T[K] | T {
|
|
28
27
|
if (Array.isArray(objOrArr)) {
|
|
29
28
|
const length = objOrArr.length;
|
|
30
29
|
let index = keyOrIndex as number;
|
|
@@ -37,41 +36,31 @@ export function getOrThrow<T extends object, K extends keyof T>(
|
|
|
37
36
|
if (index >= 0 && index < length) {
|
|
38
37
|
const value = objOrArr[index];
|
|
39
38
|
|
|
40
|
-
if (value
|
|
41
|
-
return value as NonNullable<T>;
|
|
42
|
-
} else if (value === undefined) {
|
|
39
|
+
if (value === undefined) {
|
|
43
40
|
throw new Error(
|
|
44
|
-
errorMessage ?? `Value at index ${String(keyOrIndex)} is undefined
|
|
45
|
-
);
|
|
46
|
-
} else {
|
|
47
|
-
throw new Error(
|
|
48
|
-
errorMessage ?? `Value at index ${String(keyOrIndex)} is null.`
|
|
41
|
+
errorMessage ?? `Value at index ${String(keyOrIndex)} is undefined.`,
|
|
49
42
|
);
|
|
50
43
|
}
|
|
44
|
+
return value;
|
|
51
45
|
} else {
|
|
52
46
|
throw new Error(
|
|
53
|
-
errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds
|
|
47
|
+
errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`,
|
|
54
48
|
);
|
|
55
49
|
}
|
|
56
50
|
} else {
|
|
57
51
|
if (keyOrIndex in objOrArr) {
|
|
58
52
|
const value = objOrArr[keyOrIndex as K];
|
|
59
53
|
|
|
60
|
-
if (value
|
|
61
|
-
return value as NonNullable<T[K]>;
|
|
62
|
-
} else if (value === undefined) {
|
|
63
|
-
throw new Error(
|
|
64
|
-
errorMessage ?? `Value at key "${String(keyOrIndex)}" is undefined.`
|
|
65
|
-
);
|
|
66
|
-
} else {
|
|
54
|
+
if (value === undefined) {
|
|
67
55
|
throw new Error(
|
|
68
|
-
errorMessage ?? `Value at key "${String(keyOrIndex)}" is
|
|
56
|
+
errorMessage ?? `Value at key "${String(keyOrIndex)}" is undefined.`,
|
|
69
57
|
);
|
|
70
58
|
}
|
|
59
|
+
return value;
|
|
71
60
|
} else {
|
|
72
61
|
throw new Error(
|
|
73
62
|
errorMessage ??
|
|
74
|
-
`Key "${String(keyOrIndex)}" does not exist in the object
|
|
63
|
+
`Key "${String(keyOrIndex)}" does not exist in the object.`,
|
|
75
64
|
);
|
|
76
65
|
}
|
|
77
66
|
}
|