Skip to content

Latest commit

 

History

History
100 lines (78 loc) · 2.8 KB

README.md

File metadata and controls

100 lines (78 loc) · 2.8 KB

@weis-guys/result

Docs   •   github   •   npm

Contribute

Always open to ideas. Positive or negative, all are welcome. Feel free to contribute an issue or PR.

Installation

npm install @weis-guys/result
yarn add @weis-guys/result
pnpm add @weis-guys/result
bun add @weis-guys/result

Getting Started

import Result from '@weis-guys/result'

const okResult = Result.ok( 'good value' )
// { success: true, value: "good value"  }

const warningResult = Result.okWithWarning( 'good value', 'some kind of warning' )
// { success: true, value: "good value", warning: "some kind of warning" }

const errorResult = Result.error( 'some kind of error' )
// { success: false, error: "some kind of error" }

/**
 * this function could return a value, an error, or a value with a warning
 */
function someFn () {
    const items = [ okResult, warningResult, errorResult ]
    return items[ Math.floor( Math.random() * items.length ) ]
}

const result = someFn()
console.log( { result } )

if ( result.success ) {
    console.log( result.value )
    // 'good value'

    result.warning && console.warn( result.warning )
    // 'some kind of warning'
} else {
    console.error( result.error )
    // 'some kind of error'
}

const value = Result.getValue( result )
console.log( value )
// 'good value' | undefined

const warning = Result.getWarning( result )
console.log( warning )
// 'some kind of warning' | undefined

const error = Result.getError( result )
console.log( error )
// 'some kind of error' | undefined

TODO

  • Auto publish to npm
    • GitHub Actions ?