get-or-throw 2.2.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "get-or-throw",
3
- "version": "2.2.0",
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.ts",
22
+ "main": "./dist/index.cjs",
23
+ "types": "./dist/index.d.cts",
24
24
  "exports": {
25
25
  ".": {
26
- "import": "./dist/index.js",
27
- "require": "./dist/index.cjs"
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": {
@@ -45,16 +51,16 @@
45
51
  "docs:preview": "vitepress preview docs"
46
52
  },
47
53
  "devDependencies": {
48
- "@codecompose/typescript-config": "^3.0.0",
49
- "del-cli": "^5.1.0",
50
- "oxfmt": "^0.33.0",
51
- "oxlint": "^1.48.0",
52
- "oxlint-tsgolint": "^0.15.0",
53
- "tsdown": "^0.12.0",
54
- "typescript": "6.0.0-beta",
55
- "vite": "^6.0.0",
54
+ "@codecompose/typescript-config": "^3.1.0",
55
+ "del-cli": "^7.0.0",
56
+ "oxfmt": "^0.43.0",
57
+ "oxlint": "^1.58.0",
58
+ "oxlint-tsgolint": "^0.20.0",
59
+ "tsdown": "^0.21.7",
60
+ "typescript": "6.0.2",
61
+ "vite": "^8.0.5",
56
62
  "vitepress": "^1.6.0",
57
- "vitest": "^3.0.8"
63
+ "vitest": "^4.1.2"
58
64
  },
59
65
  "engines": {
60
66
  "node": ">=22.12.0"
@@ -1,19 +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
- /** Export the same function under the alias 'got' */
16
-
17
- //#endregion
18
- export { getOrThrow, getOrThrow as got };
19
- //# sourceMappingURL=index-CLvtl5bd.d.ts.map
@@ -1,19 +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
- /** Export the same function under the alias 'got' */
16
-
17
- //#endregion
18
- export { getOrThrow, getOrThrow as got };
19
- //# sourceMappingURL=index-CRoiksIS.d.cts.map
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
@@ -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.js 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.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","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"}