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

genre

v2.0.0

Published

Object template declaration and runtime type checking

Downloads

25

Readme

genre npm version

Object template declaration and runtime type checking

Installation

npm i genre

Usage

genre is really just a simple function. It takes in an object, and a template you wish to compare it to. The function will return true or false depending on if the object matches the template.

const check = require('genre')

const template = {
  name: i => i === 'Bob'
}

check({ name: 'Bob' }, template)  // true
check({ name: 'John' }, template) // false

As the above code shows, the template is an object with functions to validate fields. In this example, the name field is being validated as equaling "Bob".

This isn't a very handy template, but it showcases how genre can be used to validate fields on objects. Most of the time you will care more about the presence of certain fields, and what types or range the values are in. For this reason, genre comes with several predefined types you may utilize as well:

const check = require('genre')
const { Str, Num, Bool } = require('genre/types')

const template = {
  name: Str,
  age: Num,
  isAdmin: Bool
}

// true
check({
  name: 'Phil',
  age: 30,
  isAdmin: true
}, template)

Types

Here is a list of all predefined types genre includes:

| Name | Description | | --- | --- | | Any | any non-null value | | Str | is value a String | | Num | is value a Number | | Bool | is value a Boolean | | Int | is value an integer | | Float | is value a float | | Arr(T?) | is value an array of type T (T default to Any) | | Obj(template?) | is value an Object (if template is provided, check value against it) | | Dat | is value a Date | | Fun | is value a Function | | GT(n, T) | is value of type T and > n | | GTE(n, T) | is value of type T and >= n | | LT(n, T) | is value of type T and < n | | LTE(n, T) | is value of type T and <= n | | EQ(n, T) | is value of type T and == n | | EQQ(n, T) | is value of type T and === n | | NEQ(n, T) | is value of type T and != n | | NEQQ(n, T) | is value of type T and !== n | | Or(...T) | is value of any provided type | | Not(...T) | is value of any type other than those provided | | Enum(...values) | is value included in list of values | | Optional(T) | make field optional (validates to true if null or undefined) |

Advanced Types

Types can be combined to create advanced types for use in templates:

const Between0And100 = GTE(0, LTE(100, Int))

const Documents = Arr(Obj({
  title: Str,
  content: Str
}))

const ArrayOfIntsOrStrings = Arr(Or(Int, Str))

const OptionalArrayOfNonEmptyStrings = Optional(Arr(NEQ(false, Str)))

By default, if any field defined on the template is missing from the object, the check will fail. Utilize Optional to define fields as being not required.

Once again, the types are really just functions that take in a value and return true or false if they should pass. Feel free to create your own validation functions to use and abuse as you see fit:

const template = {
  // only even ages... for reasons
  age: i => i % 2 === 0,

  // check against list of allowed names
  name: i => ['Phil', 'Amanda', 'Chris'].includes(i),

  // flip of the coin -- not recommended
  isAdmin: () => Math.random() > 0.5
}