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

coercive

v0.1.5

Published

An intuitive interface for type coercion and data cleanup

Downloads

3

Readme

Coercive

Coercive is a JavaScript toolkit for writing type coercions: functions that parse strings into other formats with configurable defaults if the string can't be parsed. Object coercion functions can be used to clean up data (from CSV, or other string-based formats) by coercing strings into numbers, dates, and more.

travis-ci

Examples

Coercive provides functions for coercing both individual values and keys of objects. Here are some examples:

// coerce a number or return zero if the result is NaN
var numOrZero = coerce.number(0);
numOrZero("42")   // 42
numOrZero("asdf") // 0

var intOrNull = coerce.int(null);
intOrNull("42")   // 42
intOrNull("asdf") // null

// coerce the "size" key of an object to a float (in place)
var sizeToFloat = coerce.object()
  .key("size", "float");
sizeToFloat({size: "46.5"});  // {"size": 46.5}

// coerce a date string into a Date object:
var parseDate = coerce.date("%Y-%m-%d");
parseDate("2001-10-31") // <Date>
parseDate("asdf")       // null

// you can also create multi-format date coercions, e.g. if your
// data contains messy data in multiple formats
var parseDate = coerce.date([
  "%Y-%m-%d",
  "%d/%m/%Y"
]);
parseDate("2001-10-31") // <Date>
parseDate("13/10/2001") // <Date>

// object coercions have a .map() method, too, just in case
// you don't want to modify the objects in place
var a = {size: "1.4"},
    b = {size: "2.6"};
[a, b].map(sizeToFloat.map);
// produces an array of new objects with coerced keys:
// [{"size": 1.4}, {"size": 2.6}]

// and you can coerce multiple keys like this:
var prepData = coerce.object()
  .keys({
    height: "number",
    weight: "int",
    birthday: coerce.date("%Y-%m-%d")
  });
prepData({
  height: "6.5",
  weight: "250",
  birthday: "1945-12-15"
});
// produces:
// {"height": 6.5, "weight": 250, "birthday": <Date>}
// or, just apply your coersions to a list of data objects,
// e.g. loaded from a CSV file:
d3.csv("path/to/data.csv", function(error, data) {
  data.forEach(prepData);
  // your data should have numbers and Date objects in it now
});

Usage (Node.js)

To use coercive in Node.js:

$ npm install coercive

then, in your script:

var coerce = require("coercive");

Usage (browser)

To use coercive in your browser, just include the script:

<script src="path/to/coercive.js"></script>
<!-- and, if you're using coerce.date(), be sure to include d3 -->
<script src="http://d3js.org/d3.v3.min.js"></script>

This gives you the coercive API in the coerce object (not coercive!).