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

@future-widget-lab/safe-ops

v0.1.1

Published

A set of helper functions for performing operations safely, preventing runtime errors from disrupting your application.

Readme

@future-widget-lab/safe-ops

A collection of helper functions that ensure operations never fail, even if an error occurs within a callback function. These helpers wrap standard methods in a try-catch block, allowing execution to continue even when errors arise.

Features

  • Prevents entire operations from failing due to individual errors.
  • Safe alternatives to common array methods.
  • -with-errors variants for the common array methods that return both results and error reports.

Installation

npm install @future-widget-lab/safe-ops

Usage

safeguard

Use this helper to safeguard against falsey values

import { safeguard } from '@future-widget-lab/safe-ops';

const values = [0, 1, '', 'hello', null, undefined, false, [], {}];

const truthy = values.filter(safeguard);

console.log(truthy); // Output: [1, 'hello', [], {}]

attempt

Use this helper to safely executes a function and catch any errors that occur.

import { attempt } from '@future-widget-lab/safe-ops';

const riskyOperation = () => JSON.parse('invalid json');

const output = attempt(riskyOperation);

if (output.ok) {
  console.log('Success:', output.result);
} else {
  console.error('Error occurred:', output.error);
}

safeJsonParse

Use this helper to safely parse a possibly faulty JSON object.

safeJsonParse('{"valid": true}', {}); // { "valid": true }

safeJsonParse('invalid json', { userId: 1 }); // {"userId": 1}

safeEvery

Use this helper to determine whether all the members of an array satisfy the specified test, even if an error occurs in some iterations.

import { safeEvery } from '@future-widget-lab/safe-ops';

const numbers = [1, 2, 3, 4, 5];
const allEven = safeEvery(numbers, (num) => {
  if (num === 3) {
    throw new Error('Unexpected number!');
  }

  return num % 2 === 0;
});

console.log(allEven); // false

safeFilter

Use this helper to return the elements of an array that meet the condition specified in a callback function, even if some callbacks throw errors.

import { safeFilter } from '@future-widget-lab/safe-ops';

const numbers = [1, 2, 3, 4, 5];
const filtered = safeFilter(numbers, (num) => {
  if (num === 3) {
    throw new Error('Error processing!');
  }

  return num % 2 === 0;
});

console.log(filtered); // [2, 4]

safeFind

Use this helper to return the first element in the array where the predicate is true, even if an error occurs.

import { safeFind } from '@future-widget-lab/safe-ops';

const numbers = [1, 2, 3, 4, 5];
const found = safeFind(numbers, (num) => {
  if (num === 2) {
    throw new Error('Failure!');
  }

  return num > 3;
});

console.log(found); // 4

safeFindIndex

Use this helper to return the index of the first element where the predicate is true, even if some iterations fail.

import { safeFindIndex } from '@future-widget-lab/safe-ops';

const numbers = [1, 2, 3, 4, 5];
const index = safeFindIndex(numbers, (num) => {
  if (num === 2) {
    throw new Error('Error here!');
  }

  return num > 3;
});

console.log(index); // 3

safeForEach

Use this helper to execute a function for each element in an array without stopping on errors.

import { safeForEach } from '@future-widget-lab/safe-ops';

const numbers = [1, 2, 3, 4, 5];
safeForEach(numbers, (num) => {
  if (num === 3) {
    throw new Error('Processing failed!');
  }

  console.log(num);
});

// 1, 2, 4, 5 (skips 3 due to error but continues)

safeMap

Use this helper to transform an array into a new array while handling errors gracefully.

import { safeMap } from '@future-widget-lab/safe-ops';

const numbers = [1, 2, 3, 4, 5];
const mapped = safeMap(numbers, (num) => {
  if (num === 4) {
    throw new Error('Error transforming!');
  }

  return num * 2;
});

console.log(mapped); // [2, 4, 6, 10]

safeReduce

Use this helper to accumulate a result from an array, ensuring errors don't interrupt execution.

import { safeReduce } from '@future-widget-lab/safe-ops';

const numbers = [1, 2, 3, 4, 5];
const sum = safeReduce(
  numbers,
  (acc, num) => {
    if (num === 3) {
      throw new Error('Summation error!');
    }

    return acc + num;
  },
  0
);

console.log(sum); // 12

safeSome

Use this helper to determine whether at least one element in the array satisfies the specified test, even if an error occurs.

import { safeSome } from '@future-widget-lab/safe-ops';

const numbers = [1, 2, 3, 4, 5];
const hasEven = safeSome(numbers, (num) => {
  if (num === 3) {
    throw new Error('Failure!');
  }

  return num % 2 === 0;
});

console.log(hasEven); // true

-with-errors Variants

The -with-errors variants return both results and an errors array containing details of failed items.

import { safeMapWithErrors } from '@future-widget-lab/safe-ops';

const numbers = [1, 2, 3, 4, 5];
const { result, errors } = safeMapWithErrors(numbers, (num) => {
  if (num === 4) {
    throw new Error('Error mapping!');
  }

  return num * 2;
});

console.log(result); // [2, 4, 6, 10]
console.log(errors); // [{ error: [Error: Error mapping!], item: 4, index: 3 }]

Important Note

safeMap (and similar array helpers) operate synchronously and do not handle asynchronous transformations. If you pass an async function, the result will be an array of unresolved promises, just like with Array.prototype.map.

For asynchronous cases, consider using Promise.all, p-map, or manually handling errors within your async function.

License

MIT