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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@jmjf/result

v1.1.0

Published

A simple Result (either) class for TypeScript projects

Readme

result

A simple Result (either) type for TypeScript applications.

You need this if

  • You're building a TypeScript application.
  • You have a function that could return data you need or an error.
  • You want to detect errors easily.

What it does

If a variable x is of type Result<okType, errorType> then either

  • x.value !== null && x.error === null or
  • x.value === null && x.error !== null

There is no case where x.value !== null and x.error !== null

It is possible for x.value === null and x.error === null if x = okResult(null), but only because you did it on purpose.

The code below acts as a type guard. Inside the if, x.value === null.

if (x.error !== null) {
   // here, x.value === null and x.error is errorType

   return  // no way out if an error
}

// x.value !== null && x.error === null and x.value is okType

If you've ever coded Go, the pattern above should be familiar. The happy path is on the left and the error path is on the right (indented). You can choose the reverse pattern. Whichever you choose, consistency makes code easier to understand.

How to use

  • Declare the function to return a Result<okType, errorType>.
  • Return an okResult or errorResult.
  • if (result.error !== null) { /* handle error */ }

Example

The example below uses primitive types, but Result<DirEnt[], NotFoundError | IOError> works too if you have complex types and custom error types.

import { existsSync, lstatSync, mkdirSync, readdirSync } from 'node:fs';
import path from 'node:path';
import { errorResult, okResult, type Result } from '@jmjf/result';

// Declare the function to return a Result<okType, errorType>
export function getInputFiles(inPath: string): Result<string[], string> {
   if (!existsSync(inPath)) {
      // return an errorResult
      return errorResult(`ERROR: ${inPath} not found`);
   }

   if (!lstatSync(inPath).isDirectory()) {
      // return an okResult
      return okResult([path.resolve(inPath)]);
   }

   try {
      const fileNames = readdirSync(inPath).filter((fn) =>
         ['.yaml', '.yml', '.json'].includes(path.extname(fn).toLowerCase()),
      );
      if (fileNames.length === 0) {
         // return an errorResult
         return errorResult(`ERROR: no YAML or JSON files found in ${inPath}`);
      }
      // return an okResult
      return okResult(fileNames.map((fn) => path.resolve(inPath, fn)) as string[]);
   } catch (e) {
      // Result is handy for wrapping thrown errors
      // return an errorResult
      return errorResult(`ERROR: ${e.name} ${e.code} reading directory ${inPath}`);
   }
}

// a function that logs result
function getFirstInputFile(inPath: string) {
   const inputFilesResult = getInputFiles(inPath);
   if (inputFilesResult.error !== null) {
      console.log(inputFilesResult.error);
      return;
   }
   // because we returned in the if, value must be present
   console.log(inputFilesResult.value[0]);
}

// a function that returns a result based on the result it gets
function getInputFileCount(inPath: string): Result<number, string> {
   const inputFilesResult = getInputFiles(inPath);
   if (inputFilesResult.error !== null) {
      return inputFilesResult; 
      // this works because both errorTypes are the same
      // if errorTypes are different, convert it here
   }

   return okResult(inputFilesResult.value.length);
}