1.0.8 • Published 5 years ago

result-tools v1.0.8

Weekly downloads
31
License
ISC
Repository
-
Last release
5 years ago

ResultTools

Chat on Gitter CircleCI Coverage Status dependencies Status Known Vulnerabilities

ResultTools is a standard library for JavaScript functional programming.

The ResultTools library goal is to provide developers an auxiliary tool for modeling results of sync and async operations. Annuls the try/catch need, therefor, allowing better control of sequence operations, error handling and propagation.

Async operations can be performed by the _try and trySync operation to perform sync operations. The operation results are, Ok(success Value) or Error(error Value) and can be accessed through the get or orElse operations. These results offer tests through the isOk or isError operations. Based on the operation results it is possible to use fluent features, such as .chain, .chainSync, .serial, .serialSync, .map and .mapSync to execute sync and async sequence operations in a controlled way. Also, there is no need to perform the try operation, since it can be used in isolation.

Installation

ResultTools can be installed through npm:

$ npm install result-tools

Example

  • _try(fn) Used to execute async operations. The operation result can be either Ok(success Value) or Error(error Value).

    error case:
    const Result = require('result-tool');
    
    const fnPromise = () => Promise.reject(-1);
    
    Result._try(fnPromise);
    
    // ==> Result.Error(-1)
    success case:
    const Result = require('result-tool');
    
    const fnPromise = () => Promise.resolve('Annuls the try/catch need');
    
    Result._try(fnPromise);
    
    // ==> Result.Ok('Annuls the try/catch need')
  • trySync(fn) Used to execute sync operations. The operation result can be either Ok(success Value) or Error(error Value).

    error case:
    const Result = require('result-tool');
    
    const fnPromise = () => Error(-1);
    
    Result.trySync(fnPromise);
    
    // ==> Result.Error(-1)
    success case:
    const Result = require('result-tool');
    
    const fnPromise = () => Ok('Annuls the try/catch need');
    
    Result.trySync(fnPromise);
    
    // ==> Result.Ok('Annuls the try/catch need')
  • chain(fn) Used to execute async operations where the arguments are the results from the last operation, generating a new result.
error case:
const Result = require('result-tool');

const fnPromise = () => Promise.reject(-1);

//operation will not be performed
const fnChain = (arg) => Promise.resolve(arg);

Result
  ._try(fnPromise)
  .chain(fnChain)
  .run();

// ==> Result.Error(-1)
success case:
const Result = require('result-tool');

const fnPromise = () => Promise.resolve('Annuls the');

const fnChain = (arg) => Promise.resolve(`${arg} try/catch need`);

Result
  ._try(fnPromise)
  .chain(fnChain)
  .run();

// ==> Result.Ok('Annuls the try/catch need')
  • chainSync(fn) Used to execute sync operations where the arguments are the results from the last operation, generating a new result.

    error case:
    const Result = require('result-tool');
    
    const fnPromise = () => Error(-1);
    
    //operation will not be performed
    const fnChain = (arg) => Ok(arg);
    
    Result
      .trySync(fnPromise)
      .chainSync(fnChain)
      .run();
    
    // ==> Result.Error(-1)
    success case:
    const Result = require('result-tool');
    
    const fnPromise = () => Ok('Annuls the');
    
    const fnChain = (arg) => Ok(`${arg} try/catch need`);
    
    Result
      .trySync(fnPromise)
      .chainSync(fnChain)
      .run();
    
    // ==> Result.Ok('Annuls the try/catch need')
  • serial(fn, fnArgs) Used to execute async operations where the arguments could be received, generating a new result.

    error case:
    const Result = require('result-tool');
    
    const fnPromise = () => Promise.reject(-1);
    
    //operation will not be performed
    const fnSerial = (arg) => Promise.resolve(arg);
    
    Result
      ._try(fnPromise)
      .serial(fnSerial, 2)
      .run();
    
    // ==> Result.Error(-1)
    success case:
    const Result = require('result-tool');
    
    const fnPromise = () => Promise.resolve(4);
    
    const fnSerial = (arg) => Promise.resolve(arg);
    
    Result
      ._try(fnPromise)
      .serial(fnSerial, 2)
      .run();
    
    // ==> Result.Ok([4, 2])
  • serialSync(fn, fnArgs) Used to execute sync operations where the arguments could be received, generating a new result.

    error case:
    const Result = require('result-tool');
    
    const fnPromise = () => Error(-1);
    
    //operation will not be performed
    const fnSerial = (arg) => Ok(arg);
    
    Result
      .trySync(fnPromise)
      .serialSync(fnSerial, 2)
      .run();
    
    // ==> Result.Error(-1)
    success case:
    const Result = require('result-tool');
    
    const fnPromise = () => Ok(4);
    
    const fnSerial = (arg) => Ok(arg);
    
    Result
      .trySync(fnPromise)
      .serialSync(fnSerial, 2)
      .run();
    
    // ==> Result.Ok([4, 2])
  • map(fn) Used to execute async transformation operations where the arguments are the results from the last operation, generating a new result.

    error case:
    const Result = require('result-tool');
    
    const fnPromise = () => Promise.reject(-1);
    
    //operation will not be performed
    const fnMap = (arg) => Promise.resolve(arg);
    
    Result
      ._try(fnPromise)
      .map(fnMap)
      .run();
    
    // ==> Result.Error(-1)
    success case:
    const Result = require('result-tool');
    
    const fnPromise = () => Promise.resolve('Annuls the');
    
    const fnMap = (arg) => Promise.resolve(`${arg} try/catch need`);
    
    Result
      ._try(fnPromise)
      .map(fnMap)
      .run();
    
    // ==> Result.Ok('Annuls the try/catch need')
  • mapSync(fn) Used to execute sync transformation operations where the arguments are the results from the last operation, generating a new result.

    error case:
    const Result = require('result-tool');
    
    const fnPromise = () => Error(-1);
    
    //operation will not be performed
    const fnMap = (arg) => Ok(arg);
    
    Result
      .trySync(fnPromise)
      .mapSync(fnMap)
      .run();
    
    // ==> Result.Error(-1)
    success case:
    const Result = require('result-tool');
    
    const fnPromise = () => Ok('Annuls the');
    
    const fnMap = (arg) => Ok(`${arg} try/catch need`);
    
    Result
      .trySync(fnPromise)
      .mapSync(fnMap)
      .run();
    
    // ==> Result.Ok('Annuls the try/catch need')
  • get Used to access the result value.

    error case:
    const Result = require('result-tool');
    
    const fnPromise = () => Error(-1);
    
    Result
      .trySync(fnPromise)
      .get();
    
    // ==> -1
    success case:
    const Result = require('result-tool');
    
    const fnPromise = () => Ok('Annuls the try/catch need');
    
    Result
      .trySync(fnPromise)
      .get();
    
    // ==> 'Annuls the try/catch need'
  • orElse(arg) Used to access the success value or argument value in error cases.

    error case:
    const Result = require('result-tool');
    
    const fnPromise = () => Error(-1);
    
    Result
      .trySync(fnPromise)
      .orElse('There is an error');
    
    // ==> There is an error
    success case:
    const Result = require('result-tool');
    
    const fnPromise = () => Ok('Annuls the try/catch need');
    
    Result
      .trySync(fnPromise)
      .orElse('There is an error');
    
    // ==> 'Annuls the try/catch need'
  • isOk Used to test the result value. Returns True if the result value is Ok.

    error case:
    const Result = require('result-tool');
    
    const fnPromise = () => Error(-1);
    
    Result
      .trySync(fnPromise)
      .isOk();
    
    // ==> false
    success case:
    const Result = require('result-tool');
    
    const fnPromise = () => Ok('Annuls the try/catch need');
    
    Result
      .trySync(fnPromise)
      .isOk();
    
    // ==> true
  • isError Used to test the result value. Returns True if the result value is Error.

error case:
const Result = require('result-tool');

const fnPromise = () => Error(-1);

Result
  .trySync(fnPromise)
  .isError();

// ==> true
success case:
const Result = require('result-tool');

const fnPromise = () => Ok('Annuls the try/catch need');

Result
  .trySync(fnPromise)
  .isError();

// ==> false

Supported platforms

Result Tools is written for the ECMAScript 2015 platform. If you're running your program in an older platform, you'll need es5-shim and es6-shim.

Having trouble using ResultTools?
ResultTools discussion channels and information

License

See the LICENSE file in this repository for detailed information.

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago