(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> |
|
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.
Methods
-
(static) options(optsopt) → {Object}
-
Merge options object with default options.
Parameters:
Name Type Attributes Description optsObject | function <optional>
Options! If function, ignored.
Properties
Name Type Attributes Default Description debugboolean <optional>
false Debug mode?
indentnumber <optional>
2 How many spaces to indent JSON output
no-colorboolean <optional>
No colors? Even a little?
falsein programmatic usage;trueotherwise.line-nosboolean <optional>
Display line numbers?
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 oObject | Array | string | number Thing to stringify and output.
optsObject <optional>
Options. See module:j2j.options
Returns:
- Type
- string
-
(static) parse(s, opts, callbackopt) → {Promise}
Attempts to parse a string into JSONable.
-
If you pass something like
"{foo: 'bar'}"intoJSON.stringify(), it will of course return"{foo: 'bar'}"because you gave it a string. We need to giveJSON.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 sString Raw string to evaluate
optsObject | function See module:j2j.options. Callback if function.
callbackfunction <optional>
Callback to call with results from evaluation, if you don't like Promises.
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
callbackis NOT specified.Parameters:
Name Type Attributes Description inputstring String to parse
optsObject | function <optional>
See module:j2j.options. Callback if function.
callbackfunction <optional>
Optional callback if you don't want to use Promises.
- Source:
- See:
Returns:
- Type
- Promise