npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

object-result

v1.2.0

Published

Simple Rust-like Result object for basic error handling.

Downloads

17

Readme

object-result

Result object represent either an error or a successful computation.

Usage

Results are never created directly, instead object-assign exposes two factory functions, createErr and createOk, that construct result objects representing an error and an ok respectively.

  let objectResult = require('object-result'),
      ok = objectResult.ok,
      err = objectResult.err;

  // creates a success result with an object used for the success value
  let goodKitty = ok({ a : 100, b : "kitty" });

  // creates an error result with an object used for the error value
  let badKitty = err({ msg : "bad stuff, oh no!" });

Sometimes you want to do some transformation on a success or error -- but you dont want to modify the relative success-state of the result. In this case, you would use the map/map_err methods.

  // map the goodKitty's success value from the object (from above) to just
  // a string.
  goodKitty.map(function(value) {
    return value.b;
  });

  // map the badKitty's error value from the object above to just a string
  badKitty.map_err(function(errVal) {
    return errVal.msg;
  });

  // map only effects ok's and map_err only effects err's
  // so...

  goodKitty.map_err(function(val) {
    return "super " + val;
  });

  badKitty.map(function(errMsg) {
    return "Error: " + errMsg;
  });

  // have no effect

Sometimes more flexibility is needed -- in particular you may need continue a computation but only if no error has occured. In this case, one should use the and_then/or_else methods.

  // and_then is good for continuing successful computations
  let newKitty = goodKitty.and_then(function(value) {
    return some_operation_that_may_fail(value);
  });

  // or_else is good for handling errors -- or transforming them into successes
  let newKitty = badKitty.or_else(function(value) {
    return some_operation_that_may_fix_error(value);
  });

It can be useful to chain or_else or and_then methods together. However, as a word of caution -- generally it is a mistake to chain or_else and and_then methods simultaneously. Use multiple and_then's to continue a successful computation, or use multiple or_else's to do complex error management, but don't try to do both at the same time! (it can get confusing very quickly)

Usually when you want to chain and_then with an or_else you should really be using the match method. Match is effectively and_then and or_else together, but because it isn't chained it is less suprising.

  let someKitty = goodKitty.match({
    ok  : (val) => ok(val * 2),
    err : (errVal) => ok(10)
  });