get-or-throw 1.1.0 → 1.2.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/README.md CHANGED
@@ -3,6 +3,14 @@
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
+ - Zero dependencies.
13
+
6
14
  ## Installation
7
15
 
8
16
  ```bash
@@ -17,6 +25,10 @@ pnpm add get-or-throw
17
25
  const arr = [1, 2, 3];
18
26
  console.log(getOrThrow(arr, 1)); // Output: 2
19
27
 
28
+ /** Support for negative indexing */
29
+ const arr = [1, 2, 3];
30
+ console.log(getOrThrow(arr, -1)); // Output: 3
31
+
20
32
  /** This will throw an error: "Index 3 is out of bounds." */
21
33
  console.log(getOrThrow(arr, 3));
22
34
 
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
- if (keyOrIndex in objOrArr) {
31
- return objOrArr[keyOrIndex];
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.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
- if (keyOrIndex in objOrArr) {
5
- return objOrArr[keyOrIndex];
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "get-or-throw",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "A convenience function for adhering to Typescript's noUncheckedIndexedAccess rule",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -24,8 +24,16 @@ export function getOrThrow<T extends object, K extends keyof T>(
24
24
  errorMessage?: string
25
25
  ): T[K] | T {
26
26
  if (Array.isArray(objOrArr)) {
27
- if (keyOrIndex in objOrArr) {
28
- return objOrArr[keyOrIndex as number]!;
27
+ const length = objOrArr.length;
28
+ let index = keyOrIndex as number;
29
+
30
+ // Handle negative indexing
31
+ if (index < 0) {
32
+ index = length + index;
33
+ }
34
+
35
+ if (index >= 0 && index < length) {
36
+ return objOrArr[index]!;
29
37
  } else {
30
38
  throw new Error(
31
39
  errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`