Skip to content

Commit 8bc84c6

Browse files
committed
feat(std/results): improve tags usage
1 parent 08d24d1 commit 8bc84c6

File tree

15 files changed

+37
-31
lines changed

15 files changed

+37
-31
lines changed

experiments/example/src/definition.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ declare global {
66
namespace Std {
77
// ------------ Std - results ------------
88

9-
interface CustomErrors {
9+
interface Error {
1010
examples: typeof exampleTags
1111
}
1212
}

experiments/example/src/users/say-hi.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { $try, Err, err, fromThrowable, ok } from '@sirutils/std/results'
1+
import { $try, Err, err, fromAsyncThrowable, ok } from '@sirutils/std/results'
22

33
import { exampleTags } from '../tag'
44
import { $getUser } from './get-user'
@@ -18,7 +18,7 @@ export const $sayHi = (name?: string) =>
1818
return ok(`Hi ${found.name}-${found.age}`)
1919
}, exampleTags.get('say-hi'))
2020

21-
export const a = fromThrowable(
21+
export const a = fromAsyncThrowable(
2222
(path: string, data: string) => Bun.write(path, data),
2323
e =>
2424
(e instanceof Err

packages/std/src/io/definition.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/// <reference path="../shared/index.ts" />
2+
/// <reference path="../results/index.ts" />
23

34
import type { ioTags } from './tag'
45

56
declare global {
67
namespace Std {
78
// ------------ Errors ------------
8-
interface CustomErrors {
9+
interface Error {
910
'std/io': typeof ioTags
1011
}
1112
}

packages/std/src/io/tag.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export const ioTags = Tags.create('std/io')
55
.add('invalid-mime')
66

77
// causes
8-
.add('read')
8+
.add('read', '$read')
9+
.add('read-json', '$readJson')

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ export const $readJson = $fn(async <T>(path: string) => {
2929
}
3030

3131
return (await file.json()) as T
32-
}, ioTags.get('read'))
32+
}, ioTags.get('read-json'))

packages/std/src/results/definition.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@ declare global {
1515
namespace Std {
1616
// ------------ Errors ------------
1717

18-
/**
19-
* The interface where we combine tags in each project
20-
*/
21-
interface CustomErrors {}
22-
2318
/**
2419
* Use this instead of CustomErrors. CustomErrors is for overriding
2520
*/
26-
interface Error extends Std.CustomErrors {
21+
interface Error {
2722
'std/results': typeof resultTags
2823
}
2924

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class Tags<U extends [string, string], T extends string> {
1717
return this as Tags<U | [K, V], T>
1818
}
1919

20-
get<K extends LiteralUnion<U['0'], string>>(key: K) {
20+
get<K extends LiteralUnion<U['0'], `?${string}`>>(key: K) {
2121
const found = this.tags.get(key)
2222

2323
if (!found) {

packages/std/src/shared/definition.ts

-12
This file was deleted.

packages/std/src/shared/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import './definition'
2-
31
export * from './types/common'
42

53
export * from './utils/types'
4+
export * from './utils/timing'
65

76
export type * from 'type-fest'

packages/std/src/shared/tag.ts

-3
This file was deleted.
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"name": "alice",
4+
"surname": "zuberg",
5+
"age": 19
6+
}
7+
]

packages/std/tests/io/read/correct.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{]
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, test } from 'bun:test'
2+
import { join } from 'node:path'
3+
4+
import { $read } from '../../../dist/io'
5+
import { Ok, ResultAsync } from '../../../dist/results'
6+
7+
describe('std/io/read', () => {
8+
describe('json', () => {
9+
test('correct', async () => {
10+
const result = $read(join(__dirname, './correct.json'))
11+
const called = await result
12+
13+
expect(result).toBeInstanceOf(ResultAsync)
14+
expect(called).toBeInstanceOf(Ok)
15+
})
16+
})
17+
})

packages/std/tests/results/fn.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, test } from 'bun:test'
22

33
import { $fn, Err, Ok, ResultAsync, err, errAsync, ok, okAsync } from '../../dist/results'
4-
import { wait } from '../../src/shared/utils/timing'
4+
import { wait } from '../../dist/shared'
55

66
describe('std/results/fn', () => {
77
describe('sync', () => {

0 commit comments

Comments
 (0)