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

resultra

v0.1.4

Published

Rust inspired Result type

Downloads

28

Readme

Resultra

Rust inspired result structure for javascript

Example

import { ok, err } from 'resultra'
import { parseInt } from 'reusltra/utils'

function parseName() {
  const name = prompt('Enter your name');
  return name
    ? ok(name)
    : err(new Error('name is required'))
}

function parseAge() {
  const age = prompt('Enter your age');
  return age ? parseInt(age, 10) : err(new Error('Age is required'))
}

const personResult = parseName()
  .andThen(name =>
    parseAge()
      .map(age =>
        ({ name, age })
      )
  );

if (personResult.ok()) {
    const person = personResult.unwrap();
    console.log('Hello', person.name)
    console.log('You are', person.age, 'years old')
} else {
    console.log('Error:', personResult.error)
}

Constructors

ok

Creates an OkResult

const okResult = ok(1);
const okValue = okResult.value

err

Creates an ErrResult

const errResult = err('error');
const errValue = errResult.error

Methods

ok

Returns true if the result is an OkResult

Example:

map

Maps the value of the result if it is an OkResult

andThen

Maps the value of the result to another result if it is an OkResult

unwrap

Unwraps the value of the result if it is an OkResult. Throws an error if the result is an ErrResult.

Utils

catchError

Catches errors thrown by a function and returns a Result

const result = catchError(() => {
  throw new Error('error')
})
assert(!result.ok(), 'result should be an error here')
assert(result.error.message === 'error')

catchAsync

Catches errors thrown by an async function and returns a Promise of a Result

const result = await catchAsync(async () => {
  throw new Error('error')
})
assert(!result.ok(), 'result should be an error here')
assert(result.error.message === 'error')

collect

Collects an array of results into a single result of all the values. If any of the results are errors, the first error is returned.

const results = collect([
  ok(1),
  ok(2),
  ok(3),
])
assert(results.ok(), 'results should be ok')
assert(results.value.length === 3, 'results should have 3 values')
const results = collect([
  ok(1),
  err('error'),
  ok(3),
])

assert(!results.ok(), 'results should be an error')
assert(results.error === 'error', 'error should be "error"')

collectAsync

Collects an array of async results into a single result of all the values. If any of the results are errors, the first error is returned.

const results = await collectAsync([
  Promise.resolve(ok(1)),
  Promise.resolve(ok(2)),
  Promise.resolve(ok(3)),
])
assert(results.ok(), 'results should be ok')
assert(results.value.length === 3, 'results should have 3 values')
const results = await collectAsync([
  Promise.resolve(ok(1)),
  Promise.resolve(err('error')),
  Promise.resolve(ok(3)),
])

assert(!results.ok(), 'results should be an error')
assert(results.error === 'error', 'error should be "error"')

successes

Collects an array of results into an array of all successful result values.

const results = successes([
  ok(1),
  ok(2),
  ok(3),
])
assert(results.length === 3, 'results should have 3 values')
const results = successes([
  ok(1),
  err('error'),
  ok(3),
])

assert(results.length === 2, 'results should be equal to [1,3]')

successesAsync

Collects an array of async results into an array of all successful result values.

const results = await successesAsync([
  Promise.resolve(ok(1)),
  Promise.resolve(ok(2)),
  Promise.resolve(ok(3)),
])

assert(results.length === 3, 'results should have 3 values')
const results = await successesAsync([
  Promise.resolve(ok(1)),
  Promise.resolve(err('error')),
  Promise.reject(ok(3)),
])

assert(results.length === 1, 'results should be equal to [1]')

fromJsonRpc

Creates a Result from a JSON RPC response

const result = fromJsonRpc({
  result: 1,
})
assert(result.ok(), 'result should be ok')
assert(result.value === 1, 'result should be equal to 1')
const result = fromJsonRpc({
  error: new Error('error'),
})

assert(!result.ok(), 'result should be an error')
assert(result.error.message === 'error', 'error should be "error"')

fromPromise

Creates a Promise of Result from a Promise. It will be ok, if the promise resolves, or an error, if the promise rejects.

const result = await fromPromise(Promise.resolve(1))
assert(result.ok(), 'result should be ok')
assert(result.value === 1, 'result should be equal to 1')
const result = await fromPromise(Promise.reject(new Error('error')))
assert(!result.ok(), 'result should be an error')
assert(result.error.message === 'error', 'error should be "error"')

parseInt

Creates a Result of number from a string and a base. It will be ok, if the string can be parsed, or an error, if the string cannot be parsed.

const result = parseInt('1', 10)
assert(result.ok(), 'result should be ok')
assert(result.value === 1, 'result should be equal to 1')
const result = parseInt('a', 10)
assert(!result.ok(), 'result should be an error')
assert(result.error.message === 'Failed to parse a as number', 'error should be "Failed to parse a as number"')

parseFloat

Creates a Result of number from a string. It will be ok, if the string can be parsed, or an error, if the string cannot be parsed.

const result = parseFloat('1.1')
assert(result.ok(), 'result should be ok')
assert(result.value === 1.1, 'result should be equal to 1.1')
const result = parseFloat('a')
assert(!result.ok(), 'result should be an error')
assert(result.error.message === 'Failed to parse a as number', 'error should be "Failed to parse a as number"')

assert

Returns ok result if value is truthy, err with specified message if not

const result = assert(1, 'error')
assert(result.ok(), 'result should be ok')
assert(result.value === 1, 'result should be equal to 1')
const result = assert(0, 'error')
assert(!result.ok(), 'result should be an error')
assert(result.error.message === 'error', 'error should be "error"')

validate

Checks if predicate returns true for a value, returns ok result if true, err with specified message if not

const result = validate(1, (value) => value === 1, 'error')
assert(result.ok(), 'result should be ok')
assert(result.value === 1, 'result should be equal to 1')
const result = validate(0, (value) => value === 1, 'error')
assert(!result.ok(), 'result should be an error')
assert(result.error.message === 'error', 'error should be "error"')