npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-cst

v1.2.0

Published

Parse JSON into CST (Concrete Syntax Tree)

Downloads

48,530

Readme

npm version downloads build status coverage status Language grade: JavaScript

json-cst

This package parses a JSON into CST (Concrete Syntax Tree), similar to an AST but more low-level and with ties to the lexer tokens. It uses json-lexer to parse the file into tokens. The speed is practically the same as json-to-ast (it's ~10% faster than json-to-ast), but it's far smaller (even including json-lexer). Pure package is 7x smaller, install size 12x smaller, bundling it makes it 6x smaller according to bundlephobia (json-to-ast vs json-cst).

It comes with TypeScript typings.

Install

npm i json-cst or yarn add json-cst

This is a pure ESM package, and requires Node.js >=14.13.1

Simple usage

Exports

The package exports parse(json: string, options: ParseCstOptions): CstNode.

options is an optional object which can contain includeValueTokens: true to include the value tokens in the result, meaning, for objects and arrays, they will include the slice of tokens for the beginning and end of the object/array.

Definition

The tokens are parsed into a hierarchy of nodes, each with a "kind" property:

type CstKindLiteral = 'literal'; // null, true, false
type CstKindNumber = 'number';
type CstKindString = 'string';
type CstKindObjectPropertyColon = 'object-property-colon';
type CstKindObjectProperty = 'object-property';
type CstKindObject = 'object';
type CstKindArrayElement = 'array-element';
type CstKindArray = 'array';

And the CstNode returned by parse() is a CstValueNode, i.e. one of:

  • CstNodeLiteral
  • CstNodeNumber
  • CstNodeString
  • CstNodeObject
  • CstNodeArray

Other nodes are:

  • CstNodeObjectProperty
  • CstNodeObjectPropertyColon
  • CstNodeArrayElement

Each token contain a { range: CstTokenRange } where

interface CstTokenRange {
    start: number;
    end: number;
}

Each of the primitive tokens CstNodeLiteral, CstNodeNumber and CstNodeString contain { token: Token } being the raw token from json-lexer.

Object and array tokens CstNodeObject and CstNodeArray contain a property children being an array of either CstNodeObjectProperty or CstNodeArrayElement.

A CstNodeObjectProperty has a keyToken property being the lexer token for the property name, and a valueNode being a CstNode. A CstNodeArrayElement also has a valueNode.

See types.ts for exact typings.