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

promise-composer

v0.3.11

Published

A tool allowing simple conditional composition with promises in JS

Downloads

43

Readme

Tests coverage

How to install ?

  1. Run npm install promise-composer
  2. Include the Promise Composer Object (PCO) with something like :
var PCO = require('promise-composer');

What does it do ?

Promise Composer is a small JS library that gives some utility tools and functions to help asynchronous processes to dynamically check/validate data.

I wrote it since I was tired of checking type errors manually using the same old if/else or switch/case methods, so instead of writing :

if (myCheckingFunc(value) && myOtherFunc(value)) {
  doStuff()
} else {
  doOtherStuff()
}

You could just write :

Promise.resolve(value)
  .then(PCO.myCheckingFunc)
  .then(PCO.myOtherFunc)
  .then(doStuff)
  .catch(doOtherStuff)

It still is pretty minimal, but I do think this can be of help to ensure a layer of dynamical verification without having to cope with if/else forests or huge monolithic switch/case blocks.

Create an assertion

To create a custom assertion function, you only have to use the PCO.assert function.

function validateObject(x) {
  return Array.isArray(x)
    && x.length > 3
    && x.length < 9
}

Promise.resolve(15)
  .then(x => PCO.assert(x, validateObject))
  .then(doOtherStuff)
  .catch(computeError)

Common assertion functions

PCO.isset Check if an element isn't undefined

PCO.exists Check if an element isn't undefined and is non-null

PCO.fullString Check if an element is a non-empty Sstring (trims whitespaces)

PCO.isObject Check if an element is a non-array Object

PCO.isArray Check if an element is a valid Array

PCO.isNumber Check if an element is a valid Number

PCO.isInteger Check if an element is a valid Integer

Multiple assertions

If you need multiple validations at once, you can use two important functions :

PCO.any This functions acts as an "or" operator. It will proceed if any of the given assertions is true.

Example

Promise.resolve("Coucou")
  .then(x => PCO.any(x, [PCO.fullString, PCO.isset]))
  .then(doStuff)
  .catch(computeError)

// This example should not trigger the .catch() block

PCO.all This function acts as an "and" operator. It will proceed if all of the given assertions are true.

Example

Promise.resolve(15)
  .then(x => PCO.all(x, [PCO.fullString, PCO.isset]))
  .then(doStuff)
  .catch(computeError)

// This example should trigger the .catch() block !

Important !

PCO.any and PCO.all both require as a second argument an array of assertions. You can use standard PCO assertion functions as well as custom assertion functions (this might be useful for dataset validation for instance).

Verify assertion on multiple values

You can use PCO.traverse using an array of values instead of a single value to check every value inside the array with your assertion.

Example

Promise.resolve(["foo", "bar", null])
  .then(x => PCO.traverse(x, PCO.exists))
  .then(doStuff)
  .catch(computeError)

// This exemple should trigger the .catch() block !