Skip to content

Result type representing either a success or failure

License

Notifications You must be signed in to change notification settings

NickKelly1/nkp-result

Repository files navigation

@nkp/result

npm version deploy status known vulnerabilities

Minimal zero dependency utility library for the Result type. Result represents the output of an operation that either succeded or failed.

import { Result } from '@nkp/result';

function work(): Result<number, string> {
  const input = 'this is user input';
  if (input.length >= 5){ 
    return Result.err('input must be less-than 5 characters long');
  }
  return Result.ok(input.length);
}


const result = work();

if (Result.isOk(result)) {
  const success: number = reuslt.value;
}

if (Result.isErr(result)) {
  const message: string = result.value;
}

Table of contents

Exports

@nkp/result exports both CommonJS and ES modules.

Installation

npm

npm install @nkp/result

yarn

yarn add @nkp/result

pnpm

pnpm add @nkp/result

Publishing

To a release a new version:

  1. Update the version number in package.json
  2. Push the new version to the master branch on GitHub
  3. Create a new release on GitHub for the latest version

This will trigger a GitHub action that tests and publishes the npm package.