j2j
Version:
CLI tool to convert JS to JSON
1,329 lines (447 loc) • 19.3 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Module: j2j</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Module: j2j</h1>
<section>
<header>
<h2>
j2j
</h2>
</header>
<article>
<div class="container-overview">
<dt>
<h4 class="name" id="module:j2j"><span class="type-signature"></span>(require("j2j"))<span class="signature">(input, opts<span class="signature-attributes">opt</span>, callback<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Promise}</span></h4>
<p class="summary">Parses and generates output from a string. Fulfills with output.</p>
</dt>
<dd>
<div class="description">
<p>For programmatic use. If for some reason you pass it a non-string,
it will just give you output, which is a shortcut for <code>JSON.stringify()</code>.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>input</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="attributes">
</td>
<td class="description last"><p>String to parse</p></td>
</tr>
<tr>
<td class="name"><code>opts</code></td>
<td class="type">
<span class="param-type">Object</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last"><p>See <a href="module-j2j.html#options">module:j2j.options</a></p></td>
</tr>
<tr>
<td class="name"><code>callback</code></td>
<td class="type">
<span class="param-type">function</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last"><p>Optional callback if you don't want to use Promises.</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="j2j.js.html">j2j.js</a>, <a href="j2j.js.html#line189">line 189</a>
</li></ul></dd>
<dt class="tag-see">See:</dt>
<dd class="tag-see">
<ul>
<li><a href="module-j2j.html#write">module:j2j.write</a></li>
</ul>
</dd>
</dl>
<h5>Returns:</h5>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type">Promise</span>
</dd>
</dl>
<h5>Example</h5>
<pre class="prettyprint"><code>var j2j = require('j2j');
j2j("{foo: 'bar', baz: 2}", {indent: false})
.then(function(output) {
expect(output).to.equal('{"foo":"bar","baz":2}');
});</code></pre>
</dd>
<div class="description"><p>Attempts to convert your JavaScript to JSON.</p></div>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="j2j.js.html">j2j.js</a>, <a href="j2j.js.html#line10">line 10</a>
</li></ul></dd>
</dl>
</div>
<h3 class="subsection-title">Methods</h3>
<dl>
<dt>
<h4 class="name" id="options"><span class="type-signature">(static) </span>options<span class="signature">(opts<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Object}</span></h4>
</dt>
<dd>
<div class="description">
<p>Merge options object with default options.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>opts</code></td>
<td class="type">
<span class="param-type">Object</span>
|
<span class="param-type">function</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last"><p>Options! If function, ignored.</p>
<h6>Properties</h6>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th>Default</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>debug</code></td>
<td class="type">
<span class="param-type">boolean</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
false
</td>
<td class="description last"><p>Debug mode?</p></td>
</tr>
<tr>
<td class="name"><code>indent</code></td>
<td class="type">
<span class="param-type">number</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
2
</td>
<td class="description last"><p>How many spaces to indent JSON output</p></td>
</tr>
<tr>
<td class="name"><code>no-color</code></td>
<td class="type">
<span class="param-type">boolean</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
</td>
<td class="description last"><p>No colors? Even a little? <code>false</code> in programmatic usage; <code>true</code> otherwise.</p></td>
</tr>
<tr>
<td class="name"><code>line-nos</code></td>
<td class="type">
<span class="param-type">boolean</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="default">
</td>
<td class="description last"><p>Display line numbers?</p></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="j2j.js.html">j2j.js</a>, <a href="j2j.js.html#line122">line 122</a>
</li></ul></dd>
</dl>
<h5>Returns:</h5>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type">Object</span>
</dd>
</dl>
</dd>
<dt>
<h4 class="name" id="output"><span class="type-signature">(static) </span>output<span class="signature">(o, opts<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {string}</span></h4>
<p class="summary">Given a JS variable, stringify it, with optional color</p>
</dt>
<dd>
<div class="description">
<p>This function is synchronous.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>o</code></td>
<td class="type">
<span class="param-type">Object</span>
|
<span class="param-type">Array</span>
|
<span class="param-type">string</span>
|
<span class="param-type">number</span>
</td>
<td class="attributes">
</td>
<td class="description last"><p>Thing to stringify and output.</p></td>
</tr>
<tr>
<td class="name"><code>opts</code></td>
<td class="type">
<span class="param-type">Object</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last"><p>Options. See <a href="module-j2j.html#options">module:j2j.options</a></p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="j2j.js.html">j2j.js</a>, <a href="j2j.js.html#line97">line 97</a>
</li></ul></dd>
</dl>
<h5>Returns:</h5>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type">string</span>
</dd>
</dl>
</dd>
<dt>
<h4 class="name" id="parse"><span class="type-signature">(static) </span>parse<span class="signature">(s, opts, callback<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Promise}</span></h4>
<p class="summary">Attempts to parse a string into JSONable.</p>
</dt>
<dd>
<div class="description">
<p>If you pass something like <code>"{foo: 'bar'}"</code> into <code>JSON.stringify()</code>, it will of course
return <code>"{foo: 'bar'}"</code> because you gave it a string. We need to give <code>JSON.stringify()</code> some actual
JavaScript. So we attempt to evaluate the string as JS within a sandbox. Because it's evaluated,
you can actually put expressions in it, call functions, etc., as long as everything is defined.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>s</code></td>
<td class="type">
<span class="param-type">String</span>
</td>
<td class="attributes">
</td>
<td class="description last"><p>Raw string to evaluate</p></td>
</tr>
<tr>
<td class="name"><code>opts</code></td>
<td class="type">
<span class="param-type">Object</span>
|
<span class="param-type">function</span>
</td>
<td class="attributes">
</td>
<td class="description last"><p>See <a href="module-j2j.html#options">module:j2j.options</a>. Callback if function.</p></td>
</tr>
<tr>
<td class="name"><code>callback</code></td>
<td class="type">
<span class="param-type">function</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last"><p>Callback to call with results from evaluation, if you don't like Promises.</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="j2j.js.html">j2j.js</a>, <a href="j2j.js.html#line64">line 64</a>
</li></ul></dd>
</dl>
<h5>Returns:</h5>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type">Promise</span>
</dd>
</dl>
</dd>
<dt>
<h4 class="name" id="write"><span class="type-signature">(static) </span>write<span class="signature">(input, opts<span class="signature-attributes">opt</span>, callback<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Promise}</span></h4>
<p class="summary">Parses and generates output from a string, then writes the output somewhere.</p>
</dt>
<dd>
<div class="description">
<p>Used by CLI. Exits with nonzero code if error and <code>callback</code> is NOT specified.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Attributes</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>input</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="attributes">
</td>
<td class="description last"><p>String to parse</p></td>
</tr>
<tr>
<td class="name"><code>opts</code></td>
<td class="type">
<span class="param-type">Object</span>
|
<span class="param-type">function</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last"><p>See <a href="module-j2j.html#options">module:j2j.options</a>. Callback if function.</p></td>
</tr>
<tr>
<td class="name"><code>callback</code></td>
<td class="type">
<span class="param-type">function</span>
</td>
<td class="attributes">
<optional><br>
</td>
<td class="description last"><p>Optional callback if you don't want to use Promises.</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="j2j.js.html">j2j.js</a>, <a href="j2j.js.html#line142">line 142</a>
</li></ul></dd>
<dt class="tag-see">See:</dt>
<dd class="tag-see">
<ul>
<li><a href="module-j2j.html">module:j2j</a></li>
</ul>
</dd>
</dl>
<h5>Returns:</h5>
<dl>
<dt>
Type
</dt>
<dd>
<span class="param-type">Promise</span>
</dd>
</dl>
</dd>
</dl>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Index</a></h2><h3>Modules</h3><ul><li><a href="module-j2j.html">j2j</a></li></ul>
</nav>
<br clear="both">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha9</a> on Sun Sep 07 2014 10:49:15 GMT-0700 (PDT)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>