get-or-throw 1.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/LICENSE +21 -0
- package/README.md +27 -0
- package/dist/index.cjs +48 -0
- package/dist/index.d.cts +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +21 -0
- package/dist/index.mjs +21 -0
- package/package.json +36 -0
- package/src/get-or-throw.ts +32 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +8 -0
- package/tsup.config.ts +8 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Thijs Koerselman
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# get-or-throw
|
|
2
|
+
|
|
3
|
+
A convenience function for adhering to Typescript's `noUncheckedIndexedAccess` setting.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add get-or-throw
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
...or use the equivalent for your package manager.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
const arr = [1, 2, 3];
|
|
17
|
+
console.log(getOrThrow(arr, 1)); // Output: 2
|
|
18
|
+
|
|
19
|
+
/** This will throw an error: "Index 3 is out of bounds." */
|
|
20
|
+
console.log(getOrThrow(arr, 3));
|
|
21
|
+
|
|
22
|
+
const obj = { a: 1, b: 2, c: 3 };
|
|
23
|
+
console.log(getOrThrow(obj, "b")); // Output: 2
|
|
24
|
+
|
|
25
|
+
/** This will throw an error: "Key "d" does not exist in the object." */
|
|
26
|
+
console.log(getOrThrow(obj, "d"));
|
|
27
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
getOrThrow: () => getOrThrow
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
|
|
27
|
+
// src/get-or-throw.ts
|
|
28
|
+
function getOrThrow(objOrArr, keyOrIndex) {
|
|
29
|
+
if (Array.isArray(objOrArr)) {
|
|
30
|
+
if (keyOrIndex in objOrArr) {
|
|
31
|
+
return objOrArr[keyOrIndex];
|
|
32
|
+
} else {
|
|
33
|
+
throw new Error(`Index ${String(keyOrIndex)} is out of bounds.`);
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
if (keyOrIndex in objOrArr) {
|
|
37
|
+
return objOrArr[keyOrIndex];
|
|
38
|
+
} else {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`Key "${String(keyOrIndex)}" does not exist in the object.`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
46
|
+
0 && (module.exports = {
|
|
47
|
+
getOrThrow
|
|
48
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get a value from an object or array, or throw an error if the key or index does not exist.
|
|
3
|
+
* @param objOrArr The object or array to get the value from.
|
|
4
|
+
* @param keyOrIndex The key or index to get the value from.
|
|
5
|
+
* @returns The value at the given key or index.
|
|
6
|
+
* @throws An error if the key or index does not exist.
|
|
7
|
+
*/
|
|
8
|
+
declare function getOrThrow<T extends object, K extends keyof T>(obj: T, key: K): T[K];
|
|
9
|
+
declare function getOrThrow<T>(arr: T[], index: number): T;
|
|
10
|
+
|
|
11
|
+
export { getOrThrow };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get a value from an object or array, or throw an error if the key or index does not exist.
|
|
3
|
+
* @param objOrArr The object or array to get the value from.
|
|
4
|
+
* @param keyOrIndex The key or index to get the value from.
|
|
5
|
+
* @returns The value at the given key or index.
|
|
6
|
+
* @throws An error if the key or index does not exist.
|
|
7
|
+
*/
|
|
8
|
+
declare function getOrThrow<T extends object, K extends keyof T>(obj: T, key: K): T[K];
|
|
9
|
+
declare function getOrThrow<T>(arr: T[], index: number): T;
|
|
10
|
+
|
|
11
|
+
export { getOrThrow };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/get-or-throw.ts
|
|
2
|
+
function getOrThrow(objOrArr, keyOrIndex) {
|
|
3
|
+
if (Array.isArray(objOrArr)) {
|
|
4
|
+
if (keyOrIndex in objOrArr) {
|
|
5
|
+
return objOrArr[keyOrIndex];
|
|
6
|
+
} else {
|
|
7
|
+
throw new Error(`Index ${String(keyOrIndex)} is out of bounds.`);
|
|
8
|
+
}
|
|
9
|
+
} else {
|
|
10
|
+
if (keyOrIndex in objOrArr) {
|
|
11
|
+
return objOrArr[keyOrIndex];
|
|
12
|
+
} else {
|
|
13
|
+
throw new Error(
|
|
14
|
+
`Key "${String(keyOrIndex)}" does not exist in the object.`
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export {
|
|
20
|
+
getOrThrow
|
|
21
|
+
};
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/get-or-throw.ts
|
|
2
|
+
function getOrThrow(objOrArr, keyOrIndex) {
|
|
3
|
+
if (Array.isArray(objOrArr)) {
|
|
4
|
+
if (keyOrIndex in objOrArr) {
|
|
5
|
+
return objOrArr[keyOrIndex];
|
|
6
|
+
} else {
|
|
7
|
+
throw new Error(`Index ${String(keyOrIndex)} is out of bounds.`);
|
|
8
|
+
}
|
|
9
|
+
} else {
|
|
10
|
+
if (keyOrIndex in objOrArr) {
|
|
11
|
+
return objOrArr[keyOrIndex];
|
|
12
|
+
} else {
|
|
13
|
+
throw new Error(
|
|
14
|
+
`Key "${String(keyOrIndex)}" does not exist in the object.`
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export {
|
|
20
|
+
getOrThrow
|
|
21
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "get-or-throw",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A convenience function for adhering to TS noUncheckedIndexedAccess",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.cjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.cjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/0x80/get-or-throw.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"noUncheckedIndexedAccess"
|
|
20
|
+
],
|
|
21
|
+
"author": "Thijs Koerselman",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/0x80/get-or-throw/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/0x80/get-or-throw#readme",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@codecompose/typescript-config": "^1.0.5",
|
|
29
|
+
"tsup": "^8.3.0",
|
|
30
|
+
"typescript": "^5.6.2"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsup",
|
|
34
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get a value from an object or array, or throw an error if the key or index does not exist.
|
|
3
|
+
* @param objOrArr The object or array to get the value from.
|
|
4
|
+
* @param keyOrIndex The key or index to get the value from.
|
|
5
|
+
* @returns The value at the given key or index.
|
|
6
|
+
* @throws An error if the key or index does not exist.
|
|
7
|
+
*/
|
|
8
|
+
export function getOrThrow<T extends object, K extends keyof T>(
|
|
9
|
+
obj: T,
|
|
10
|
+
key: K
|
|
11
|
+
): T[K];
|
|
12
|
+
export function getOrThrow<T>(arr: T[], index: number): T;
|
|
13
|
+
export function getOrThrow<T extends object, K extends keyof T>(
|
|
14
|
+
objOrArr: T | T[],
|
|
15
|
+
keyOrIndex: K | number
|
|
16
|
+
): T[K] | T {
|
|
17
|
+
if (Array.isArray(objOrArr)) {
|
|
18
|
+
if (keyOrIndex in objOrArr) {
|
|
19
|
+
return objOrArr[keyOrIndex as number]!;
|
|
20
|
+
} else {
|
|
21
|
+
throw new Error(`Index ${String(keyOrIndex)} is out of bounds.`);
|
|
22
|
+
}
|
|
23
|
+
} else {
|
|
24
|
+
if (keyOrIndex in objOrArr) {
|
|
25
|
+
return objOrArr[keyOrIndex as K];
|
|
26
|
+
} else {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`Key "${String(keyOrIndex)}" does not exist in the object.`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./get-or-throw";
|
package/tsconfig.json
ADDED