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

svast-utils

v0.0.6

Published

Utilisties for working with svast nodes.

Downloads

11

Readme

svast-utils

svast trees are non standard and have arrays of child nodes in strange places. These utilities are helpful when working with svast trees.

I have not thought any of this through and threw them together very quickly. I may add some actually useful utilities in the future but this is enough to be going along with.

None of these utilities are immutable operations because that shit is expensive.


Install it

npm i svast-utils

Use it

There are currently two function in this module that are exposed as named exports

walk

Walks a svast tree, visiting every node. You give it a svast tree (or a svast compatible tree) and a callback function to execute for each node.

type walk = (tree: Node, cb: walkCallback) => Node;

type walkCallback = (node: Node, parent: Node | undefined) => void | boolean;

Pass in a svast tree to walk and a callback function to execute for every node that is visited. Walk will return the tree when it has finished walking, this is the exact tree you passed in, it is not a copy.

The callback function will be passed the current node as the first argument and the parent node as the second argument. When you are in the root node, the parent will be undefined otherwise it will always exist.

You can return false from the callback function. This will prevent walk from walking any childnodes. This will not halt the walk entirely: it will not prevent sibling nodes from being walked.

There is no copying and no returning of new nodes. It just walks the tree with some optional bailouts. Feel free to mutate the tree as you go but if you do stuff like changing the length a children array while it is being walked something bad might happen.

walk calls itself recursively, if your tree is very large then your computer will explode.

example

import { walk } from 'svast-utils';

const tree = {
  type: 'root',
  children: [
    { type: 'hello' },
    { type: 'hello' },
    { type: 'somethingelse' , children: [ ... ]},
  ]
}

const node_names = [];

walk(tree, (node, parent) => {
  node_names.push(node.type);
  // this will prevent the children of this node from being walked
  if (node.type === 'somethingelse') return false;
})

// node_names === ['root', 'hello', 'hello', 'somethingelse']

cleanPositions

This removes all positional data from every node in a tree. It just walks a tree and deletes them all. This may be useful if you no longer need the positional data and want a smaller tree.

You simply pass the function a tree to clean and it will return the cleaned tree. This is the exact tree you passed in, mutated. It is not a copy.

example

import { cleanPositions } from 'svast-utils';

const tree = {
  type: 'root',
  children: [
    {
      type: 'hello',
      position: { start: { ... }, end: { ... } }
    },
    {
      type: 'hello',
      position: { start: { ... }, end: { ... } }
    },
    {
      type: 'somethingelse' ,
      children: [ ... ],
      position: { start: { ... }, end: { ... }}
    },
  ],
  position: { start: { ... }, end: { ... }}
}

const clean_tree = cleanPositions(tree);

// clean_tree === tree === {
//   type: 'root',
//   children: [
//     { type: 'hello' },
//     { type: 'hello' },
//     {
//       type: 'somethingelse' ,
//       children: [ ... ],
//     },
//   ],
// }