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

query-ast

v1.0.5

Published

A library to traverse/modify an AST

Downloads

961,010

Readme

QueryAST

Build Status Dependabot Status NPM version

A library to traverse/modify an AST

Documentation

Read the API documentation

Usage

let createQueryWrapper = require('query-ast')
let $ = createQueryWrapper(ast, options)

Getting Started

QueryAST aims to provide a jQuery like API for traversing an AST.

let ast = {
  type: 'program',
  value: [{
    type: 'item_container',
    value: [{
      type: 'item',
      value: 'a'
    }]
  }, {
    type: 'item_container',
    value: []
  }, {
    type: 'item',
    value: 'b'
  }]
}

// Create a QueryWrapper that will be used to traverse/modify an AST
let $ = createQueryWrapper(ast)

// By default, the QueryWrapper is scoped to the root node
$('item').length() // 2

// The QueryWrapper can also be scoped to a NodeWrapper or array of NodeWrappers
$('item_container').filter((n) => {
  return $(n).has('item')
}).length() // 1

Selectors

Most of the traversal functions take an optional QueryWrapper~Selector argument that will be use to filter the results.

A selector can be 1 of 3 types:

  • string that is compared against the return value of options.getType()
  • regexp that is compared against the return value of options.getType()
  • function that will be passed a NodeWrapper and expected to return a boolean
let ast = {
  type: 'program',
  value: [{
    type: 'item_container',
    value: [{
      type: 'item',
      value: 'a'
    }]
  }, {
    type: 'item',
    value: 'b'
  }]
}

let $ = createQueryWrapper(ast)

// String
$('item').length() // 2

// RegExp
$(/item/).length() // 3

// Function
$((n) => n.node.value === 'a').length() // 1

Default format

By default, QueryAST assumes that an AST will be formatted as a node tree where each node has a type key and a value key that either contains the string value of the node or an array of child nodes.

let ast = {
  type: 'program',
  value: [{
    type: 'item',
    value: 'a'
  }]
}

Alternate formats

Not every AST follows the same format, so QueryAST also provides a way to traverse any tree structure. Below are the default options used to handle the above AST structure.

let options = {
  /**
   * Return true if the node has children
   *
   * @param {object} node
   * @returns {boolean}
   */
  hasChildren: (node) => Array.isArray(node.value),
  /**
   * Return an array of child nodes
   *
   * @param {object} node
   * @returns {object[]}
   */
  getChildren: (node) => node.value,
  /**
   * Return a string representation of the node's type
   *
   * @param {object} node
   * @returns {string}
   */
  getType: (node) => node.type,
  /**
   * Convert the node back to JSON. This usually just means merging the
   * children back into the node
   *
   * @param {object} node
   * @param {object[]} [children]
   * @returns {string}
   */
  toJSON: (node, children) => {
    return Object.assign({}, node, {
      value: children ? children : node.value
    })
  },
  /**
   * Convert the node to a string
   *
   * @param {object} node
   * @returns {string}
   */
  toString: (node) => {
    return typeof node.value === 'string' ? node.value : ''
  }
}

Running tests

Clone the repository, then:

npm install
# requires node >= 6.0.0
npm test

Generate Documentation

npm run doc