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

eson

v0.5.0

Published

Extended JSON - pluggable JSON logic for dates, includes, and more

Downloads

134

Readme

eson

Extended JSON for node.

Installation

$ npm install eson

Parser

Currently only the parser portion is implemented, useful for configuration files. For example a typical configuration file might look something like the following:

{
  "views": "/www/example.com/views",
  "view engine": "jade",
  "poll interval": 5000,
  "canvas size": { "width": 800, "height": 600 }
}

With Extended JSON you can define plugin functions, or use ones bundled with eson to transform the input, allowing for more declarative configurations as shown here:

{
  "views": "{root}/views",
  "view engine": "jade",
  "poll interval": "5 seconds",
  "canvas size": "800x600"
}

Writing plugins

Writing a plugin is simple, it's a function which takes the signature (key, val, parser). Let's write one that transforms every value to "foo":

function foo(key, val, parser) {
  return 'foo';
}

Then use the plugin like so:

var eson = require('eson');

var conf = eson()
  .use(foo)
  .read('path/to/config.json');

Now suppose path/to/config.json contained { "foo": "bar", "bar": "baz" }, the foo() plugin would yield { "foo": "foo", "bar": "foo" }. So you get the picture, with this we can accept arbitrary strings such as "5 seconds" and transform it to the more useful 5000 milliseconds representation.

Many plugins may of course be used, and all will be executed regardless, so if necessary subsequent plugins may still make modifications. Depending on what the plugins the order used may have an effect on the JSON.

eson()
  .use(eson.ms)
  .use(eson.include)
  .use(eson.dimensions)
  .use(eson.replace('{root}', '/www/example.com'))
  .parse('{ "interval": "15 minutes" }');

eson.ms

The milliseconds plugin supports strings like "5s", "5 seconds", "3 days", etc:

eson()
  .use(eson.ms)
  .parse('{ "interval": "15 minutes" }');

yields:

{ interval: 900000 }

eson.include

The include plugin allows you to literally include other JSON files. This works in both arrays and object literals, and loads relative to the callee's file. For example:

eson()
  .use(eson.include)
  .parse('{ "prod": "include config/production" }');

yields:

{ prod: { whatever: 'is', within: 'config/production.json' }}

You can also include multiple files via a glob. This has a special syntax and works in one of three ways.

Consider a config folder containing the following two files:

database.json:

{"db", "redis"}

app.json:

{"listen", 3000}
Merging multiple files into one:

eson()
  .use(eson.include)
  .parse('{ "prod": "include config/*" }');

yields:

{
	prod: {
		db: "redis",
		listen: 3000
	}
}
Collect files into a map, keyed by filename:

// use curly brackets to collect as a map
eson()
  .use(eson.include)
  .parse('{ "prod": "include { config/* }" }');

yields:

{
	prod: {
		database: {
			db: "redis"
		},
		app: {
			listen: 3000
		}
	}
}
Collect files as an array:

// use square brackets to collect as an array
eson()
  .use(eson.include)
  .parse('{ "prod": "include [ config/* ]" }'); 

yields:

{
	prod: [
		{db: "redis"},
		{listen: 3000}
	]
}

eson.bools

Convert "yes", "no", "on", "off", "enabled", "disabled" into booleans.

eson.env([prefix])

Allow environment variables to define config values. If you have the following:

{
  "upload path": "/data/uploads"
}

You could then export UPLOAD_PATH=/tmp to change this value. Optionally when a prefix is given such as "MYAPP_" then you must prefix such as MYAPP_UPLOAD_PATH=/tmp.

eson.replace(str, val)

The replace plugin allows you to replace arbitrary substrings, useful for constants such as the application's root directory etc.

eson()
  .use(eson.replace('{root}', '/www/example.com'))
  .parse('{ "upload path": "{root}/tmp" }');

yields:

{ "upload path": "/www/example.com/tmp" }

eson.args([args])

Parse from the given args or ARGV. For example if you have a setting named "dev ui" with a default value of false, --dev-ui would enable it, or --dev-ui yes would provide the value "yes" which is of course also truthy.

To compliment --NAME you may also negate this, if "dev ui" is enabled by default then you may use --no-dev-ui to disable it.

eson.glob

The glob plugin allows you to specify glob strings, prefixed by "glob":

eson()
  .use(eson.glob)
  .parse('{ "js": "glob public/{js,vendor}/*.js" }');

yields:

{ js: ["public/js/app.js", "public/js/user.js", "public/vendor/jquery.js"] }

moar!

That's it for now, just experimenting with it, feel free to send me a PR or open and issue if you have some ideas. I'd like to keep everything valid JSON, for example you can use the include plugin to include env-specific config into package.json, and package.json remains a valid JSON document.

For addition documentation view the test markdown.

Running tests

$ npm install
$ make test

License

(The MIT License)

Copyright (c) 2012 TJ Holowaychuk <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.