get-or-throw 2.0.1 → 2.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/.editorconfig +33 -0
- package/.github/workflows/{ci.yml → checks.yml} +12 -9
- package/.github/workflows/docs.yml +74 -0
- package/.github/workflows/publish.yml +157 -0
- package/.oxfmtignore +2 -0
- package/.oxfmtrc.jsonc +12 -0
- package/.oxlintrc.json +18 -0
- package/README.md +17 -48
- package/dist/{index.d.ts → index-CLvtl5bd.d.ts} +5 -1
- package/dist/{index.d.cts → index-CRoiksIS.d.cts} +5 -1
- package/dist/index.cjs +22 -67
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +20 -39
- package/dist/index.js.map +1 -0
- package/docs/.vitepress/config.mts +47 -0
- package/docs/.vitepress/theme/CustomLayout.vue +31 -0
- package/docs/.vitepress/theme/custom.css +3 -0
- package/docs/.vitepress/theme/index.mts +8 -0
- package/docs/api.md +65 -0
- package/docs/getting-started.md +53 -0
- package/docs/index.md +46 -0
- package/docs/reasoning.md +144 -0
- package/docs/usage.md +85 -0
- package/package.json +45 -32
- package/src/get-or-throw.ts +1 -1
- package/tsconfig.json +1 -4
- package/tsdown.config.ts +10 -0
- package/.prettierignore +0 -1
- package/.prettierrc.json +0 -4
- package/dist/index.mjs +0 -21
- package/eslint.config.js +0 -28
- package/tsup.config.ts +0 -8
package/.editorconfig
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Defines the coding style for different editors and IDEs.
|
|
2
|
+
# https://editorconfig.org/
|
|
3
|
+
|
|
4
|
+
# top-most EditorConfig file
|
|
5
|
+
root = true
|
|
6
|
+
|
|
7
|
+
# Rules for source code.
|
|
8
|
+
[*]
|
|
9
|
+
charset = utf-8
|
|
10
|
+
end_of_line = lf
|
|
11
|
+
trim_trailing_whitespace = true
|
|
12
|
+
insert_final_newline = true
|
|
13
|
+
indent_style = space
|
|
14
|
+
indent_size = 2
|
|
15
|
+
max_line_length = 80
|
|
16
|
+
|
|
17
|
+
[*.{py,pyi}]
|
|
18
|
+
indent_size = 4
|
|
19
|
+
|
|
20
|
+
# Documentation.
|
|
21
|
+
[*.md]
|
|
22
|
+
max_line_length = 0
|
|
23
|
+
trim_trailing_whitespace = false
|
|
24
|
+
|
|
25
|
+
# Git commit messages.
|
|
26
|
+
[COMMIT_EDITMSG]
|
|
27
|
+
max_line_length = 0
|
|
28
|
+
trim_trailing_whitespace = false
|
|
29
|
+
|
|
30
|
+
# Makefiles require tabs.
|
|
31
|
+
[Makefile]
|
|
32
|
+
indent_style = tab
|
|
33
|
+
indent_size = 8
|
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
name:
|
|
1
|
+
name: Checks
|
|
2
2
|
|
|
3
3
|
on:
|
|
4
4
|
push:
|
|
5
5
|
branches: [main]
|
|
6
6
|
pull_request:
|
|
7
7
|
branches: ["**"]
|
|
8
|
+
workflow_call:
|
|
8
9
|
|
|
9
10
|
concurrency:
|
|
10
|
-
|
|
11
|
+
# Skip concurrency restriction when called from another workflow
|
|
12
|
+
# This prevents deadlocks between parent workflow and this one
|
|
13
|
+
group:
|
|
14
|
+
${{ github.event_name == 'workflow_call' && format('{0}-{1}', github.workflow,
|
|
15
|
+
github.run_id) || format('{0}-{1}', github.workflow, github.ref) }}
|
|
11
16
|
cancel-in-progress: true
|
|
12
17
|
|
|
13
18
|
jobs:
|
|
@@ -16,8 +21,7 @@ jobs:
|
|
|
16
21
|
strategy:
|
|
17
22
|
fail-fast: false
|
|
18
23
|
matrix:
|
|
19
|
-
command: [
|
|
20
|
-
|
|
24
|
+
command: [check-types, check-format, check-lint, test]
|
|
21
25
|
name: ${{ matrix.command }}
|
|
22
26
|
|
|
23
27
|
steps:
|
|
@@ -26,7 +30,7 @@ jobs:
|
|
|
26
30
|
- name: Setup Node.js
|
|
27
31
|
uses: actions/setup-node@v4
|
|
28
32
|
with:
|
|
29
|
-
node-version:
|
|
33
|
+
node-version: 24
|
|
30
34
|
|
|
31
35
|
- name: Enable corepack
|
|
32
36
|
run: corepack enable pnpm
|
|
@@ -42,15 +46,14 @@ jobs:
|
|
|
42
46
|
with:
|
|
43
47
|
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
|
|
44
48
|
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
|
|
45
|
-
# First tries exact match with lock file hash. If not found,
|
|
46
|
-
# falls back to any cache starting with 'pnpm-store-'.
|
|
47
|
-
# This way we get exact cache on repeated runs, but can still
|
|
48
|
-
# use older cache as starting point when dependencies change.
|
|
49
49
|
restore-keys: |
|
|
50
50
|
${{ runner.os }}-pnpm-store-
|
|
51
51
|
|
|
52
52
|
- name: Install dependencies
|
|
53
53
|
run: pnpm install --frozen-lockfile
|
|
54
54
|
|
|
55
|
+
- name: Build
|
|
56
|
+
run: pnpm build
|
|
57
|
+
|
|
55
58
|
- name: Run ${{ matrix.command }}
|
|
56
59
|
run: pnpm ${{ matrix.command }}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
name: Deploy Docs
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
paths:
|
|
7
|
+
- "docs/**"
|
|
8
|
+
- "package.json"
|
|
9
|
+
- "pnpm-lock.yaml"
|
|
10
|
+
- ".github/workflows/docs.yml"
|
|
11
|
+
|
|
12
|
+
permissions:
|
|
13
|
+
contents: read
|
|
14
|
+
pages: write
|
|
15
|
+
id-token: write
|
|
16
|
+
|
|
17
|
+
concurrency:
|
|
18
|
+
group: pages
|
|
19
|
+
cancel-in-progress: false
|
|
20
|
+
|
|
21
|
+
jobs:
|
|
22
|
+
build:
|
|
23
|
+
runs-on: ubuntu-latest
|
|
24
|
+
|
|
25
|
+
steps:
|
|
26
|
+
- uses: actions/checkout@v4
|
|
27
|
+
|
|
28
|
+
- name: Setup Node.js
|
|
29
|
+
uses: actions/setup-node@v4
|
|
30
|
+
with:
|
|
31
|
+
node-version: 24
|
|
32
|
+
|
|
33
|
+
- name: Enable corepack
|
|
34
|
+
run: corepack enable pnpm
|
|
35
|
+
|
|
36
|
+
- name: Get pnpm store directory
|
|
37
|
+
id: pnpm-cache
|
|
38
|
+
shell: bash
|
|
39
|
+
run: |
|
|
40
|
+
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
|
|
41
|
+
|
|
42
|
+
- name: Setup pnpm cache
|
|
43
|
+
uses: actions/cache@v4
|
|
44
|
+
with:
|
|
45
|
+
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
|
|
46
|
+
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
|
|
47
|
+
restore-keys: |
|
|
48
|
+
${{ runner.os }}-pnpm-store-
|
|
49
|
+
|
|
50
|
+
- name: Install dependencies
|
|
51
|
+
run: pnpm install --frozen-lockfile
|
|
52
|
+
|
|
53
|
+
- name: Build docs
|
|
54
|
+
run: pnpm docs:build
|
|
55
|
+
|
|
56
|
+
- name: Setup Pages
|
|
57
|
+
uses: actions/configure-pages@v5
|
|
58
|
+
|
|
59
|
+
- name: Upload artifact
|
|
60
|
+
uses: actions/upload-pages-artifact@v3
|
|
61
|
+
with:
|
|
62
|
+
path: docs/.vitepress/dist
|
|
63
|
+
|
|
64
|
+
deploy:
|
|
65
|
+
environment:
|
|
66
|
+
name: github-pages
|
|
67
|
+
url: ${{ steps.deployment.outputs.page_url }}
|
|
68
|
+
needs: build
|
|
69
|
+
runs-on: ubuntu-latest
|
|
70
|
+
|
|
71
|
+
steps:
|
|
72
|
+
- name: Deploy to GitHub Pages
|
|
73
|
+
id: deployment
|
|
74
|
+
uses: actions/deploy-pages@v4
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_dispatch:
|
|
5
|
+
inputs:
|
|
6
|
+
bump:
|
|
7
|
+
description: "Version bump type"
|
|
8
|
+
required: true
|
|
9
|
+
type: choice
|
|
10
|
+
options:
|
|
11
|
+
- patch
|
|
12
|
+
- minor
|
|
13
|
+
- major
|
|
14
|
+
- prepatch
|
|
15
|
+
- preminor
|
|
16
|
+
- premajor
|
|
17
|
+
- prerelease
|
|
18
|
+
preid:
|
|
19
|
+
description: "Prerelease identifier (alpha, beta, rc). Only used with pre* bumps."
|
|
20
|
+
required: false
|
|
21
|
+
type: string
|
|
22
|
+
explicit_version:
|
|
23
|
+
description: "Explicit version (e.g. 2.0.0-beta.1). Overrides bump and preid when set."
|
|
24
|
+
required: false
|
|
25
|
+
type: string
|
|
26
|
+
dry_run:
|
|
27
|
+
description: "Dry run — skip publish, push, and release"
|
|
28
|
+
required: false
|
|
29
|
+
type: boolean
|
|
30
|
+
default: false
|
|
31
|
+
|
|
32
|
+
concurrency:
|
|
33
|
+
group: publish
|
|
34
|
+
cancel-in-progress: false
|
|
35
|
+
|
|
36
|
+
jobs:
|
|
37
|
+
checks:
|
|
38
|
+
if: github.ref == 'refs/heads/main' || startsWith(inputs.bump, 'pre') || inputs.explicit_version != ''
|
|
39
|
+
uses: ./.github/workflows/checks.yml
|
|
40
|
+
|
|
41
|
+
publish:
|
|
42
|
+
if: github.ref == 'refs/heads/main' || startsWith(inputs.bump, 'pre') || inputs.explicit_version != ''
|
|
43
|
+
needs: checks
|
|
44
|
+
runs-on: ubuntu-latest
|
|
45
|
+
# Authentication is handled via OIDC trusted publishing (id-token),
|
|
46
|
+
# so no NPM_TOKEN secret is needed.
|
|
47
|
+
permissions:
|
|
48
|
+
contents: write
|
|
49
|
+
id-token: write
|
|
50
|
+
|
|
51
|
+
steps:
|
|
52
|
+
- uses: actions/checkout@v4
|
|
53
|
+
|
|
54
|
+
- name: Setup Node.js
|
|
55
|
+
uses: actions/setup-node@v4
|
|
56
|
+
with:
|
|
57
|
+
node-version: 24
|
|
58
|
+
registry-url: "https://registry.npmjs.org"
|
|
59
|
+
|
|
60
|
+
- name: Enable corepack
|
|
61
|
+
run: corepack enable pnpm
|
|
62
|
+
|
|
63
|
+
- name: Get pnpm store directory
|
|
64
|
+
id: pnpm-cache
|
|
65
|
+
shell: bash
|
|
66
|
+
run: |
|
|
67
|
+
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
|
|
68
|
+
|
|
69
|
+
- name: Setup pnpm cache
|
|
70
|
+
uses: actions/cache@v4
|
|
71
|
+
with:
|
|
72
|
+
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
|
|
73
|
+
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
|
|
74
|
+
restore-keys: |
|
|
75
|
+
${{ runner.os }}-pnpm-store-
|
|
76
|
+
|
|
77
|
+
- name: Install dependencies
|
|
78
|
+
run: pnpm install --frozen-lockfile
|
|
79
|
+
|
|
80
|
+
- name: Build
|
|
81
|
+
run: pnpm build
|
|
82
|
+
|
|
83
|
+
- name: Configure git
|
|
84
|
+
run: |
|
|
85
|
+
git config user.name "github-actions[bot]"
|
|
86
|
+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
|
|
87
|
+
|
|
88
|
+
- name: Bump version
|
|
89
|
+
id: version
|
|
90
|
+
env:
|
|
91
|
+
BUMP: ${{ inputs.bump }}
|
|
92
|
+
PREID: ${{ inputs.preid }}
|
|
93
|
+
EXPLICIT_VERSION: ${{ inputs.explicit_version }}
|
|
94
|
+
run: |
|
|
95
|
+
if [[ -n "$EXPLICIT_VERSION" ]]; then
|
|
96
|
+
npm version "$EXPLICIT_VERSION" --git-tag-version true
|
|
97
|
+
else
|
|
98
|
+
ARGS=("$BUMP")
|
|
99
|
+
if [[ "$BUMP" == pre* && -n "$PREID" ]]; then
|
|
100
|
+
ARGS+=(--preid "$PREID")
|
|
101
|
+
fi
|
|
102
|
+
npm version "${ARGS[@]}" --git-tag-version true
|
|
103
|
+
fi
|
|
104
|
+
|
|
105
|
+
VERSION=$(node -p "require('./package.json').version")
|
|
106
|
+
TAG="v${VERSION}"
|
|
107
|
+
|
|
108
|
+
echo "version=${VERSION}" >> $GITHUB_OUTPUT
|
|
109
|
+
echo "tag=${TAG}" >> $GITHUB_OUTPUT
|
|
110
|
+
|
|
111
|
+
if [[ "$VERSION" == *-* ]]; then
|
|
112
|
+
echo "dist_tag=next" >> $GITHUB_OUTPUT
|
|
113
|
+
echo "prerelease=true" >> $GITHUB_OUTPUT
|
|
114
|
+
else
|
|
115
|
+
echo "dist_tag=latest" >> $GITHUB_OUTPUT
|
|
116
|
+
echo "prerelease=false" >> $GITHUB_OUTPUT
|
|
117
|
+
fi
|
|
118
|
+
|
|
119
|
+
- name: Publish summary
|
|
120
|
+
run: |
|
|
121
|
+
echo "### Publish Summary" >> $GITHUB_STEP_SUMMARY
|
|
122
|
+
echo "" >> $GITHUB_STEP_SUMMARY
|
|
123
|
+
echo "- **Version:** ${{ steps.version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
|
|
124
|
+
echo "- **Tag:** ${{ steps.version.outputs.tag }}" >> $GITHUB_STEP_SUMMARY
|
|
125
|
+
echo "- **Dist tag:** ${{ steps.version.outputs.dist_tag }}" >> $GITHUB_STEP_SUMMARY
|
|
126
|
+
echo "- **Prerelease:** ${{ steps.version.outputs.prerelease }}" >> $GITHUB_STEP_SUMMARY
|
|
127
|
+
echo "- **Dry run:** ${{ inputs.dry_run }}" >> $GITHUB_STEP_SUMMARY
|
|
128
|
+
|
|
129
|
+
# Publish before push: npm publish is not retryable (same version
|
|
130
|
+
# can't be published twice), while git push is idempotent. If push
|
|
131
|
+
# fails after a successful publish, it can simply be retried manually.
|
|
132
|
+
- name: Publish to npm
|
|
133
|
+
if: ${{ inputs.dry_run == false }}
|
|
134
|
+
run: |
|
|
135
|
+
npm publish --provenance --tag ${{ steps.version.outputs.dist_tag }}
|
|
136
|
+
|
|
137
|
+
- name: Push version commit
|
|
138
|
+
if: ${{ inputs.dry_run == false }}
|
|
139
|
+
run: git push origin HEAD
|
|
140
|
+
|
|
141
|
+
- name: Push version tag
|
|
142
|
+
if: ${{ inputs.dry_run == false }}
|
|
143
|
+
run: git push origin ${{ steps.version.outputs.tag }}
|
|
144
|
+
|
|
145
|
+
- name: Create GitHub Release
|
|
146
|
+
if: ${{ inputs.dry_run == false }}
|
|
147
|
+
run: |
|
|
148
|
+
PRERELEASE_FLAG=""
|
|
149
|
+
if [[ "${{ steps.version.outputs.prerelease }}" == "true" ]]; then
|
|
150
|
+
PRERELEASE_FLAG="--prerelease"
|
|
151
|
+
fi
|
|
152
|
+
|
|
153
|
+
gh release create "${{ steps.version.outputs.tag }}" \
|
|
154
|
+
--generate-notes \
|
|
155
|
+
$PRERELEASE_FLAG
|
|
156
|
+
env:
|
|
157
|
+
GH_TOKEN: ${{ github.token }}
|
package/.oxfmtignore
ADDED
package/.oxfmtrc.jsonc
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./node_modules/oxfmt/configuration_schema.json",
|
|
3
|
+
"printWidth": 80,
|
|
4
|
+
"tabWidth": 2,
|
|
5
|
+
"useTabs": false,
|
|
6
|
+
"semi": true,
|
|
7
|
+
"singleQuote": false,
|
|
8
|
+
"trailingComma": "all",
|
|
9
|
+
"bracketSpacing": true,
|
|
10
|
+
"arrowParens": "always",
|
|
11
|
+
"endOfLine": "lf",
|
|
12
|
+
}
|
package/.oxlintrc.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./node_modules/oxlint/configuration_schema.json",
|
|
3
|
+
"plugins": ["typescript"],
|
|
4
|
+
"categories": {
|
|
5
|
+
"correctness": "error",
|
|
6
|
+
"suspicious": "warn",
|
|
7
|
+
"perf": "warn"
|
|
8
|
+
},
|
|
9
|
+
"rules": {
|
|
10
|
+
"typescript/no-floating-promises": "error",
|
|
11
|
+
"typescript/no-misused-promises": "error",
|
|
12
|
+
"typescript/await-thenable": "error",
|
|
13
|
+
"typescript/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }],
|
|
14
|
+
"typescript/no-unsafe-type-assertion": "off",
|
|
15
|
+
"no-await-in-loop": "off"
|
|
16
|
+
},
|
|
17
|
+
"ignorePatterns": ["*.d.ts", "*.d.mts"]
|
|
18
|
+
}
|
package/README.md
CHANGED
|
@@ -1,70 +1,39 @@
|
|
|
1
|
-
# Get-Or-Throw
|
|
1
|
+
# Get-Or-Throw
|
|
2
2
|
|
|
3
3
|
A convenience function for safely accessing values in dynamic objects and
|
|
4
4
|
arrays. It gets the value at a specified key or index, and throws an error if
|
|
5
|
-
the resulting value is `undefined`.
|
|
6
|
-
message.
|
|
7
|
-
|
|
8
|
-
This was created to make it easy to adhere to Typescript's
|
|
9
|
-
[noUncheckedIndexedAccess](https://www.typescriptlang.org/tsconfig/#noUncheckedIndexedAccess)
|
|
10
|
-
setting, which is recommended for strict type checking.
|
|
5
|
+
the resulting value is `undefined`.
|
|
11
6
|
|
|
12
7
|
## Features
|
|
13
8
|
|
|
14
|
-
-
|
|
15
|
-
- Works with both objects and arrays
|
|
16
|
-
- Supports negative indexing for arrays
|
|
17
|
-
- Allows for custom error messages
|
|
18
|
-
- Zero dependencies
|
|
9
|
+
- TypeScript assertions for type narrowing
|
|
10
|
+
- Works with both objects and arrays
|
|
11
|
+
- Supports negative indexing for arrays
|
|
12
|
+
- Allows for custom error messages
|
|
13
|
+
- Zero dependencies
|
|
19
14
|
- Provides `got` as alias for `getOrThrow`
|
|
20
15
|
|
|
21
|
-
##
|
|
16
|
+
## Quick Start
|
|
22
17
|
|
|
23
18
|
```bash
|
|
24
19
|
pnpm add get-or-throw
|
|
25
20
|
```
|
|
26
21
|
|
|
27
|
-
...or use the equivalent for your package manager.
|
|
28
|
-
|
|
29
|
-
## Usage
|
|
30
|
-
|
|
31
|
-
The example code below uses the `got` alias but `getOrThrow` is also available
|
|
32
|
-
if you want to be more explicit.
|
|
33
|
-
|
|
34
22
|
```ts
|
|
35
|
-
|
|
36
|
-
const value = got(arr, 1); // Output: 2
|
|
23
|
+
import { got } from "get-or-throw";
|
|
37
24
|
|
|
38
|
-
/** Support for negative indexing */
|
|
39
25
|
const arr = [1, 2, 3];
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
/** This will throw an error: "Index 3 is out of bounds." */
|
|
43
|
-
const value = got(arr, 3);
|
|
26
|
+
got(arr, 1); // 2
|
|
44
27
|
|
|
45
28
|
const obj = { a: 1, b: 2, c: 3 };
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
/** This will throw an error: "Key "d" does not exist in the object." */
|
|
49
|
-
const value = got(obj, "d");
|
|
50
|
-
|
|
51
|
-
/** This will throw an error: "Failed to find d" */
|
|
52
|
-
const key = "d";
|
|
53
|
-
const value = got(obj, key, `Failed to find ${key}`);
|
|
29
|
+
got(obj, "b"); // 2
|
|
30
|
+
```
|
|
54
31
|
|
|
55
|
-
|
|
56
|
-
const arr = [1, null, 3];
|
|
57
|
-
const value = got(arr, 1); // Output: null
|
|
32
|
+
## Documentation
|
|
58
33
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const value = got(arr, 1);
|
|
34
|
+
For full documentation visit
|
|
35
|
+
[get-or-throw.codecompose.dev](https://get-or-throw.codecompose.dev/).
|
|
62
36
|
|
|
63
|
-
|
|
64
|
-
const obj = { a: 1, b: null, c: 3 };
|
|
65
|
-
const value = got(obj, "b"); // Output: null
|
|
37
|
+
## License
|
|
66
38
|
|
|
67
|
-
|
|
68
|
-
const obj = { a: 1, b: undefined, c: 3 };
|
|
69
|
-
const value = got(obj, "b");
|
|
70
|
-
```
|
|
39
|
+
MIT
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/get-or-throw.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Get a value from an object or array, and throw an error if the key or index
|
|
3
4
|
* does not exist or if the resulting value is undefined.
|
|
@@ -9,7 +10,10 @@
|
|
|
9
10
|
* @throws An error if the key or index does not exist, or if the value is
|
|
10
11
|
* undefined.
|
|
11
12
|
*/
|
|
12
|
-
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T, keyOrIndex: K, errorMessage?: string): T[K]
|
|
13
|
+
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T, keyOrIndex: K, errorMessage?: string): NonNullable<T[K]>;
|
|
13
14
|
declare function getOrThrow<T>(objOrArr: T[], keyOrIndex: number, errorMessage?: string): T;
|
|
15
|
+
/** Export the same function under the alias 'got' */
|
|
14
16
|
|
|
17
|
+
//#endregion
|
|
15
18
|
export { getOrThrow, getOrThrow as got };
|
|
19
|
+
//# sourceMappingURL=index-CLvtl5bd.d.ts.map
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/get-or-throw.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Get a value from an object or array, and throw an error if the key or index
|
|
3
4
|
* does not exist or if the resulting value is undefined.
|
|
@@ -9,7 +10,10 @@
|
|
|
9
10
|
* @throws An error if the key or index does not exist, or if the value is
|
|
10
11
|
* undefined.
|
|
11
12
|
*/
|
|
12
|
-
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T, keyOrIndex: K, errorMessage?: string): T[K]
|
|
13
|
+
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T, keyOrIndex: K, errorMessage?: string): NonNullable<T[K]>;
|
|
13
14
|
declare function getOrThrow<T>(objOrArr: T[], keyOrIndex: number, errorMessage?: string): T;
|
|
15
|
+
/** Export the same function under the alias 'got' */
|
|
14
16
|
|
|
17
|
+
//#endregion
|
|
15
18
|
export { getOrThrow, getOrThrow as got };
|
|
19
|
+
//# sourceMappingURL=index-CRoiksIS.d.cts.map
|
package/dist/index.cjs
CHANGED
|
@@ -1,69 +1,24 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
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
|
-
got: () => getOrThrow
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(src_exports);
|
|
27
|
-
|
|
28
|
-
// src/get-or-throw.ts
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/get-or-throw.ts
|
|
29
3
|
function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
throw new Error(
|
|
46
|
-
errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
} else {
|
|
50
|
-
if (keyOrIndex in objOrArr) {
|
|
51
|
-
const value = objOrArr[keyOrIndex];
|
|
52
|
-
if (value === void 0) {
|
|
53
|
-
throw new Error(
|
|
54
|
-
errorMessage ?? `Value at key "${String(keyOrIndex)}" is undefined.`
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
return value;
|
|
58
|
-
} else {
|
|
59
|
-
throw new Error(
|
|
60
|
-
errorMessage ?? `Key "${String(keyOrIndex)}" does not exist in the object.`
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
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.`);
|
|
64
19
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
20
|
+
//#endregion
|
|
21
|
+
exports.getOrThrow = getOrThrow;
|
|
22
|
+
exports.got = getOrThrow;
|
|
23
|
+
|
|
24
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
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
CHANGED
|
@@ -1,41 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/get-or-throw.ts
|
|
2
2
|
function getOrThrow(objOrArr, keyOrIndex, errorMessage) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
throw new Error(
|
|
19
|
-
errorMessage ?? `Index ${String(keyOrIndex)} is out of bounds.`
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
} else {
|
|
23
|
-
if (keyOrIndex in objOrArr) {
|
|
24
|
-
const value = objOrArr[keyOrIndex];
|
|
25
|
-
if (value === void 0) {
|
|
26
|
-
throw new Error(
|
|
27
|
-
errorMessage ?? `Value at key "${String(keyOrIndex)}" is undefined.`
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
return value;
|
|
31
|
-
} else {
|
|
32
|
-
throw new Error(
|
|
33
|
-
errorMessage ?? `Key "${String(keyOrIndex)}" does not exist in the object.`
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
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.`);
|
|
37
18
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
19
|
+
//#endregion
|
|
20
|
+
export { getOrThrow, getOrThrow as got };
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|