get-or-throw 1.1.0 → 1.2.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/README.md +11 -0
- package/dist/index.cjs +7 -2
- package/dist/index.d.cts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +7 -2
- package/package.json +1 -1
- package/src/get-or-throw.ts +10 -12
package/README.md
CHANGED
|
@@ -3,6 +3,13 @@
|
|
|
3
3
|
A convenience function for adhering to Typescript's `noUncheckedIndexedAccess`
|
|
4
4
|
setting.
|
|
5
5
|
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
- Uses Typescript assertions for type narrowing.
|
|
9
|
+
- Works with both objects and arrays.
|
|
10
|
+
- Supports negative indexing for arrays.
|
|
11
|
+
- Allows for custom error messages.
|
|
12
|
+
|
|
6
13
|
## Installation
|
|
7
14
|
|
|
8
15
|
```bash
|
|
@@ -17,6 +24,10 @@ pnpm add get-or-throw
|
|
|
17
24
|
const arr = [1, 2, 3];
|
|
18
25
|
console.log(getOrThrow(arr, 1)); // Output: 2
|
|
19
26
|
|
|
27
|
+
/** Support for negative indexing */
|
|
28
|
+
const arr = [1, 2, 3];
|
|
29
|
+
console.log(getOrThrow(arr, -1)); // Output: 3
|
|
30
|
+
|
|
20
31
|
/** This will throw an error: "Index 3 is out of bounds." */
|
|
21
32
|
console.log(getOrThrow(arr, 3));
|
|
22
33
|
|
package/dist/index.cjs
CHANGED
|
@@ -27,8 +27,13 @@ module.exports = __toCommonJS(src_exports);
|
|
|
27
27
|
// src/get-or-throw.ts
|
|
28
28
|
function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
29
29
|
if (Array.isArray(objOrArr)) {
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
const length = objOrArr.length;
|
|
31
|
+
let index = keyOrIndex;
|
|
32
|
+
if (index < 0) {
|
|
33
|
+
index = length + index;
|
|
34
|
+
}
|
|
35
|
+
if (index >= 0 && index < length) {
|
|
36
|
+
return objOrArr[index];
|
|
32
37
|
} else {
|
|
33
38
|
throw new Error(
|
|
34
39
|
errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`
|
package/dist/index.d.cts
CHANGED
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
* @returns The value at the given key or index.
|
|
9
9
|
* @throws An error if the key or index does not exist.
|
|
10
10
|
*/
|
|
11
|
-
declare function getOrThrow<T extends object, K extends keyof T>(
|
|
12
|
-
declare function getOrThrow<T>(arr: T[], index: number, errorMessage?: string): T;
|
|
11
|
+
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T | T[], keyOrIndex: K | number, errorMessage?: string): T[K] | T;
|
|
13
12
|
|
|
14
13
|
export { getOrThrow };
|
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
* @returns The value at the given key or index.
|
|
9
9
|
* @throws An error if the key or index does not exist.
|
|
10
10
|
*/
|
|
11
|
-
declare function getOrThrow<T extends object, K extends keyof T>(
|
|
12
|
-
declare function getOrThrow<T>(arr: T[], index: number, errorMessage?: string): T;
|
|
11
|
+
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T | T[], keyOrIndex: K | number, errorMessage?: string): T[K] | T;
|
|
13
12
|
|
|
14
13
|
export { getOrThrow };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
// src/get-or-throw.ts
|
|
2
2
|
function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
3
3
|
if (Array.isArray(objOrArr)) {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const length = objOrArr.length;
|
|
5
|
+
let index = keyOrIndex;
|
|
6
|
+
if (index < 0) {
|
|
7
|
+
index = length + index;
|
|
8
|
+
}
|
|
9
|
+
if (index >= 0 && index < length) {
|
|
10
|
+
return objOrArr[index];
|
|
6
11
|
} else {
|
|
7
12
|
throw new Error(
|
|
8
13
|
errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`
|
package/package.json
CHANGED
package/src/get-or-throw.ts
CHANGED
|
@@ -8,24 +8,22 @@
|
|
|
8
8
|
* @returns The value at the given key or index.
|
|
9
9
|
* @throws An error if the key or index does not exist.
|
|
10
10
|
*/
|
|
11
|
-
export function getOrThrow<T extends object, K extends keyof T>(
|
|
12
|
-
obj: T,
|
|
13
|
-
key: K,
|
|
14
|
-
errorMessage?: string
|
|
15
|
-
): T[K];
|
|
16
|
-
export function getOrThrow<T>(
|
|
17
|
-
arr: T[],
|
|
18
|
-
index: number,
|
|
19
|
-
errorMessage?: string
|
|
20
|
-
): T;
|
|
21
11
|
export function getOrThrow<T extends object, K extends keyof T>(
|
|
22
12
|
objOrArr: T | T[],
|
|
23
13
|
keyOrIndex: K | number,
|
|
24
14
|
errorMessage?: string
|
|
25
15
|
): T[K] | T {
|
|
26
16
|
if (Array.isArray(objOrArr)) {
|
|
27
|
-
|
|
28
|
-
|
|
17
|
+
const length = objOrArr.length;
|
|
18
|
+
let index = keyOrIndex as number;
|
|
19
|
+
|
|
20
|
+
// Handle negative indexing
|
|
21
|
+
if (index < 0) {
|
|
22
|
+
index = length + index;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (index >= 0 && index < length) {
|
|
26
|
+
return objOrArr[index]!;
|
|
29
27
|
} else {
|
|
30
28
|
throw new Error(
|
|
31
29
|
errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`
|