Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ason

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ason

Advanced JSON - pluggable JSON logic inspired by GYP

  • 0.1.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

ason

Advanced JSON inspired by GYP

Installation

$ npm install ason

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 Advanced JSON you can define plugin functions, or use ones bundled with ason 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 ason = require('ason');

var conf = ason()
  .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.

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

ason.ms

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

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

yields:

{ interval: 900000 }

ason.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:

ason()
  .use(ason.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:

ason()
  .use(ason.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
ason()
  .use(ason.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
ason()
  .use(ason.include)
  .parse('{ "prod": "include [ config/* ]" }');

yields:

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

ason.bools

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

ason.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.

ason.replace(str, val)

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

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

yields:

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

ason.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.

ason.glob

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

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

yields:

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

Running tests

$ npm install
$ make test

License

(The MIT License)

Copyright (c) 2013 Christian Sullivan <cs@euforic.co>

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.

Keywords

FAQs

Package last updated on 01 Mar 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc