get-or-throw 2.0.0 → 2.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/.github/workflows/ci.yml +1 -1
- package/README.md +0 -80
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/package.json +1 -1
- package/src/get-or-throw.ts +1 -1
package/.github/workflows/ci.yml
CHANGED
package/README.md
CHANGED
|
@@ -68,83 +68,3 @@ const value = got(obj, "b"); // Output: null
|
|
|
68
68
|
const obj = { a: 1, b: undefined, c: 3 };
|
|
69
69
|
const value = got(obj, "b");
|
|
70
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.d.cts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* @throws An error if the key or index does not exist, or if the value is
|
|
10
10
|
* undefined.
|
|
11
11
|
*/
|
|
12
|
-
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T, keyOrIndex: K, errorMessage?: string): T[K]
|
|
12
|
+
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T, keyOrIndex: K, errorMessage?: string): NonNullable<T[K]>;
|
|
13
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
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* @throws An error if the key or index does not exist, or if the value is
|
|
10
10
|
* undefined.
|
|
11
11
|
*/
|
|
12
|
-
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T, keyOrIndex: K, errorMessage?: string): T[K]
|
|
12
|
+
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T, keyOrIndex: K, errorMessage?: string): NonNullable<T[K]>;
|
|
13
13
|
declare function getOrThrow<T>(objOrArr: T[], keyOrIndex: number, errorMessage?: string): T;
|
|
14
14
|
|
|
15
15
|
export { getOrThrow, getOrThrow as got };
|
package/package.json
CHANGED
package/src/get-or-throw.ts
CHANGED