Module: j2j

j2j

(require("j2j"))(input, optsopt, callbackopt) → {Promise}

Parses and generates output from a string. Fulfills with output.

For programmatic use. If for some reason you pass it a non-string, it will just give you output, which is a shortcut for JSON.stringify().

Parameters:
Name Type Attributes Description
input string

String to parse

opts Object <optional>

See module:j2j.options

callback function <optional>

Optional callback if you don't want to use Promises.

Source:
See:
Returns:
Type
Promise
Example
var j2j = require('j2j');

j2j("{foo: 'bar', baz: 2}", {indent: false})
  .then(function(output) {
    expect(output).to.equal('{"foo":"bar","baz":2}');
  });

Attempts to convert your JavaScript to JSON.

Source:

Methods

(static) options(optsopt) → {Object}

Merge options object with default options.

Parameters:
Name Type Attributes Description
opts Object | function <optional>

Options! If function, ignored.

Properties
Name Type Attributes Default Description
debug boolean <optional>
false

Debug mode?

indent number <optional>
2

How many spaces to indent JSON output

no-color boolean <optional>

No colors? Even a little? false in programmatic usage; true otherwise.

line-nos boolean <optional>

Display line numbers?

Source:
Returns:
Type
Object

(static) output(o, optsopt) → {string}

Given a JS variable, stringify it, with optional color

This function is synchronous.

Parameters:
Name Type Attributes Description
o Object | Array | string | number

Thing to stringify and output.

opts Object <optional>

Options. See module:j2j.options

Source:
Returns:
Type
string

(static) parse(s, opts, callbackopt) → {Promise}

Attempts to parse a string into JSONable.

If you pass something like "{foo: 'bar'}" into JSON.stringify(), it will of course return "{foo: 'bar'}" because you gave it a string. We need to give JSON.stringify() 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.

Parameters:
Name Type Attributes Description
s String

Raw string to evaluate

opts Object | function

See module:j2j.options. Callback if function.

callback function <optional>

Callback to call with results from evaluation, if you don't like Promises.

Source:
Returns:
Type
Promise

(static) write(input, optsopt, callbackopt) → {Promise}

Parses and generates output from a string, then writes the output somewhere.

Used by CLI. Exits with nonzero code if error and callback is NOT specified.

Parameters:
Name Type Attributes Description
input string

String to parse

opts Object | function <optional>

See module:j2j.options. Callback if function.

callback function <optional>

Optional callback if you don't want to use Promises.

Source:
See:
Returns:
Type
Promise