Skip to content

Commit 4a0f7bd

Browse files
committed
adds match functionality, simpler narrowing using ok (deprecating isOk() and isError()), Result.error makes strings constant by default
1 parent c0cd117 commit 4a0f7bd

13 files changed

+761
-68
lines changed

docs/chaining-vs-generator-syntax.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ Similar to arrays and promises, you can also chain operations on a result. The m
2323
```ts
2424
// Without chaining
2525
const resultA = someOperation();
26-
if (resultA.isOk()) {
26+
if (resultA.ok) {
2727
const resultB = anotherOperation(resultA.value);
28-
if (resultB.isOk()) {
28+
if (resultB.ok) {
2929
const resultC = yetAnotherOperation(resultB.value);
30-
if (resultC.isOk()) {
30+
if (resultC.ok) {
3131
// do something
3232
} else {
3333
// handle error
@@ -44,7 +44,7 @@ const result = someOperation()
4444
.map((value) => anotherOperation(value))
4545
.map((value) => yetAnotherOperation(value))
4646

47-
if (result.isOk()) {
47+
if (result.ok) {
4848
// do something
4949
} else {
5050
// handle error

docs/getting-started.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,10 @@ function readConfig(filePath: string) {
152152

153153
const result = readConfig("config.json");
154154
// ^?
155-
156-
157-
//
158155
```
159156

157+
<div class="spacer" />
158+
160159
If you look at the final `result`, you'll see exactly what the outcome of the function can be. And while technically the above code is correct, it is very verbose. Luckily, the library provides a way to make this code more concise.
161160

162161
### Adding the `Result.try` helper
@@ -194,15 +193,15 @@ function readConfig(filePath: string) {
194193
() => fs.readFileSync(filePath, "utf-8"),
195194
(error) => new IOError(`Unable to read file: ${filePath}`, { cause: error })
196195
);
197-
if (contentResult.isError()) {
196+
if (!contentResult.ok) {
198197
return contentResult;
199198
}
200199

201200
const jsonResult = Result.try(
202201
() => JSON.parse(contentResult.value),
203202
() => new ParseError(`Unable to parse JSON from file: ${filePath}`)
204203
);
205-
if (jsonResult.isError()) {
204+
if (!jsonResult.ok) {
206205
return jsonResult;
207206
}
208207

@@ -266,12 +265,12 @@ const parseConfig = Result.wrap(
266265

267266
function readConfig(filePath: string) {
268267
const contentResult = readFile(filePath);
269-
if (contentResult.isError()) {
268+
if (!contentResult.ok) {
270269
return contentResult;
271270
}
272271

273272
const jsonResult = parseJSON(contentResult.value);
274-
if (jsonResult.isError()) {
273+
if (!jsonResult.ok) {
275274
return jsonResult;
276275
}
277276

@@ -360,7 +359,7 @@ Remember this pattern from a few examples back?
360359

361360
```typescript
362361
const result = operation();
363-
if (result.isError()) {
362+
if (!result.ok) {
364363
return result;
365364
}
366365
```

0 commit comments

Comments
 (0)