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

doubledash

v0.4.2

Published

A schema-based object parser

Downloads

12

Readme

Doubledash Build Status Greenkeeper badge

A schema-based object parser

  • Installation
  • Example
  • API
  • License

Installation

npm install doubledash --save

Example

/* Custom class as an example */
class CustomClass {
  constructor (hey) { this._hey = hey }
}

/* Creating the schema */
const Schema = require('doubledash')
const template = {
  option1: Boolean, // Shorthand for { type: Boolean }
  option2: { type: Number, required: true, min: 5 }, // Custom function 'min' (see below)
  option3: [{
    option3_1: { type: String, default: 'string' },
    option3_2: { type: Object, required: true },
    option3_3: CustomClass
  }] // Embedding keys into an object (see result)
}

const schema = new Schema(template) // Constructed schema

function min (rule, input, key) {
  if (input <= rule) {
    input = rule
  }
  return input
}

schema.plugin('min', min) // Add new functionality to the schema

const options = {
  option2: 1,
  option3: {
    option3_2: { },
    option3_3: new CustomClass('hi there')
  }
}

console.log(schema.use(options)) // Use the schema on the input object

/*
{ option2: 5,
  option3: {
    option3_1: 'string',
    option3_2: { },
    option3_3: CustomClass { _hey: 'hi there' }
  }
}
*/

API

  • Schema
  • schema#plugin()
  • schema#use()

Schema(template)

Schema is the exposed class. Create it by using new Schema(template).
The template argument is an object with the following notation:

const template = {
  option1: Boolean // simple option of type 'Boolean'
  option2: { type: Number, required: true } // a simple option with multiple properties
  option3: [{ // an option containing suboptions
    option3_1: { type: String, default: 'string' },
    option3_2: { type: Object, required: true }
  }]
}

Objects can have the following rules by default:

  • type: Any
    Will throw an error when input is not of this type
    Mandatory at versions below 0.4.0

  • required: Boolean
    Will throw an error when a required option is not set

  • default: Any
    Overrides input when not set


schema.plugin(key, function, overwrite)

This method plugs in your functions to the object parser, making it modular.

  • key: String
    The key you use in your schema to use the function.

  • function: Function
    Function that processes the input relative to the value of key during parsing.

  • overwrite: Boolean
    When true, this will overwrite the previous function at key with function function.
    This is useful for overwriting built-in plugins, such as type, required or default.

This function should return the processed input, or throw errors:

const template = { num: { type: Number, required: true, minimum: 5 } }
const schema = new Schema(template)

function min (rule, input, key) {
  if (input <= rule) {
    input = rule
  }
  return input
}

schema.plugin('minimum', min)

Doubledash will pass three arguments to your function whenever it reads the set key in a template.
These three arguments are:

  • rule: Any
    The given value when you create your template. In the example above, this value is 5.

  • input: Any
    The actual input of the object that use was called on.

  • key: String
    The name of the object with given input. Useful for throwing errors.

If this might still be too vague, you could check out the built-in plugins in doubledash.js, the source file of the project.


schema.use(input)

This method is used to test your input to the referencing schema.

The input argument should be an object that will be tested against the schema.
This method will return a processed object, which is in accordance to this schema.

See the example for more info.

License

MIT