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

@universal-packages/parameters

v1.1.3

Published

Compact parameters checker and shaper

Downloads

3,360

Readme

Parameters

npm version Testing codecov

Compact parameters checker and shaper. You may use this when you need to clean up parameters coming from a request to take an expected and clean shape.

Install

npm install @universal-packages/parameters

Parameters

The Parameters class is the main interface to start checking and shaping objects.

import { Parameters } from '@universal-packages/parameters'

const subject = { user: { name: 'David', undesirable: '555' } }
const parameters = new Parameters(subject)

const shaped = parameters.shape({ user: ['name'] })

console.log(shaped)

// > { user: { name: 'David' } }

Instance methods

shape(...shape: Object | string[])

The shape method arguments should be the list of attributes you want to preserve when shaping a subject, we call these each a ParamEntry and the whole list of them is the shape.

String entries

The most simple way of shaping an object is just by passing the name of the attributes you want to preserve from the subject.

import { Parameters } from '@universal-packages/parameters'

const subject = { a: 'a', b: 'b', c: 'c' }
const parameters = new Parameters(subject)

const shaped = parameters.shape('a', 'b')

// > { a: 'a', b: 'b' }

Alternatively you can describe the list ob attribute keys by passing an object with those keys

const shaped = parameters.shape({ a: {} }, { b: {} })

or even

const shaped = parameters.shape({ a: {}, b: {} })

The attributes point to objects that describe the attribute, in this case we just need the attributes to be preserved so we let them empty.

Deep shaping

To describe the shape deeper you need to describe attributes through object descriptors. In the bellow example we describe that the root subject should contain a user key, and then we describe that key as a shape, again, a shape can be an array of string listing what that object should contain or attribute descriptors.

import { Parameters } from '@universal-packages/parameters'

const subject = { user: { name: 'David', undesirable: '555' } }
const parameters = new Parameters(subject)

const shaped = parameters.shape({ user: ['name'] })

// > { user: { name: 'David' } }

Alternatively you can describe the same as:

const shaped = parameters.shape({ user: [{ name: {} }] })

or

const shaped = parameters.shape({ user: { shape: ['name'] } })

or

const shaped = parameters.shape({ user: { shape: [{ name: {} }] } })

Arrays

To describe an attribute as an array you wrap your shape in a another array.

import { Parameters } from '@universal-packages/parameters'

const subject = {
  user: {
    name: 'David',
    books: [
      { id: 1, name: 'A', selected: true },
      { id: 2, name: 'B', selected: false }
    ]
  }
}
const parameters = new Parameters(subject)

const shaped = parameters.shape({ user: ['name', { books: [['id', 'selected']] }] })

// > { user: { name: 'David', books: [{ id: 1, selected: true }, { id: 2, selected: false }] } }

Alternatively you can describe the same as:

const shaped = parameters.shape({ user: ['name', { books: { shape: [['id', 'selected']] } }] })

Enums

As a good to have for params we support enum shaping, if the subject does not provide a value within the enum constrain the shape method will throw an error.

import { Parameters } from '@universal-packages/parameters'

const subject = { settings: { color: 'red' } }
const parameters = new Parameters(subject)

const shaped = parameters.shape({ settings: [{ color: new Set(['red', 'blue', 'white']) }] })

// > { settings: { color: 'red' } }

Alternatively you can describe the same as:

const shaped = parameters.shape({ settings: [{ color: { enum: new Set(['red', 'blue', 'white']) } }] })

Enum Arrays

Complementary to enums you can specify that the value should be an array of the permitted values.

import { Parameters } from '@universal-packages/parameters'

const subject = { settings: { colors: ['white'] } }
const parameters = new Parameters(subject)

const shaped = parameters.shape({ settings: [{ colors: { enumArray: new Set(['red', 'blue', 'white']) } }] })

// > { settings: { colors: ['white'] } }

Errors

The shape function will throw and error when the shape can not be obtain through the subject.

import { Parameters } from '@universal-packages/parameters'

const subject = { user: { name: 'David', undesirable: '555' } }
const parameters = new Parameters(subject)

const shaped = parameters.shape({ user: ['firstName'] })

Error: subject/user/firstName was not provided and is not optional

if you want shape to not throw errors for some attributes when the subject does not provide them, you can describe them as optional.

import { Parameters } from '@universal-packages/parameters'

const subject = { user: { name: 'David', undesirable: '555' } }
const parameters = new Parameters(subject)

const shaped = parameters.shape({ user: [{ firstName: { optional: true } }] })

Typescript

This library is developed in TypeScript and shipped fully typed.

Contributing

The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.

License

MIT licensed.