@brigand/craft-js

0.1.0 • Public • Published

craft-js aids in creating JavaScript source code programmatically.

Install

yarn add @brigand/craft-js

# or
npm install --save @brigand/craft-js

fromValue

Currently the only API available, fromValue allows you to convert a primitive JS value to either a babel AST node, or JS source code.

const { fromValue } = require('@brigand/craft-js');

const jsCode = fromValue({ x: 1, y: [2, 3] }).toJs();
console.log(jsCode);

This program produces the following output:

{
  x: 1,
  y: [2, 3]
}

To get at the inner value (a babel AST node in this case) you can call .unwrap().

const node = fromValue({ x: 1, y: [2, 3] }).unwrap();
node; /* =>
{ type: 'ObjectExpression',
  properties:
   [ { type: 'ObjectProperty',
       key: { type: 'Identifier', name: 'x' },
       value: { type: 'NumericLiteral', value: 1 },
       computed: false,
       shorthand: false,
       decorators: null },
     { type: 'ObjectProperty',
       key: { type: 'Identifier', name: 'y' },
       value:
        { type: 'ArrayExpression',
          elements:
           [ { type: 'NumericLiteral', value: 2 },
             { type: 'NumericLiteral', value: 3 } ] },
       computed: false,
       shorthand: false,
       decorators: null } ] }
*/

Readme

Keywords

none

Package Sidebar

Install

npm i @brigand/craft-js

Weekly Downloads

2

Version

0.1.0

License

MIT

Unpacked Size

158 kB

Total Files

16

Last publish

Collaborators

  • brigand2
  • greenjello