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

hyperparams

v1.0.3

Published

A fast, sandboxed matching engine with serializable rules

Downloads

336

Readme

Hyperparams

A strong params inspired library for Express and plain Javascript that's fast and functional.

Quickstart

$ yarn add hyperparams

Observe the following:

const { extract } = require('hyperparams')
const userParams = extract('user', ['email', 'password', { address: ['zip']}])
app.post('/users', (req, res)=>{
  try{
    await User.save(userParams(req.body))
    res.json({all: 'good'})
  } catch(err) {
    res.json({all: 'bad'})
  }
})

We made a userParams a strong-params extractor. This means we didn't do any left-hand-right-hand coding, any brittle deep and-and drilldowns to extract a property.

More importantly, we didn't accept everything that's on body as is, such as an _id property (internal ID on MongoDB, for example), or a malliciously added admin flag, potentially elevating a user's permissions.

Note that when a missing root or property is encountered, we throw an exception and this is by-design. If you'd like an error object, make wrapper and return an {error: ..., value: ...} struct. That's just one suggestion though.

What's next

You can give defaults to hyperparams if you like (these are deep defaults):

// let's say body is: { user: { email: '[email protected]' } }
extract('user', ['email', { foobar: ['foobaz']}], { foobar: { foobaz: 42 } })(body)
// -> { user: { email: '[email protected]', foobar: { foobaz: 42 } } }

And you can couple it with any validation library like ajv or joi.

Strong params vs validation

This is not a validation library. Hence, it will not check types, formats or any of that. If you need to do that, then you can use vanilla object schema validation libraries like ajv or joi; they have features to remove excess properties for varying degrees of support.

More importantly, you can decide that validation as a concerns sits in your model library in any case, and/or even (probably) database constraints.

But note that if you don't need involved type validation at the request layer, and you still a validation library, you'll pay with performance and developer experience.

Here are some numbers to get an idea about speed:

- baseline: joi single prop x 26,949 ops/sec ±1.12% (83 runs sampled)
- baseline: joi flat pick x 13,439 ops/sec ±1.56% (82 runs sampled)
- baseline: ajv single prop x 54,967 ops/sec ±1.98% (78 runs sampled)
- baseline: ajv flat pick x 59,308 ops/sec ±2.08% (84 runs sampled)
- baseline: lodash flat pick x 511,284 ops/sec ±1.43% (83 runs sampled)
- baseline: lodash get x 15,648,434 ops/sec ±1.51% (80 runs sampled)
- hyperparams: nested w/array mapping x 103,790 ops/sec ±1.70% (84 runs sampled)
- hyperparams: single prop x 490,952 ops/sec ±1.61% (83 runs sampled)

Contributing

Fork, implement, add tests, pull request, get my everlasting thanks and a respectable place here :).

Thanks:

To all Contributors - you make this happen, thanks!

Copyright

Copyright (c) 2017 Dotan Nahum @jondot. See LICENSE for further details.