Skip to content

Commit 6b47d39

Browse files
committed
docs(std): added missing inline documentations
1 parent 125cc90 commit 6b47d39

File tree

12 files changed

+79
-1
lines changed

12 files changed

+79
-1
lines changed

packages/std/src/io/utils/read.ts

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { $fn, err } from '../../results'
22

33
import { ioTags } from '../tag'
44

5+
/**
6+
* The $read function reads a file from the specified path and
7+
* returns its contents as a ArrayBuffer in AsyncResult.
8+
*/
59
export const $read = $fn(async (path: string) => {
610
const file = Bun.file(path)
711
const exists = await file.exists()
@@ -13,6 +17,10 @@ export const $read = $fn(async (path: string) => {
1317
return await file.arrayBuffer()
1418
}, ioTags.get('read'))
1519

20+
/**
21+
* The $readJson function reads a file from the specified path and
22+
* returns its contents as a object in AsyncResult.
23+
*/
1624
export const $readJson = $fn(async <T>(path: string) => {
1725
const file = Bun.file(path)
1826
const exists = await file.exists()

packages/std/src/io/utils/write.ts

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import type { JsonValue } from '../../shared'
33

44
import { ioTags } from '../tag'
55

6+
/**
7+
* The $write function writes a file to the specified path and
8+
* returns true in AsyncResult.
9+
*/
610
export const $write = $fn(async (path: string, data: string | ArrayBuffer, create = true) => {
711
const file = Bun.file(path)
812
const exists = await file.exists()
@@ -16,6 +20,10 @@ export const $write = $fn(async (path: string, data: string | ArrayBuffer, creat
1620
return true
1721
}, ioTags.get('write'))
1822

23+
/**
24+
* The $writeJson function writes a object (as JSON) to the specified path and
25+
* returns true in AsyncResult.
26+
*/
1927
export const $writeJson = $fn(async (path: string, data: JsonValue, create = true) => {
2028
const file = Bun.file(path)
2129
const exists = await file.exists()

packages/std/src/results/utils/async.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Err } from './err'
22
import { Ok } from './ok'
33

4+
/**
5+
* This class converts a promise into ResultAsync
6+
*/
47
export class ResultAsync<T, N extends Std.ErrorValues = never, C extends Std.ErrorValues[] = []>
58
implements PromiseLike<Std.Result<T, N, C>>
69
{
@@ -32,9 +35,15 @@ export class ResultAsync<T, N extends Std.ErrorValues = never, C extends Std.Err
3235
}
3336
}
3437

38+
/**
39+
* Shortcut for creating successful AsyncResult
40+
*/
3541
export const okAsync = <T>(value: T): ResultAsync<T, never, never> =>
3642
new ResultAsync(Promise.resolve(new Ok<T, never, never>(value)))
3743

44+
/**
45+
* Shortcut for creating failed AsyncResult
46+
*/
3847
export const errAsync = <N extends Std.ErrorValues>(
3948
err: N,
4049
message = ''

packages/std/src/results/utils/capsule.ts

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { ResultAsync } from './async'
44

55
import { handleCatch, handleThen } from './internal/handlers'
66

7+
/**
8+
* The capsule function is used to wrap a function for unknown errors
9+
* This function doesn't return a Result or AsyncResult
10+
*/
711
export const capsule = <A extends BlobType[], R>(
812
fn: (...args: A) => R,
913
...additionalCauses: Std.ErrorValues[]

packages/std/src/results/utils/err.ts

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import type { BlobType } from '../../shared'
22

33
import type { Ok } from './ok'
44

5+
/**
6+
* This class represents a failed result
7+
*/
58
export class Err<
69
T = never,
710
const N extends Std.ErrorValues = never,
@@ -84,6 +87,9 @@ export class Err<
8487
}
8588
}
8689

90+
/**
91+
* Shortcut for creating failed result
92+
*/
8793
export function err<T = never, const N extends Std.ErrorValues = never>(
8894
name: N,
8995
message: string

packages/std/src/results/utils/fn.ts

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { handleCatch, handleThen } from './internal/handlers'
66
const invalidUsage = resultTags.get('invalid-usage')
77
const cause = resultTags.get('fn')
88

9+
/**
10+
* The $fn function is used to wrap a function that returns a Result or ResultAsync
11+
* and automatically handles the error cases.
12+
*/
913
export const $fn = <A extends BlobType[], R, C extends Std.ErrorValues[] = []>(
1014
fn: (...args: A) => R,
1115
...additionalCauses: C

packages/std/src/results/utils/from-throwable.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import type { BlobType } from '../../shared'
33
import { ResultAsync } from './async'
44
import { Ok } from './ok'
55

6+
/**
7+
* Converts an async function that throws to a function that returns a ResultAsync
8+
*/
69
export const fromAsyncThrowable = <A extends readonly BlobType[], T, R>(
710
fn: (...args: A) => Promise<T>,
811
errorFn: (err: unknown) => R

packages/std/src/results/utils/internal/handlers.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { Ok, ok } from '../ok'
77

88
const invalidUsage = resultTags.get('invalid-usage')
99

10+
/**
11+
* This helper extracts the actual value from its first argument (even its nested ok's or okAsync)
12+
*/
1013
export const handleThen = (
1114
data: BlobType,
1215
...additionalCauses: Std.ErrorValues[]
@@ -56,6 +59,9 @@ export const handleThen = (
5659
return ok(result)
5760
}
5861

62+
/**
63+
* This helper adds additional causes to an error
64+
*/
5965
export const handleCatch = (
6066
e: BlobType,
6167
...additionalCauses: Std.ErrorValues[]
@@ -64,7 +70,7 @@ export const handleCatch = (
6470
return e.appendCause(...additionalCauses)
6571
}
6672

67-
return err(invalidUsage, 'incorrect $fn usage')
73+
return err(invalidUsage, 'incorrect usage of handleCatch')
6874
.appendCause(...additionalCauses)
6975
.appendData(e)
7076
}

packages/std/src/results/utils/ok.ts

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import type { BlobType } from '../../shared'
22

33
import type { Err } from './err'
44

5+
/**
6+
* This class represents a successful result
7+
*/
58
export class Ok<T, const N extends Std.ErrorValues = never, const C extends Std.ErrorValues[] = []>
69
implements Std.ResultType<T, N, C>
710
{
@@ -33,4 +36,7 @@ export class Ok<T, const N extends Std.ErrorValues = never, const C extends Std.
3336
}
3437
}
3538

39+
/**
40+
* Shortcut for creating successful result
41+
*/
3642
export const ok = <T>(value: T) => new Ok(value)

packages/std/src/results/utils/tag.ts

+15
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@ import type { BlobType, LiteralUnion } from '../../shared'
22

33
import { err } from './err'
44

5+
/**
6+
* Tags Manager for result system. It's used to create a tag for a result.
7+
* Use `Tags.create` to create a new instance. Use `Tags.add` to add a new tag.
8+
* For better type inference, use with chaining `Tags.add(...).add(...)`.
9+
*/
510
export class Tags<U extends [string, string], T extends string> {
611
tags = new Map<string, string>()
712

813
constructor(protected base: T) {}
914

15+
/**
16+
* Adds a new tag to the Tag Manager.
17+
*/
1018
add<K extends string, V extends string = never>(key: K, value?: V) {
1119
const tag = `${this.base}.${value ?? key}`
1220

@@ -17,6 +25,10 @@ export class Tags<U extends [string, string], T extends string> {
1725
return this as Tags<U | [K, V], T>
1826
}
1927

28+
/**
29+
* Gets a specific key (can be a tag or a value) from the Tag Manager.
30+
* If the key is not found, an error is thrown.
31+
*/
2032
get<K extends LiteralUnion<U['0'], `?${string}`>>(key: K) {
2133
const found = this.tags.get(key)
2234

@@ -31,6 +43,9 @@ export class Tags<U extends [string, string], T extends string> {
3143
return found as R extends never ? `${T}.${K}` : `${T}.${R}`
3244
}
3345

46+
/**
47+
* Checks if the Tag Manager has a specific key (can be a tag or a value).
48+
*/
3449
has<const K extends string>(
3550
key: K
3651
): K extends Std.ExtractErrors<Tags<U, T>> ? true : K extends U['0'] ? true : false {

packages/std/src/results/utils/try.ts

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import { handleCatch, handleThen } from './internal/handlers'
66
const invalidUsage = resultTags.get('invalid-usage')
77
const cause = resultTags.get('try')
88

9+
/**
10+
* Executes the given generator function and returns the result.
11+
*
12+
* If the function throws an error, the error is wrapped in a result and returned.
13+
* If the function returns a result, the result is returned.
14+
*/
915
export function $try<R, R2, C extends Std.ErrorValues[] = []>(
1016
body: () => Generator<R, R2>,
1117
...additionalCauses: C

packages/std/src/shared/utils/timing.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Waits for a specified amount of time and returns a default value passed as a parameter
3+
*/
14
export const wait = async <T = true>(ms: number, defaultValue?: T): Promise<T> => {
25
return new Promise(resolve => {
36
setTimeout(() => {

0 commit comments

Comments
 (0)