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

hova

v1.0.1

Published

higher order function validation

Downloads

4

Readme

HOVa

Higher Order Validations Uses higher order pattern to add validation to your functions.

Why?

Our functions should be small, simple and include only the logic required to complete their task. Unfortunately we can't always trust the data that we throw at our functions. Writing validation into the functions increases their size and complexity and dilutes the logic that they contain. Take the following example:

const sum = (foo, bar) => {
  if (typeof foo !== 'number') throw new Error()
  if (typeof bar !== 'number') throw new Error()
  return foo + bar
}

module.exports = sum

In this example we can already see how there is duplication and a dilution of the functions purpose. HOVa allows you to move the validation external to the functions body to make your code cleaner, more composable and more repeatable.

const hova = require('hova')

const isNumber = (param) => typeof param === 'number'

const sum = (foo, bar) => foo + bar

module.exports = hova(sum, isNumber, isNumber)

By doing this your functions are returned to only care about their required logic and your validation methods can be reused again and again. You can even import a validation library such as validator to take care of the validation functions for you.

const hova = require('hova')

const { isInt } = require('validator')

const sum = (foo, bar) => foo + bar

module.exports = hova(sum, isInt, isInt)

how

install

npm install --save hova

use

const hova = require('hova')
const isNumber = (param) => typeof param === 'number'

const sum = (foo, bar) => foo + bar

const wrappedSum = hova(sum, isNumber, isNumber)

wrappedSum(1, 2) // 3
wrappedSum('foo', 'bar') // Error

| param | Description | |-------|-----------------------------------| | 0 | function to be wrapped | | 1...n | validation function for parameter |

The first parameter is the function to be wrapped. Any subsequent parameters are taken to be validation methods for the parameters passed to the wrapped function.

If a parameter does not pass the validation then an error will be thrown. If a parameter is passed which does not have a validation method defined for it then a validation error will be thrown.