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

@orioro/cascade

v3.0.0

Published

Small set of utility functions that help implement conditional selection/execution of options/functions. Summary: - Provide a set of ordered alternatives - Each alternative consists of a `criteria` and a `value` - For each alternative, test the criteria a

Downloads

47

Readme

cascade

Small set of utility functions that help implement conditional selection/execution of options/functions. Summary:

  • Provide a set of ordered alternatives
  • Each alternative consists of a criteria and a value
  • For each alternative, test the criteria against a given input and find the corresponding value

It's like a switch statement that the cases are provided through an array of alternatives.

Executing corresponding function

import { [test](#testcriteria-args), [cascadeExec](#cascadeexectest-alternatives-args) } from '@orioro/cascade'

const alternatives = [
  [
    item => item.type === 'book',
    item => `${item.title} - por ${item.authors.join(' e ')}`
  ],
  [
    item => item.type === 'plant',
    item => `${item.name} (${item.scientificName})`
  ],
  [
    item => item.type === 'person',
    item => `${item.firstName} ${item.middleName} ${item.lastName}, ${item.profession}`
  ],
  [
    item => `Unknown: ${(item.name || item.title)}`
  ]
]

const summarize = item => cascadeExec(testFn, alternatives, item)

console.log(summarize({
  type: 'book',
  title: 'Psicogênese da Língua Escrita',
  authors: ['Emília Ferreiro', 'Ana Teberosky']
}))
// 'Psicogênese da Língua Escrita - por Emília Ferreiro e Ana Teberosky'

console.log(summarize({
  type: 'plant',
  name: 'Samambaiaçu',
  scientificName: 'Dicksonia sellowiana'
}))
// 'Samambaiaçu (Dicksonia sellowiana)'

console.log(summarize({
  type: 'person',
  firstName: 'João',
  lastName: 'Nunes',
  middleName: 'Silveira',
  profession: 'Desenvolvedor de software'
}))
// 'João Silveira Nunes, Desenvolvedor de software'

console.log(summarize({
  type: 'something_else',
  title: 'Something else'
}))
// 'Unknown: Something else'

Purpose

This pattern is geared towards allowing developers to expose APIs that allow users to compose and customize an algorithm's behavior. Lib developers can provide some built-in alternatives that resolve common situations and allow users to provide additional alternatives to solve domain-specific requirements.

Example usage at:

  • https://github.com/orioro/node-tree-source-nodes
  • https://github.com/orioro/node-nested-map

API

Criteria

Function that returns boolean

Alternative

Tuple consisting of either:

  • [Criteria, value]
  • [value]
Alternative

Tuple consisting of either:

  • [Criteria, Function]
  • [Function]
TestFunction

Function that interprets a given criteria. Takes as arguments:

  • {Criteria} criteria
  • ...args Remaining arguments
test(criteria, args)
cascadeFind(test, alternatives, args)
cascadeExec(test, alternatives, args)
cascadeFind(test, alternatives, args)