Skip to content

Commit 84d5c5f

Browse files
2.0.0
1 parent cb2e16c commit 84d5c5f

File tree

12 files changed

+402
-317
lines changed

12 files changed

+402
-317
lines changed

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
2-
"typescript.tsdk": "node_modules\\typescript\\lib"
2+
"typescript.tsdk": "node_modules\\typescript\\lib",
3+
"typescript.enablePromptUseWorkspaceTsdk": true,
4+
"git.postCommitCommand": "none",
5+
"git.enableSmartCommit": true,
6+
"terminal.integrated.defaultProfile.windows": "Git Bash"
37
}

README.md

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,50 +49,61 @@ bun add @weis-guys/result
4949
## Getting Started
5050

5151
```ts
52-
import Result from '@weis-guys/result'
52+
import { Result } from '@weis-guys/result'
5353

54-
const okResult = Result.ok( 'good value' )
55-
// { success: true, value: "good value" }
54+
console.log( Result.ok( 'good data' ) )
55+
// { data: "good data" }
5656

57-
const warningResult = Result.okWithWarning( 'good value', 'some kind of warning' )
58-
// { success: true, value: "good value", warning: "some kind of warning" }
57+
console.log( Result( { data: 'good data' } ) )
58+
// { data: "good data" }
5959

60-
const errorResult = Result.error( 'some kind of error' )
61-
// { success: false, error: "some kind of error" }
60+
console.log( Result.ok() )
61+
// { data: {} }
62+
63+
console.log( Result.err( 'some kind of error' ) )
64+
// { error: "some kind of error" }
65+
66+
console.log( Result.warn( 'some kind of warning' ) )
67+
// { warning: "some kind of warning" }
68+
69+
console.log( Result( { data: 'good data', warning: 'some kind of warning' } ) )
70+
// { data: "good data", warning: "some kind of warning" }
71+
72+
console.log( Result( { data: 'good data', error: 'some kind of error' } ) )
73+
// { data: "good data", error: "some kind of error" }
74+
75+
console.log( Result( { data: 'good data', error: 'some kind of error', warning: 'some kind of warning' } ) )
76+
// { data: "good data", error: "some kind of error", warning: "some kind of warning" }
6277

6378
/**
64-
* this function could return a value, an error, or a value with a warning
79+
* this function returns some kind of result that isn't known until runtime
6580
*/
6681
function someFn () {
67-
const items = [ okResult, warningResult, errorResult ]
82+
const items = [
83+
Result.ok( 'good data' ),
84+
Result.err( 'some kind of error' ),
85+
Result.warn( 'some kind of warning' ),
86+
Result( { data: 'good data', warning: 'some kind of warning' } ),
87+
]
6888
return items[ Math.floor( Math.random() * items.length ) ]
6989
}
7090

71-
const result = someFn()
72-
console.log( { result } )
91+
const { data, error, warning } = someFn()
7392

74-
if ( result.success ) {
75-
console.log( result.value )
76-
// 'good value'
93+
if ( data ) {
94+
console.log( data )
95+
// 'good data'
96+
}
7797

78-
result.warning && console.warn( result.warning )
98+
if ( warning ) {
99+
console.log( warning )
79100
// 'some kind of warning'
80-
} else {
81-
console.error( result.error )
82-
// 'some kind of error'
83101
}
84102

85-
const value = Result.getValue( result )
86-
console.log( value )
87-
// 'good value' | undefined
88-
89-
const warning = Result.getWarning( result )
90-
console.log( warning )
91-
// 'some kind of warning' | undefined
92-
93-
const error = Result.getError( result )
94-
console.log( error )
95-
// 'some kind of error' | undefined
103+
if ( error ) {
104+
console.log( error )
105+
// 'some kind of error'
106+
}
96107
```
97108

98109
## TODO

docs/examples.ts

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,55 @@
1-
import Result from '@weis-guys/result'
1+
import { Result } from '@weis-guys/result'
22

3-
const okResult = Result.ok( 'good value' )
4-
// { success: true, value: "good value" }
3+
console.log( Result.ok( 'good data' ) )
4+
// { data: "good data" }
55

6-
const warningResult = Result.okWithWarning( 'good value', 'some kind of warning' )
7-
// { success: true, value: "good value", warning: "some kind of warning" }
6+
console.log( Result( { data: 'good data' } ) )
7+
// { data: "good data" }
88

9-
const errorResult = Result.error( 'some kind of error' )
10-
// { success: false, error: "some kind of error" }
9+
console.log( Result.ok() )
10+
// { data: {} }
11+
12+
console.log( Result.err( 'some kind of error' ) )
13+
// { error: "some kind of error" }
14+
15+
console.log( Result.warn( 'some kind of warning' ) )
16+
// { warning: "some kind of warning" }
17+
18+
console.log( Result( { data: 'good data', warning: 'some kind of warning' } ) )
19+
// { data: "good data", warning: "some kind of warning" }
20+
21+
console.log( Result( { data: 'good data', error: 'some kind of error' } ) )
22+
// { data: "good data", error: "some kind of error" }
23+
24+
console.log( Result( { data: 'good data', error: 'some kind of error', warning: 'some kind of warning' } ) )
25+
// { data: "good data", error: "some kind of error", warning: "some kind of warning" }
1126

1227
/**
13-
* this function could return a value, an error, or a value with a warning
28+
* this function returns some kind of result that isn't known until runtime
1429
*/
1530
function someFn () {
16-
const items = [ okResult, warningResult, errorResult ]
31+
const items = [
32+
Result.ok( 'good data' ),
33+
Result.err( 'some kind of error' ),
34+
Result.warn( 'some kind of warning' ),
35+
Result( { data: 'good data', warning: 'some kind of warning' } ),
36+
]
1737
return items[ Math.floor( Math.random() * items.length ) ]
1838
}
1939

20-
const result = someFn()
21-
console.log( { result } )
40+
const { data, error, warning } = someFn()
2241

23-
if ( result.success ) {
24-
console.log( result.value )
25-
// 'good value'
42+
if ( data ) {
43+
console.log( data )
44+
// 'good data'
45+
}
2646

27-
result.warning && console.warn( result.warning )
47+
if ( warning ) {
48+
console.log( warning )
2849
// 'some kind of warning'
29-
} else {
30-
console.error( result.error )
31-
// 'some kind of error'
3250
}
3351

34-
const value = Result.getValue( result )
35-
console.log( value )
36-
// 'good value' | undefined
37-
38-
const warning = Result.getWarning( result )
39-
console.log( warning )
40-
// 'some kind of warning' | undefined
41-
42-
const error = Result.getError( result )
43-
console.log( error )
44-
// 'some kind of error' | undefined
52+
if ( error ) {
53+
console.log( error )
54+
// 'some kind of error'
55+
}

package-lock.json

Lines changed: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@weis-guys/result",
3-
"version": "1.0.2",
3+
"version": "2.0.0",
44
"main": "index.js",
55
"type": "module",
66
"author": "Jacob Weisenburger",
@@ -14,13 +14,19 @@
1414
"scripts": {
1515
"test": "bun test --watch",
1616
"examples": "clear && bun run --watch docs/examples.ts",
17-
"build": "clear && bun --watch run scripts/build.ts",
18-
"publish": "clear && bun run scripts/publish.ts",
19-
"cmd": "cmd.exe /c start cmd /k wsl --cd ~/software/weis-guys/result"
17+
"build": "cls && bun run scripts/build.ts",
18+
"build.watch": "cls && bun --watch run scripts/build.ts",
19+
"build.cmd": "start cmd /k npm run build",
20+
"pub": "clear && bun run scripts/publish.ts",
21+
"build.pub": "npm run build && npm run pub",
22+
"cmd": "start cmd /k"
2023
},
2124
"devDependencies": {
2225
"@types/bun": "latest",
2326
"bun-plugin-dts": "^0.2.1",
24-
"@weis-guys/result": "*"
27+
"typescript": "^5.8.2"
28+
},
29+
"dependencies": {
30+
"@weis-guys/result": "^2.0.0"
2531
}
2632
}

scripts/build.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BuildConfig } from 'bun'
1+
import { $, type BuildConfig } from 'bun'
22
import dts from 'bun-plugin-dts'
33
import { copyFile, rm } from 'node:fs/promises'
44
import * as Path from 'node:path'
@@ -35,9 +35,8 @@ await Promise.resolve()
3535
const config: BuildConfig = {
3636
entrypoints: [ './src/index.ts' ],
3737
format: 'esm',
38-
minify: true,
39-
sourcemap: 'inline',
40-
target: 'node',
38+
// minify: true,
39+
// sourcemap: 'inline',
4140
plugins: [
4241
dts( {
4342
output: {
@@ -55,9 +54,9 @@ await Promise.resolve()
5554
} )
5655

5756
.then( async () => {
58-
const section = 'Reduced package.json: written'
59-
const distPath = Path.join( dist, 'package.json' )
6057
const { scripts, devDependencies, ...reduced } = packageJSON
58+
const section = `Reduced package.json: written: ${ reduced.version }`
59+
const distPath = Path.join( dist, 'package.json' )
6160

6261
await Bun.write( distPath, JSON.stringify( reduced, null, 4 ) )
6362
.then( () => console.log( section ) )
@@ -87,5 +86,14 @@ await Promise.resolve()
8786
.catch( logError( { section, srcPath, distPath } ) )
8887
} )
8988

89+
.then( async () => {
90+
const section = 'src/index.ts'
91+
const srcPath = Path.join( root, 'src/index.ts' )
92+
const distPath = Path.join( dist, 'index.ts' )
93+
await copyFile( srcPath, distPath )
94+
.then( () => console.log( 'src/index.ts: copied' ) )
95+
.catch( logError( { section, srcPath, distPath } ) )
96+
} )
97+
9098
.then( () => console.log( 'Build: done' ) )
9199
.catch( logError( { path: import.meta.path } ) )

0 commit comments

Comments
 (0)