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

tryordefault

v1.0.1

Published

Try to execute a function, return default value if failed

Downloads

4

Readme

try-or-default

Try to execute a function, or return default value if it fails (exception was rised). This helper package can be used in an enviroment that exceptions are tolerable. Where you just want a fallback value to go on and dont really care about handling occured exception.

Work well with Node.js. I didn't try in browser yet.

Installation

Install using npm

npm install tryordefault

Example of usage

To wrap something that may break, just fallback to default value on failed

  const {tryOrDefault} = require('tryordefault');
  
  // in this example, this function which will surely throw an exception
  function functionThatMayFail() {
    return JSON.parse("{]"); 
  }
  
  // basic usage
  let parsedObject = tryOrDefault(
    functionThatMyFail,     // function that has potential exception
    {}                      // default value to be returned when exception occurs
  ));

To define constant

  const {tryOrDefault} = require('tryordefault');
  
  //
  // Instead of this ...
  //
  let parsedObject = {};
  try {
    parsedObject = JSON.parse(somethingThatMayBeNotValidJSONString);  
  } catch (e) {}
  
  //
  // Now you can define a const like this
  //
  const parsedObject = tryOrDefault(
    () => JSON.parse(somethingThatMayBeNotValidJSONString),     // function that has potential exception
    {}                                                          // default value to be returned when exception occurs
  ));

To add default value to async function

  // use tryOrDefaultAsync function, if you work inside async context or to deal with async function.
  const {tryOrDefaultAsync} = require('tryordefault');
  const puppeteer           = require('puppeteer');
  
  (async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    
    // Let's navigate to IMdb,
    // and try to fetch some information about "Justice League" (did u watch it? :3)
    await page.goto('https://www.imdb.com/title/tt0974015/');   
    
    // Try to get the movie title
    // For some reason, if h1 is not there, blank will be returned
    const title = await tryOrDefaultAsync(
        async () => await page.$eval("h1[itemprop='name']", h1 => h1.innerText),
        ""
    );
    
    // Try to get the release year
    // some title on imdb doesn't have #titleYear, so it's be better to fallback to blank string
    // You may also want to take advantage of the function, to define constant
    const year = await tryOrDefaultAsync(
        async () => await page.$eval("#titleYear", yearEl => yearEl.innerText),
        ""
    );
    
    console.log({
      title: title,
      year:  year
    })

    await browser.close();
  })();