get-or-throw 1.5.1 → 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.
@@ -6,6 +6,10 @@ on:
6
6
  pull_request:
7
7
  branches: [main]
8
8
 
9
+ concurrency:
10
+ group: ${{ github.workflow }}-${{ github.ref }}
11
+ cancel-in-progress: true
12
+
9
13
  jobs:
10
14
  verify:
11
15
  runs-on: ubuntu-latest
@@ -22,12 +26,10 @@ jobs:
22
26
  - name: Setup Node.js
23
27
  uses: actions/setup-node@v4
24
28
  with:
25
- node-version: "22"
29
+ node-version: 22
26
30
 
27
- - name: Setup pnpm
28
- uses: pnpm/action-setup@v2
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` or `null`. Optionally, you can set custom
6
- error message.
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,99 @@ const value = got(obj, "d");
52
52
  const key = "d";
53
53
  const value = got(obj, key, `Failed to find ${key}`);
54
54
 
55
- /** This will throw an error: "Value at index 1 is undefined or null." */
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 or null." */
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
- /** This will throw an error: "Value at key 'b' is undefined or null." */
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
  ```
71
+
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
@@ -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 !== void 0 && value !== null) {
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 or null.
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 non-null and
9
- * non-undefined.
10
- * @throws An error if the key or index does not exist, or if the resulting
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 | (T | null)[], keyOrIndex: K | number, errorMessage?: string): NonNullable<T[K]> | NonNullable<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 or null.
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 non-null and
9
- * non-undefined.
10
- * @throws An error if the key or index does not exist, or if the resulting
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 | (T | null)[], keyOrIndex: K | number, errorMessage?: string): NonNullable<T[K]> | NonNullable<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 !== void 0 && value !== null) {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "get-or-throw",
3
- "version": "1.5.1",
3
+ "version": "2.0.0",
4
4
  "description": "A convenience function for safely getting values from dynamic objects and arrays",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -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 throw on null values", () => {
23
+ it("should allow null values", () => {
24
24
  const arr = [1, null, 3];
25
- expect(() => got(arr, 1)).toThrow("Value at index 1 is null.");
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 throw on null values", () => {
48
+ it("should allow null values", () => {
49
49
  const obj = { a: 1, b: null, c: 3 };
50
- expect(() => got(obj, "b")).toThrow('Value at key "b" is null.');
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(() => got(obj, "b")).toThrow('Value at key "b" is undefined.');
55
+ expect(() => {
56
+ got(obj, "b");
57
+ }).toThrow('Value at key "b" is undefined.');
56
58
  });
57
59
  });
58
60
 
@@ -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 or null.
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 non-null and
9
- * non-undefined.
10
- * @throws An error if the key or index does not exist, or if the resulting
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 | (T | null)[],
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
- ): NonNullable<T[K]> | NonNullable<T> {
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 !== undefined && value !== null) {
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 ??