@sandlada/result 0.0.1-20260701.a
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/CODE_OF_CONDUCT.md +132 -0
- package/LICENSE +21 -0
- package/README.md +183 -0
- package/SECURITY.md +12 -0
- package/build/IResult.d.ts +16 -0
- package/build/IResult.d.ts.map +1 -0
- package/build/IResult.js +2 -0
- package/build/IResult.js.map +1 -0
- package/build/IResultOfT.d.ts +12 -0
- package/build/IResultOfT.d.ts.map +1 -0
- package/build/IResultOfT.js +2 -0
- package/build/IResultOfT.js.map +1 -0
- package/build/Result.d.ts +53 -0
- package/build/Result.d.ts.map +1 -0
- package/build/Result.js +83 -0
- package/build/Result.js.map +1 -0
- package/build/ResultOfT.d.ts +8 -0
- package/build/ResultOfT.d.ts.map +1 -0
- package/build/ResultOfT.js +8 -0
- package/build/ResultOfT.js.map +1 -0
- package/build/index.d.ts +5 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +3 -0
- package/build/index.js.map +1 -0
- package/package.json +42 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity
|
|
10
|
+
and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the
|
|
26
|
+
overall community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or
|
|
31
|
+
advances of any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email
|
|
35
|
+
address, without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series
|
|
86
|
+
of actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or
|
|
93
|
+
permanent ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within
|
|
113
|
+
the community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.0, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/0/code_of_conduct.html][v2.0].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available
|
|
126
|
+
at [https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.0]: https://www.contributor-covenant.org/version/2/0/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kai-Orion & Sandlada
|
|
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,183 @@
|
|
|
1
|
+
# @sandlada/jss
|
|
2
|
+
|
|
3
|
+
一個簡單的 CSS-in-JS 函式庫,提供兩類工具來管理 CSS 自訂屬性(變數):
|
|
4
|
+
|
|
5
|
+
- **define 系列** — 產生 CSS 宣告(`--name: value`),用於輸出到樣式表
|
|
6
|
+
- **use 系列** — 產生 `var()` 引用(`var(--name, fallback)`),用於組合進其他宣告中
|
|
7
|
+
|
|
8
|
+
完整文件請參閱 [docs/README.md](./docs/README.md)。
|
|
9
|
+
|
|
10
|
+
## 安裝
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @sandlada/jss
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
專案使用 TypeScript 6 + ESM,匯入即可獲得完整的型別推斷。
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## define 系列 — 產生 CSS 宣告
|
|
21
|
+
|
|
22
|
+
| 函式 | 說明 | 文件 |
|
|
23
|
+
| ------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
|
|
24
|
+
| `defineVars(name, value)` | 基本變數定義,回傳字串陣列 | [docs/define-vars.md](./docs/define-vars.md) |
|
|
25
|
+
| `defineLogicalBorderRadiusVars(base, value, options?)` | 展開為四個邏輯角(`start-start` / `start-end` / `end-start` / `end-end`),回傳陣列,支援 `{ semi?, prefix? }` | [docs/define-logical-border-radius-vars.md](./docs/define-logical-border-radius-vars.md) |
|
|
26
|
+
| `defineLogicalBorderRadiusVarsRecord(base, value)` | 同上,但回傳 Record 物件 | [docs/define-logical-border-radius-vars-record.md](./docs/define-logical-border-radius-vars-record.md) |
|
|
27
|
+
| `defineTokenRefsRecord(tokens, options?)` | 將設計 Token 綁定為內部變數(`--_key: var(--key, value)`),支援形狀屬性展開與 `{ prefix? }` | [docs/define-token-refs-record.md](./docs/define-token-refs-record.md) |
|
|
28
|
+
| `defineOverrides(source, overrides)(prefix?)` | 型別安全的樣式覆蓋輔助,Curried API,可傳入 `null` 跳過型別約束 | [docs/define-overrides.md](./docs/define-overrides.md) |
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { defineVars } from '@sandlada/jss'
|
|
32
|
+
|
|
33
|
+
// ['--color: red']
|
|
34
|
+
defineVars('color', 'red')
|
|
35
|
+
|
|
36
|
+
// ['--color: red', '--bg-color: blue']
|
|
37
|
+
defineVars({ color: 'red', 'bg-color': 'blue' })
|
|
38
|
+
|
|
39
|
+
// 第三個參數 true 加上分號尾綴
|
|
40
|
+
defineVars('color', 'red', true) // ['--color: red;']
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { defineTokenRefsRecord } from '@sandlada/jss'
|
|
45
|
+
|
|
46
|
+
const AppTokens = {
|
|
47
|
+
'button-text-color': 'red',
|
|
48
|
+
'button-bg-color': 'white',
|
|
49
|
+
'button-shape': 'var(--md-sys-shape-corner-full, 9999px)',
|
|
50
|
+
} as const
|
|
51
|
+
|
|
52
|
+
// 將設計 Token 綁定為內部變數:
|
|
53
|
+
// { '--_button-text-color': 'var(--button-text-color, red)', ... }
|
|
54
|
+
defineTokenRefsRecord(AppTokens)
|
|
55
|
+
|
|
56
|
+
// 使用 prefix 為所有 token 加上前綴:
|
|
57
|
+
defineTokenRefsRecord(AppTokens, { prefix: '--md-badge' })
|
|
58
|
+
// { '--_button-text-color': 'var(--md-badge-button-text-color, red)', ... }
|
|
59
|
+
|
|
60
|
+
// 展開形狀屬性為四個邏輯角:
|
|
61
|
+
defineTokenRefsRecord(AppTokens, { expandShapes: ['button-shape'] })
|
|
62
|
+
// {
|
|
63
|
+
// '--_button-text-color': 'var(--button-text-color, red)',
|
|
64
|
+
// '--_button-shape-start-start': 'var(--button-shape-start-start, var(--md-sys-shape-corner-full, 9999px))',
|
|
65
|
+
// '--_button-shape-start-end': 'var(--button-shape-start-end, ...)',
|
|
66
|
+
// '--_button-shape-end-start': 'var(--button-shape-end-start, ...)',
|
|
67
|
+
// '--_button-shape-end-end': 'var(--button-shape-end-end, ...)',
|
|
68
|
+
// }
|
|
69
|
+
|
|
70
|
+
// 加入基底變數作為中繼備援(兩層 var() 遞迴):
|
|
71
|
+
defineTokenRefsRecord(AppTokens, { expandShapes: ['button-shape'], useBaseFallback: true })
|
|
72
|
+
// {
|
|
73
|
+
// '--_button-shape-start-start': 'var(--button-shape-start-start, var(--button-shape, var(--md-sys-shape-corner-full, 9999px)))',
|
|
74
|
+
// ...
|
|
75
|
+
// }
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## defineOverrides — 型別安全的樣式覆蓋
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { defineOverrides } from '@sandlada/jss'
|
|
84
|
+
|
|
85
|
+
const FocusRing = {
|
|
86
|
+
'outline-color': 'red',
|
|
87
|
+
'outline-width': '2px',
|
|
88
|
+
} as const
|
|
89
|
+
|
|
90
|
+
// 型別安全模式:從 source 推導鍵值約束,所有鍵皆為可選
|
|
91
|
+
// { 'outline-color': 'blue' }
|
|
92
|
+
defineOverrides(FocusRing, { 'outline-color': 'blue' })()
|
|
93
|
+
|
|
94
|
+
// 套用 CSS 變數前綴
|
|
95
|
+
// { '--my-comp-outline-color': 'blue' }
|
|
96
|
+
defineOverrides(FocusRing, { 'outline-color': 'blue' })('--my-comp')
|
|
97
|
+
|
|
98
|
+
// 部分覆蓋或空物件皆可
|
|
99
|
+
defineOverrides(FocusRing, {})()
|
|
100
|
+
|
|
101
|
+
// 無型別約束模式:source 傳入 null
|
|
102
|
+
defineOverrides(null, { 'outline-color': 'blue' })()
|
|
103
|
+
|
|
104
|
+
// 實際使用:覆蓋共享元件樣式
|
|
105
|
+
const CompB = {
|
|
106
|
+
'bg-color': 'blue',
|
|
107
|
+
...defineOverrides(FocusRing, { 'outline-color': 'blue' })('--my-comp'),
|
|
108
|
+
} as const
|
|
109
|
+
// { 'bg-color': 'blue', '--my-comp-outline-color': 'blue' }
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## 通用選項
|
|
115
|
+
|
|
116
|
+
部分函式支援第二個(或第三個)參數傳入 `JSSOptions` 物件來控制行為:
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
interface JSSOptions {
|
|
120
|
+
semi?: boolean // 是否在結尾加上分號(;)
|
|
121
|
+
prefix?: string // CSS 變數名稱前綴(例如 '--md-badge')
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
**`semi`** — 加上分號尾綴,適用於需要 inline style 語句的場景。
|
|
126
|
+
|
|
127
|
+
**`prefix`** — 為產生的 CSS 變數名稱加上自訂前綴。例如使用 `{ prefix: '--md-badge' }` 時,
|
|
128
|
+
`useVars('color-primary', 'blue', { prefix: '--md-badge' })` 會輸出 `var(--md-badge-color-primary, blue)`,
|
|
129
|
+
取代預設的 `var(--color-primary, blue)`。
|
|
130
|
+
|
|
131
|
+
> **注意:** `prefix` 僅影響 CSS 變數的**名稱部分**,不影響 Record 的 JavaScript 鍵名。
|
|
132
|
+
> 支援 prefix 的函式:`useVars`、`useVarsRecord`、`useInternalVarsRecord`、`useLogicalBorderRadiusVars`、
|
|
133
|
+
> `useLogicalBorderRadiusVarsRecord`、`defineLogicalBorderRadiusVars`、`defineTokenRefsRecord`。
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## use 系列 — 產生 `var()` 引用
|
|
138
|
+
|
|
139
|
+
| 函式 | 說明 | 文件 |
|
|
140
|
+
| -------------------------------------------------------------- | -------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
|
|
141
|
+
| `useVars(name, fallback, options?)` | 基本變數引用,回傳陣列,支援 `{ semi?, prefix? }` | [docs/use-vars.md](./docs/use-vars.md) |
|
|
142
|
+
| `useVarsRecord(name, fallback, options?)` | 基本變數引用,回傳 Record,支援 `{ semi?, prefix? }` | [docs/use-vars-record.md](./docs/use-vars-record.md) |
|
|
143
|
+
| `useInternalVars(name, fallback)` | 內部變數(`--_` 前綴)引用,回傳陣列 | [docs/use-internal-vars.md](./docs/use-internal-vars.md) |
|
|
144
|
+
| `useInternalVarsRecord(name, fallback, options?)` | 內部變數引用,回傳 Record,支援 `{ semi?, prefix? }` | [docs/use-internal-vars-record.md](./docs/use-internal-vars-record.md) |
|
|
145
|
+
| `useLogicalBorderRadiusVars(corner, fallback, options?)` | 邏輯角變數(兩層 `var()` 遞迴),回傳陣列,支援 `{ semi?, prefix? }` | [docs/use-logical-border-radius-vars.md](./docs/use-logical-border-radius-vars.md) |
|
|
146
|
+
| `useLogicalBorderRadiusVarsRecord(corner, fallback, options?)` | 邏輯角變數,回傳 Record,支援 `{ semi?, prefix? }` | [docs/use-logical-border-radius-vars-record.md](./docs/use-logical-border-radius-vars-record.md) |
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
import { useVars } from '@sandlada/jss'
|
|
150
|
+
|
|
151
|
+
// ['var(--color-primary, blue)']
|
|
152
|
+
useVars('color-primary', 'blue')
|
|
153
|
+
|
|
154
|
+
// 支援 `var()` 遞迴鏈:
|
|
155
|
+
// ['var(--a, var(--b, var(--c, default-value)))']
|
|
156
|
+
useVars(['a', 'b', 'c'], 'default-value')
|
|
157
|
+
|
|
158
|
+
// Record 模式自動剝除 -- 前綴作為鍵名:
|
|
159
|
+
// { 'color-primary': 'var(--color-primary, blue)' }
|
|
160
|
+
useVarsRecord('--color-primary', 'blue')
|
|
161
|
+
|
|
162
|
+
// 使用 prefix 選項為所有變數名稱加上前綴:
|
|
163
|
+
// ['var(--md-badge-color-primary, blue)']
|
|
164
|
+
useVars('color-primary', 'blue', { prefix: '--md-badge' })
|
|
165
|
+
|
|
166
|
+
// ['var(--md-badge-a, var(--md-badge-b, var(--md-badge-c, default-value)))']
|
|
167
|
+
useVars(['a', 'b', 'c'], 'default-value', { prefix: '--md-badge' })
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## 開發
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
# 安裝依賴
|
|
176
|
+
npm install
|
|
177
|
+
|
|
178
|
+
# 執行測試
|
|
179
|
+
npx vitest
|
|
180
|
+
|
|
181
|
+
# 型別檢查
|
|
182
|
+
npx tsc --noEmit
|
|
183
|
+
```
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## Supported Versions
|
|
4
|
+
|
|
5
|
+
Security updates are only applied to the latest published version.
|
|
6
|
+
|
|
7
|
+
## Reporting a Vulnerability
|
|
8
|
+
|
|
9
|
+
Report vulnerabilities in a private
|
|
10
|
+
[GitHub security advisory](https://github.com/sandlada/result/security/advisories).
|
|
11
|
+
|
|
12
|
+
**Please do not disclose security vulnerabilities in public issues.**
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IResult — base result contract.
|
|
3
|
+
*
|
|
4
|
+
* Every result exposes an error, a success flag, and a failure flag.
|
|
5
|
+
* The error is always accessible; on success it returns an internal sentinel.
|
|
6
|
+
* Consumers should check `isSuccess` before interpreting `error`.
|
|
7
|
+
*/
|
|
8
|
+
export interface IResult<TError = Error> {
|
|
9
|
+
/** Always accessible. On success, returns the NONE sentinel. */
|
|
10
|
+
readonly error: TError;
|
|
11
|
+
/** `true` if the operation succeeded. */
|
|
12
|
+
readonly isSuccess: boolean;
|
|
13
|
+
/** `true` if the operation failed. Always the negation of `isSuccess`. */
|
|
14
|
+
readonly isFailure: boolean;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=IResult.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IResult.d.ts","sourceRoot":"","sources":["../src/IResult.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,WAAW,OAAO,CAAC,MAAM,GAAG,KAAK;IACnC,gEAAgE;IAChE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,yCAAyC;IACzC,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAE5B,0EAA0E;IAC1E,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;CAC/B"}
|
package/build/IResult.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IResult.js","sourceRoot":"","sources":["../src/IResult.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { IResult } from './IResult.js';
|
|
2
|
+
/**
|
|
3
|
+
* IResultOfT<TValue, TError> — a result that carries a success value.
|
|
4
|
+
*
|
|
5
|
+
* Extends the base {@link IResult} contract with a `value` property.
|
|
6
|
+
* Accessing `value` on a failure result throws a TypeError.
|
|
7
|
+
*/
|
|
8
|
+
export interface IResultOfT<TValue, TError = Error> extends IResult<TError> {
|
|
9
|
+
/** The success payload. Throws `TypeError` if accessed on a failure. */
|
|
10
|
+
readonly value: TValue;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=IResultOfT.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IResultOfT.d.ts","sourceRoot":"","sources":["../src/IResultOfT.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAE5C;;;;;GAKG;AACH,MAAM,WAAW,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,CAAE,SAAQ,OAAO,CAAC,MAAM,CAAC;IACvE,wEAAwE;IACxE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CAC1B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IResultOfT.js","sourceRoot":"","sources":["../src/IResultOfT.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { IResult } from './IResult.js';
|
|
2
|
+
import type { IResultOfT } from './IResultOfT.js';
|
|
3
|
+
/**
|
|
4
|
+
* Base class implementing the core result invariant.
|
|
5
|
+
*
|
|
6
|
+
* ## Invariant (enforced by constructor)
|
|
7
|
+
* - `isSuccess && error !== NONE` → **throw** (success must not carry a real error)
|
|
8
|
+
* - `!isSuccess && error === NONE` → **throw** (failure must carry a real error)
|
|
9
|
+
*
|
|
10
|
+
* Users do not instantiate this class directly.
|
|
11
|
+
* Use the static factory methods: {@link Result.Success}, {@link Result.Failure}.
|
|
12
|
+
*/
|
|
13
|
+
export declare class Result<TError = Error> implements IResult<TError> {
|
|
14
|
+
readonly isSuccess: boolean;
|
|
15
|
+
readonly error: TError;
|
|
16
|
+
/**
|
|
17
|
+
* **Internal.** Validates the mutual-exclusivity invariant.
|
|
18
|
+
*
|
|
19
|
+
* @throws If the invariant is violated (success with real error, or failure with NONE).
|
|
20
|
+
*/
|
|
21
|
+
protected constructor(isSuccess: boolean, error?: TError);
|
|
22
|
+
get isFailure(): boolean;
|
|
23
|
+
/** Creates a void success result (no value). */
|
|
24
|
+
static Success(): IResult;
|
|
25
|
+
/** Creates a success result carrying a value. Type is inferred. */
|
|
26
|
+
static Success<TValue>(value: TValue): IResultOfT<TValue>;
|
|
27
|
+
/** Creates a void failure result. */
|
|
28
|
+
static Failure(error: Error): IResult;
|
|
29
|
+
/** Creates a typed failure result. `TValue` must be specified. */
|
|
30
|
+
static Failure<TValue, TError>(error: TError): IResultOfT<TValue, TError>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Generic result carrying a success value.
|
|
34
|
+
*
|
|
35
|
+
* Extends the {@link Result} base class and implements
|
|
36
|
+
* {@link IResultOfT}.
|
|
37
|
+
*
|
|
38
|
+
* Users do not instantiate this class directly.
|
|
39
|
+
* Use {@link Result.Success} or {@link Result.Failure}.
|
|
40
|
+
*/
|
|
41
|
+
export declare class ResultOfT<TValue, TError = Error> extends Result<TError> implements IResultOfT<TValue, TError> {
|
|
42
|
+
#private;
|
|
43
|
+
/**
|
|
44
|
+
* **Internal.** Constructed by {@link Result} factory methods.
|
|
45
|
+
*/
|
|
46
|
+
constructor(value?: TValue, isSuccess?: boolean, error?: TError);
|
|
47
|
+
/**
|
|
48
|
+
* The success payload.
|
|
49
|
+
* @throws {TypeError} If this result represents a failure.
|
|
50
|
+
*/
|
|
51
|
+
get value(): TValue;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=Result.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Result.d.ts","sourceRoot":"","sources":["../src/Result.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAWlD;;;;;;;;;GASG;AACH,qBAAa,MAAM,CAAC,MAAM,GAAG,KAAK,CAAE,YAAW,OAAO,CAAC,MAAM,CAAC;IAC1D,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB;;;;OAIG;IACH,SAAS,aAAa,SAAS,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,EAavD;IAED,IAAI,SAAS,IAAI,OAAO,CAEvB;IAED,gDAAgD;IAChD,MAAM,CAAC,OAAO,IAAI,OAAO,CAAC;IAC1B,mEAAmE;IACnE,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;IAQ1D,qCAAqC;IACrC,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC;IACtC,kEAAkE;IAClE,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAO7E;AAED;;;;;;;;GAQG;AACH,qBAAa,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,CAAE,SAAQ,MAAM,CAAC,MAAM,CAAE,YAAW,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC;;IAGvG;;OAEG;IACH,YAAY,KAAK,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,EAO9D;IAED;;;OAGG;IACH,IAAI,KAAK,IAAI,MAAM,CAOlB;CACJ"}
|
package/build/Result.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal sentinel used for the `error` property of a success result.
|
|
3
|
+
*
|
|
4
|
+
* Uses `Symbol.for()` so it survives module reloads in dev.
|
|
5
|
+
* Cast to `TError` so it satisfies the type system while being
|
|
6
|
+
* distinguishable from a real user-supplied error.
|
|
7
|
+
*/
|
|
8
|
+
const NONE = Symbol.for('result:none');
|
|
9
|
+
/**
|
|
10
|
+
* Base class implementing the core result invariant.
|
|
11
|
+
*
|
|
12
|
+
* ## Invariant (enforced by constructor)
|
|
13
|
+
* - `isSuccess && error !== NONE` → **throw** (success must not carry a real error)
|
|
14
|
+
* - `!isSuccess && error === NONE` → **throw** (failure must carry a real error)
|
|
15
|
+
*
|
|
16
|
+
* Users do not instantiate this class directly.
|
|
17
|
+
* Use the static factory methods: {@link Result.Success}, {@link Result.Failure}.
|
|
18
|
+
*/
|
|
19
|
+
export class Result {
|
|
20
|
+
isSuccess;
|
|
21
|
+
error;
|
|
22
|
+
/**
|
|
23
|
+
* **Internal.** Validates the mutual-exclusivity invariant.
|
|
24
|
+
*
|
|
25
|
+
* @throws If the invariant is violated (success with real error, or failure with NONE).
|
|
26
|
+
*/
|
|
27
|
+
constructor(isSuccess, error) {
|
|
28
|
+
this.isSuccess = isSuccess;
|
|
29
|
+
if (isSuccess && error !== NONE) {
|
|
30
|
+
throw new TypeError('Result invariant violated: success must not carry a real error.');
|
|
31
|
+
}
|
|
32
|
+
if (!isSuccess && error === NONE) {
|
|
33
|
+
throw new TypeError('Result invariant violated: failure must carry a real error.');
|
|
34
|
+
}
|
|
35
|
+
this.error = error;
|
|
36
|
+
}
|
|
37
|
+
get isFailure() {
|
|
38
|
+
return !this.isSuccess;
|
|
39
|
+
}
|
|
40
|
+
static Success(value) {
|
|
41
|
+
if (arguments.length === 0) {
|
|
42
|
+
return new Result(true, NONE);
|
|
43
|
+
}
|
|
44
|
+
return new ResultOfT(value, true);
|
|
45
|
+
}
|
|
46
|
+
static Failure(error) {
|
|
47
|
+
if (error === undefined) {
|
|
48
|
+
throw new TypeError('Result.Failure requires an error argument.');
|
|
49
|
+
}
|
|
50
|
+
return new ResultOfT(undefined, false, error);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Generic result carrying a success value.
|
|
55
|
+
*
|
|
56
|
+
* Extends the {@link Result} base class and implements
|
|
57
|
+
* {@link IResultOfT}.
|
|
58
|
+
*
|
|
59
|
+
* Users do not instantiate this class directly.
|
|
60
|
+
* Use {@link Result.Success} or {@link Result.Failure}.
|
|
61
|
+
*/
|
|
62
|
+
export class ResultOfT extends Result {
|
|
63
|
+
#value;
|
|
64
|
+
/**
|
|
65
|
+
* **Internal.** Constructed by {@link Result} factory methods.
|
|
66
|
+
*/
|
|
67
|
+
constructor(value, isSuccess, error) {
|
|
68
|
+
const sentinelError = NONE;
|
|
69
|
+
super(isSuccess ?? true, isSuccess === false ? error : sentinelError);
|
|
70
|
+
this.#value = value;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The success payload.
|
|
74
|
+
* @throws {TypeError} If this result represents a failure.
|
|
75
|
+
*/
|
|
76
|
+
get value() {
|
|
77
|
+
if (!this.isSuccess) {
|
|
78
|
+
throw new TypeError('Cannot access value on a failure result. Check isSuccess before accessing value.');
|
|
79
|
+
}
|
|
80
|
+
return this.#value;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=Result.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Result.js","sourceRoot":"","sources":["../src/Result.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,MAAM,IAAI,GAAkB,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;AAEtD;;;;;;;;;GASG;AACH,MAAM,OAAO,MAAM;IACN,SAAS,CAAU;IACnB,KAAK,CAAS;IAEvB;;;;OAIG;IACH,YAAsB,SAAkB,EAAE,KAAc;QACpD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,SAAS,IAAI,KAAK,KAAM,IAA0B,EAAE,CAAC;YACrD,MAAM,IAAI,SAAS,CACf,iEAAiE,CACpE,CAAC;QACN,CAAC;QACD,IAAI,CAAC,SAAS,IAAI,KAAK,KAAM,IAA0B,EAAE,CAAC;YACtD,MAAM,IAAI,SAAS,CACf,6DAA6D,CAChE,CAAC;QACN,CAAC;QACD,IAAI,CAAC,KAAK,GAAG,KAAe,CAAC;IACjC,CAAC;IAED,IAAI,SAAS;QACT,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC;IAC3B,CAAC;IAMD,MAAM,CAAC,OAAO,CAAS,KAAc;QACjC,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACzB,OAAO,IAAI,MAAM,CAAQ,IAAI,EAAG,IAAyB,CAAC,CAAC;QAC/D,CAAC;QACD,OAAO,IAAI,SAAS,CAAgB,KAAM,EAAE,IAAI,CAAC,CAAC;IACtD,CAAC;IAMD,MAAM,CAAC,OAAO,CAAiB,KAAc;QACzC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACtB,MAAM,IAAI,SAAS,CAAC,4CAA4C,CAAC,CAAC;QACtE,CAAC;QACD,OAAO,IAAI,SAAS,CAAiB,SAAS,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IAClE,CAAC;CACJ;AAED;;;;;;;;GAQG;AACH,MAAM,OAAO,SAAkC,SAAQ,MAAc;IACxD,MAAM,CAAqB;IAEpC;;OAEG;IACH,YAAY,KAAc,EAAE,SAAmB,EAAE,KAAc;QAC3D,MAAM,aAAa,GAAI,IAA0B,CAAC;QAClD,KAAK,CACD,SAAS,IAAI,IAAI,EACjB,SAAS,KAAK,KAAK,CAAC,CAAC,CAAC,KAAM,CAAC,CAAC,CAAC,aAAa,CAC/C,CAAC;QACF,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACxB,CAAC;IAED;;;OAGG;IACH,IAAI,KAAK;QACL,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YAClB,MAAM,IAAI,SAAS,CACf,kFAAkF,CACrF,CAAC;QACN,CAAC;QACD,OAAO,IAAI,CAAC,MAAgB,CAAC;IACjC,CAAC;CACJ"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Re-export of {@link ResultOfT} from the main {@link Result} module.
|
|
3
|
+
*
|
|
4
|
+
* The generic class lives alongside {@link Result} in `Result.ts` to avoid
|
|
5
|
+
* circular dependencies. This file keeps it discoverable at the expected path.
|
|
6
|
+
*/
|
|
7
|
+
export { ResultOfT } from './Result.js';
|
|
8
|
+
//# sourceMappingURL=ResultOfT.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ResultOfT.d.ts","sourceRoot":"","sources":["../src/ResultOfT.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Re-export of {@link ResultOfT} from the main {@link Result} module.
|
|
3
|
+
*
|
|
4
|
+
* The generic class lives alongside {@link Result} in `Result.ts` to avoid
|
|
5
|
+
* circular dependencies. This file keeps it discoverable at the expected path.
|
|
6
|
+
*/
|
|
7
|
+
export { ResultOfT } from './Result.js';
|
|
8
|
+
//# sourceMappingURL=ResultOfT.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ResultOfT.js","sourceRoot":"","sources":["../src/ResultOfT.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC"}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,YAAY,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sandlada/result",
|
|
3
|
+
"version": "0.0.1-20260701.a",
|
|
4
|
+
"description": "Type-safe Result pattern for TypeScript — explicit error handling without exceptions",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./build/index.js",
|
|
7
|
+
"types": "./build/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./build/index.d.ts",
|
|
11
|
+
"default": "./build/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"build/",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE",
|
|
18
|
+
"CODE_OF_CONDUCT.md",
|
|
19
|
+
"SECURITY.md"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsgo --project tsconfig.json",
|
|
23
|
+
"test": "vitest run",
|
|
24
|
+
"test:watch": "vitest"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"result",
|
|
28
|
+
"either",
|
|
29
|
+
"error-handling",
|
|
30
|
+
"functional",
|
|
31
|
+
"typescript"
|
|
32
|
+
],
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"author": "Kai-Orion & Sandlada",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@typescript/native-preview": "^7.0.0-dev.20260630.1",
|
|
40
|
+
"vitest": "^4.1.9"
|
|
41
|
+
}
|
|
42
|
+
}
|