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

clojure-cond-js

v1.0.2

Published

Inspired by clojure's `cond` and `condp` functions, this library is meant to offer a few options for writing multi-branch conditionals in a fairly-concise way.

Downloads

2

Readme

clojure-cond-js

Inspired by clojure's cond and condp functions, this library is meant to offer a few options for writing multi-branch conditionals in a fairly-concise way.

cond

Takes predicate-callback pairs plus an additional callback and invokes the first callback for which its respective predicate returns a truthy value. If all pairs return falsey values, the fallback parameter will be invoked. Returns the return value of whichever callback was invoked.

Useful for multi-branch conditionals

const condTest = cond._(
  [
    [
      'true',                             // a logical true
      () => {                             // invokes cb and returns its return value
        console.log('true string');
        return 'true string';
      }
    ],
    [
      () => true, 
      () => console.log('true cb')        // won't be reached, would return undefined
    ],
    [false, () => console.log('false')],
    [true, () => console.log('true')]     // won't be reached, would return undefined
  ],
  () => console.log('fallback')           // would be called if no matches
);

condp

Takes unary predicate, pairs, and an additional fallback. Each pair's first element is passed to the predicate. If the predicate returns a truthy, that pair's second element is the match. The match may be an expression to return, or a callback which will receive the truthy value and return a final value from condp. If there are no matches, the fallback is invoked and result (or null) is returned when provided or an exception is thrown when not provided.

Useful for setting the value of a variable based on logic that is a little too complex for a ternary, etc.

const condpTest = cond.p(
  (n) => n === 4,
  [
    [1, 'one'],
    [2, 'two'],
    [3, 'three'],
    [4, 'four'],            // will return 'four'
    [4, () => 'other four'] // will not reach here anyway
  ],
  () => 4                   // if no matches, this would be called
);

...

const condpTest = cond.p(
  (arr) => arr.length,
  [
    [[], (l) => l],        // won't be invoked, [].length is falsey
    [[1, 2, 3], (l) => l], // will be invoked and return length of the array
    [['asdf'], 'asdf']
  ],
  () => 4
);

condo

Takes predicate-callback tuples and invokes any/all callbacks for which its respective predicate returns true. Returns return value from last matching clause, or undefined.

Could be useful for situations where any number of side effects are desired based on different conditions.

const condoTest = cond.o([
  [true, () => console.log('true')],    // will be logged
  [false, () => console.log('false')],
  [
    () => true,                
    () => {
      console.log('true cb')           // will also be logged
      return 'some return value';      // will be returned
    }
  ],
  [() => false, () => console.log('false cb')]
]);