get-or-throw 2.3.0 → 2.3.1
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/package.json +11 -5
- package/dist/index.cjs +0 -24
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -17
- package/dist/index.d.mts +0 -17
- package/dist/index.mjs +0 -22
- package/dist/index.mjs.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "get-or-throw",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"description": "A convenience function for safely getting values from dynamic objects and arrays",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"indexed",
|
|
@@ -19,12 +19,18 @@
|
|
|
19
19
|
"url": "git+https://github.com/0x80/get-or-throw.git"
|
|
20
20
|
},
|
|
21
21
|
"type": "module",
|
|
22
|
-
"main": "dist/index.cjs",
|
|
23
|
-
"types": "dist/index.d.
|
|
22
|
+
"main": "./dist/index.cjs",
|
|
23
|
+
"types": "./dist/index.d.cts",
|
|
24
24
|
"exports": {
|
|
25
25
|
".": {
|
|
26
|
-
"import":
|
|
27
|
-
|
|
26
|
+
"import": {
|
|
27
|
+
"types": "./dist/index.d.mts",
|
|
28
|
+
"default": "./dist/index.mjs"
|
|
29
|
+
},
|
|
30
|
+
"require": {
|
|
31
|
+
"types": "./dist/index.d.cts",
|
|
32
|
+
"default": "./dist/index.cjs"
|
|
33
|
+
}
|
|
28
34
|
}
|
|
29
35
|
},
|
|
30
36
|
"publishConfig": {
|
package/dist/index.cjs
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
//#region src/get-or-throw.ts
|
|
3
|
-
function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
4
|
-
if (Array.isArray(objOrArr)) {
|
|
5
|
-
const length = objOrArr.length;
|
|
6
|
-
let index = keyOrIndex;
|
|
7
|
-
/** Allow for negative indexing. */
|
|
8
|
-
if (index < 0) index = length + index;
|
|
9
|
-
if (index >= 0 && index < length) {
|
|
10
|
-
const value = objOrArr[index];
|
|
11
|
-
if (value === void 0) throw new Error(errorMessage ?? `Value at index ${String(keyOrIndex)} is undefined.`);
|
|
12
|
-
return value;
|
|
13
|
-
} else throw new Error(errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`);
|
|
14
|
-
} else if (keyOrIndex in objOrArr) {
|
|
15
|
-
const value = objOrArr[keyOrIndex];
|
|
16
|
-
if (value === void 0) throw new Error(errorMessage ?? `Value at key "${String(keyOrIndex)}" is undefined.`);
|
|
17
|
-
return value;
|
|
18
|
-
} else throw new Error(errorMessage ?? `Key "${String(keyOrIndex)}" does not exist in the object.`);
|
|
19
|
-
}
|
|
20
|
-
//#endregion
|
|
21
|
-
exports.getOrThrow = getOrThrow;
|
|
22
|
-
exports.got = getOrThrow;
|
|
23
|
-
|
|
24
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":[],"sources":["../src/get-or-throw.ts"],"sourcesContent":["/**\n * Get a value from an object or array, and throw an error if the key or index\n * does not exist or if the resulting value is undefined.\n *\n * @param objOrArr The object or array to get the value from.\n * @param keyOrIndex The key or index to get the value from.\n * @param errorMessage Optional error message to include in the error thrown.\n * @returns The value at the given key or index, guaranteed to be defined.\n * @throws An error if the key or index does not exist, or if the value is\n * undefined.\n */\nexport function getOrThrow<T extends object, K extends keyof T>(\n objOrArr: T,\n keyOrIndex: K,\n errorMessage?: string,\n): NonNullable<T[K]>;\nexport function getOrThrow<T>(\n objOrArr: T[],\n keyOrIndex: number,\n errorMessage?: string,\n): T;\nexport function getOrThrow<T extends object, K extends keyof T>(\n objOrArr: T | T[],\n keyOrIndex: K | number,\n errorMessage?: string,\n): T[K] | T {\n if (Array.isArray(objOrArr)) {\n const length = objOrArr.length;\n let index = keyOrIndex as number;\n\n /** Allow for negative indexing. */\n if (index < 0) {\n index = length + index;\n }\n\n if (index >= 0 && index < length) {\n const value = objOrArr[index];\n\n if (value === undefined) {\n throw new Error(\n errorMessage ?? `Value at index ${String(keyOrIndex)} is undefined.`,\n );\n }\n return value;\n } else {\n throw new Error(\n errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`,\n );\n }\n } else {\n if (keyOrIndex in objOrArr) {\n const value = objOrArr[keyOrIndex as K];\n\n if (value === undefined) {\n throw new Error(\n errorMessage ?? `Value at key \"${String(keyOrIndex)}\" is undefined.`,\n );\n }\n return value;\n } else {\n throw new Error(\n errorMessage ??\n `Key \"${String(keyOrIndex)}\" does not exist in the object.`,\n );\n }\n }\n}\n\n/** Export the same function under the alias 'got' */\nexport { getOrThrow as got };\n"],"mappings":";;AAqBA,SAAgB,WACd,UACA,YACA,cACU;AACV,KAAI,MAAM,QAAQ,SAAS,EAAE;EAC3B,MAAM,SAAS,SAAS;EACxB,IAAI,QAAQ;;AAGZ,MAAI,QAAQ,EACV,SAAQ,SAAS;AAGnB,MAAI,SAAS,KAAK,QAAQ,QAAQ;GAChC,MAAM,QAAQ,SAAS;AAEvB,OAAI,UAAU,KAAA,EACZ,OAAM,IAAI,MACR,gBAAgB,kBAAkB,OAAO,WAAW,CAAC,gBACtD;AAEH,UAAO;QAEP,OAAM,IAAI,MACR,gBAAgB,SAAS,OAAO,WAAW,CAAC,oBAC7C;YAGC,cAAc,UAAU;EAC1B,MAAM,QAAQ,SAAS;AAEvB,MAAI,UAAU,KAAA,EACZ,OAAM,IAAI,MACR,gBAAgB,iBAAiB,OAAO,WAAW,CAAC,iBACrD;AAEH,SAAO;OAEP,OAAM,IAAI,MACR,gBACE,QAAQ,OAAO,WAAW,CAAC,iCAC9B"}
|
package/dist/index.d.cts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
//#region src/get-or-throw.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Get a value from an object or array, and throw an error if the key or index
|
|
4
|
-
* does not exist or if the resulting value is undefined.
|
|
5
|
-
*
|
|
6
|
-
* @param objOrArr The object or array to get the value from.
|
|
7
|
-
* @param keyOrIndex The key or index to get the value from.
|
|
8
|
-
* @param errorMessage Optional error message to include in the error thrown.
|
|
9
|
-
* @returns The value at the given key or index, guaranteed to be defined.
|
|
10
|
-
* @throws An error if the key or index does not exist, or if the value is
|
|
11
|
-
* undefined.
|
|
12
|
-
*/
|
|
13
|
-
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T, keyOrIndex: K, errorMessage?: string): NonNullable<T[K]>;
|
|
14
|
-
declare function getOrThrow<T>(objOrArr: T[], keyOrIndex: number, errorMessage?: string): T;
|
|
15
|
-
//#endregion
|
|
16
|
-
export { getOrThrow, getOrThrow as got };
|
|
17
|
-
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.mts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
//#region src/get-or-throw.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Get a value from an object or array, and throw an error if the key or index
|
|
4
|
-
* does not exist or if the resulting value is undefined.
|
|
5
|
-
*
|
|
6
|
-
* @param objOrArr The object or array to get the value from.
|
|
7
|
-
* @param keyOrIndex The key or index to get the value from.
|
|
8
|
-
* @param errorMessage Optional error message to include in the error thrown.
|
|
9
|
-
* @returns The value at the given key or index, guaranteed to be defined.
|
|
10
|
-
* @throws An error if the key or index does not exist, or if the value is
|
|
11
|
-
* undefined.
|
|
12
|
-
*/
|
|
13
|
-
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T, keyOrIndex: K, errorMessage?: string): NonNullable<T[K]>;
|
|
14
|
-
declare function getOrThrow<T>(objOrArr: T[], keyOrIndex: number, errorMessage?: string): T;
|
|
15
|
-
//#endregion
|
|
16
|
-
export { getOrThrow, getOrThrow as got };
|
|
17
|
-
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
//#region src/get-or-throw.ts
|
|
2
|
-
function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
3
|
-
if (Array.isArray(objOrArr)) {
|
|
4
|
-
const length = objOrArr.length;
|
|
5
|
-
let index = keyOrIndex;
|
|
6
|
-
/** Allow for negative indexing. */
|
|
7
|
-
if (index < 0) index = length + index;
|
|
8
|
-
if (index >= 0 && index < length) {
|
|
9
|
-
const value = objOrArr[index];
|
|
10
|
-
if (value === void 0) throw new Error(errorMessage ?? `Value at index ${String(keyOrIndex)} is undefined.`);
|
|
11
|
-
return value;
|
|
12
|
-
} else throw new Error(errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`);
|
|
13
|
-
} else if (keyOrIndex in objOrArr) {
|
|
14
|
-
const value = objOrArr[keyOrIndex];
|
|
15
|
-
if (value === void 0) throw new Error(errorMessage ?? `Value at key "${String(keyOrIndex)}" is undefined.`);
|
|
16
|
-
return value;
|
|
17
|
-
} else throw new Error(errorMessage ?? `Key "${String(keyOrIndex)}" does not exist in the object.`);
|
|
18
|
-
}
|
|
19
|
-
//#endregion
|
|
20
|
-
export { getOrThrow, getOrThrow as got };
|
|
21
|
-
|
|
22
|
-
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/get-or-throw.ts"],"sourcesContent":["/**\n * Get a value from an object or array, and throw an error if the key or index\n * does not exist or if the resulting value is undefined.\n *\n * @param objOrArr The object or array to get the value from.\n * @param keyOrIndex The key or index to get the value from.\n * @param errorMessage Optional error message to include in the error thrown.\n * @returns The value at the given key or index, guaranteed to be defined.\n * @throws An error if the key or index does not exist, or if the value is\n * undefined.\n */\nexport function getOrThrow<T extends object, K extends keyof T>(\n objOrArr: T,\n keyOrIndex: K,\n errorMessage?: string,\n): NonNullable<T[K]>;\nexport function getOrThrow<T>(\n objOrArr: T[],\n keyOrIndex: number,\n errorMessage?: string,\n): T;\nexport function getOrThrow<T extends object, K extends keyof T>(\n objOrArr: T | T[],\n keyOrIndex: K | number,\n errorMessage?: string,\n): T[K] | T {\n if (Array.isArray(objOrArr)) {\n const length = objOrArr.length;\n let index = keyOrIndex as number;\n\n /** Allow for negative indexing. */\n if (index < 0) {\n index = length + index;\n }\n\n if (index >= 0 && index < length) {\n const value = objOrArr[index];\n\n if (value === undefined) {\n throw new Error(\n errorMessage ?? `Value at index ${String(keyOrIndex)} is undefined.`,\n );\n }\n return value;\n } else {\n throw new Error(\n errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`,\n );\n }\n } else {\n if (keyOrIndex in objOrArr) {\n const value = objOrArr[keyOrIndex as K];\n\n if (value === undefined) {\n throw new Error(\n errorMessage ?? `Value at key \"${String(keyOrIndex)}\" is undefined.`,\n );\n }\n return value;\n } else {\n throw new Error(\n errorMessage ??\n `Key \"${String(keyOrIndex)}\" does not exist in the object.`,\n );\n }\n }\n}\n\n/** Export the same function under the alias 'got' */\nexport { getOrThrow as got };\n"],"mappings":";AAqBA,SAAgB,WACd,UACA,YACA,cACU;AACV,KAAI,MAAM,QAAQ,SAAS,EAAE;EAC3B,MAAM,SAAS,SAAS;EACxB,IAAI,QAAQ;;AAGZ,MAAI,QAAQ,EACV,SAAQ,SAAS;AAGnB,MAAI,SAAS,KAAK,QAAQ,QAAQ;GAChC,MAAM,QAAQ,SAAS;AAEvB,OAAI,UAAU,KAAA,EACZ,OAAM,IAAI,MACR,gBAAgB,kBAAkB,OAAO,WAAW,CAAC,gBACtD;AAEH,UAAO;QAEP,OAAM,IAAI,MACR,gBAAgB,SAAS,OAAO,WAAW,CAAC,oBAC7C;YAGC,cAAc,UAAU;EAC1B,MAAM,QAAQ,SAAS;AAEvB,MAAI,UAAU,KAAA,EACZ,OAAM,IAAI,MACR,gBAAgB,iBAAiB,OAAO,WAAW,CAAC,iBACrD;AAEH,SAAO;OAEP,OAAM,IAAI,MACR,gBACE,QAAQ,OAAO,WAAW,CAAC,iCAC9B"}
|