joyent-portal/packages/pseudo-json-ast
2017-06-15 17:11:40 +01:00
..
src chore: initial lerna setup 2017-05-25 10:56:50 +01:00
test chore: initial lerna setup 2017-05-25 10:56:50 +01:00
.babelrc chore: initial lerna setup 2017-05-25 10:56:50 +01:00
.eslintignore chore: initial lerna setup 2017-05-25 10:56:50 +01:00
.eslintrc chore: initial lerna setup 2017-05-25 10:56:50 +01:00
.tern-project chore: initial lerna setup 2017-05-25 10:56:50 +01:00
CHANGELOG.md chore: publish 2017-05-25 16:07:31 +01:00
package.json feat(joyent-cp-frontend): create dg initial implementation 2017-06-15 17:11:40 +01:00
README.md docs: add container badges 2017-05-31 22:35:35 +01:00
yarn.lock chore: different test targets for CI 2017-05-31 20:04:12 +01:00

pseudo-json-ast

License: MPL 2.0 npm standard-readme compliant

Parse a JSON string into an object with location properties.

Table of Contents

Install

yarn add --dev pseudo-json-ast

Usage

import jsonAST, { loc } from 'pseudo-json-ast';
import assert from 'assert';

const ast = jsonAST(`{
  "obj": {
    "arr": [{
      "nums": [1, 2, 3],
      "strs1": ["1", "2", "3"],
      "strs: ["1", "2", "3"]
    }],
    "str": "1",
    "num": 1
  }
}`);

assert.deepEqual(Object.keys(ast), ['obj']);
assert.deepEqual(ast[loc].start.line, 1);
assert.deepEqual(ast[loc].end.line, 11);

assert.deepEqual(Object.keys(ast.obj), ['arr', 'str', 'num']);
assert.deepEqual(ast.obj[loc].start.line, 2);
assert.deepEqual(ast.obj[loc].end.line, 10);

assert.deepEqual(ast.obj.str[loc].start.line, 8);
assert.deepEqual(ast.obj.str[loc].end.line, 8);
assert.deepEqual(ast.obj.num[loc].start.line, 9);
assert.deepEqual(ast.obj.num[loc].end.line, 9);
assert.deepEqual(ast.obj.arr[0][loc].start.line, 3);
assert.deepEqual(ast.obj.arr[0][loc].end.line, 7);

assert.deepEqual(Object.keys(ast.obj.arr[0]), ['nums', 'strs1']);
assert.deepEqual(ast.obj.arr[0].nums[loc].start.line, 4);
assert.deepEqual(ast.obj.arr[0].nums[loc].end.line, 4);
assert.deepEqual(ast.obj.arr[0].strs1[loc].start.line, 5);
assert.deepEqual(ast.obj.arr[0].strs1[loc].end.line, 5);

License

MPL-2.0