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

coercion

v1.1.0

Published

Helper module to parse/cast/coerce strings.

Downloads

3,671

Readme

Coercion

A node.js module for type coercion.

Usage

parse = require('coercion')
parse.dateRange("2014-01-01-2014-06-01")

{
  from: Wed Jan 01 2014 01:00:00 GMT+0100 (CET),
  to: Sun Jun 01 2014 02:00:00 GMT+0200 (CEST)
}

Functions

sort

parse.sort('-created,title')

{
  created: 'desc',
  title: 'asc'
}

csv

parse.csv('allowed,created,title,notallowed', {allowed: ['allowed', 'created', 'title']})

[ 'allowed', 'created', 'title' ]

boolean

Always returns a boolean. Default value is false

// returns true for true, 'true', 1, '1', 'yes'
parse.boolean('1')

> true


parse.boolean('0')

> false

integer

Always returns a number. Default value is 0

// always returns a number
parse.integer('foo')

> 0

// Use a default value
parse.integer('foo', {default: 10})

> 10


// Use a maxium value
parse.integer(1000, {default: 10, max: 100})

> 100

positiveInteger

Always returns a number. Default value is 0

// parse.positiveInteger(number as string or int, default, max value)
parse.positiveInteger(req.query.limit, {default: 10, max: 100})

> 100

date

Always returns a date. Undefined if parsing failed.

parse.date("2014-01-01")

Wed Jan 01 2014 01:00:00 GMT+0100 (CET)

dateRange

Always returns an Object.

parse.dateRange("2014-01-01-2014-06-01")

{
  from: Wed Jan 01 2014 01:00:00 GMT+0100 (CET),
  to: Sun Jun 01 2014 02:00:00 GMT+0200 (CEST)
}

pagination

Always returns an Object. Default limit is 50

parse.pagination({page: '10'})

{
  offset: 450,
  limit: 50
}


// Override the offset
parse.pagination({offset: 450})

{
  offset: 450,
  limit: 50
}


// Set the limit
parse.pagination({page: 5, limit: 10})

{ offset: 40, limit: 10 }

middlewares

parse.middlewares.all(config)

-> returns function(req, res, next) {

  // which populates req.options
  req.options = req.options || {}

  // And pagination, sort & fields on req.options
  req.options.pagination = {
    offset: 0
    limit: 50
  }
  req.options.sort = {
    title: 'asc'
  }

  req.options.fields = ['title', 'name']
}

sort

fields

pagination