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 🙏

© 2026 – Pkg Stats / Ryan Hefner

compromise-unist

v0.2.0

Published

output a Abstract Syntax Tree from compromise

Readme

This plugin allows reading and writing Abstract Syntax Trees in Unist-style NLCST Format

These are cool because they provide a standard way for machines to share parsed documents, and to operate on them in consistent and collaborative ways.

The NLCST format is a natural-language specific AST, with support for defining tokenization, offsets, and word metadata.

import nlp from 'compromise'
import plugin from 'compromise-unist'
nlp.plugin(plugin)

const doc = nlp(`Bernardo: Who's there?`)
const tree = doc.toAst()
/*
{
  type: 'RootNode',
  children: [{
    type: 'ParagraphNode',
    children: [{
      type: 'SentenceNode',
      children: [{
          type: 'WordNode',
          children: [{
            type: 'TextNode',
            value: 'Bernardo',
            position: {
              start: { line: 1, column: 1, offset: 0 },
              end: { line: 1, column: 9, offset: 8 },
            },
          }],
          data: { compromise: { tags: ['Expression'] } },
        },
        { type: 'PunctuationNode', value: ':' },
        { type: 'WhiteSpaceNode', value: ' ' },
        {
          type: 'WordNode',
          children: [
            { type: 'TextNode', value: 'Who' },
            { type: 'PunctuationNode', value: "'" },
            { type: 'TextNode', value: 's' },
          ],
          ... (details omitted)
*/

// ↓ convert it back to compromise
const doc2 = nlp.fromAst(tree)
doc2.debug()
// ╭─────────
// │ 'Bernardo' - Expression
// │ 'Who's'    - ProperNoun, Noun
// │ 'there'    - Noun, Uncountable

Usage

.toAst()

This method converts the compromise document as-is, into a valid NLCST tree.

The tree follows document → paragraphs → sentences → words, with punctuation and whitespace preserved.

By default, we add optional position offsets to every node, and compromise tags on every WordNode.

You can turn this off with:

const tree = nlp('Hello world!').toAst({ tags: false, positions: false })
// {
//   type: 'RootNode',
//   children: [{
//     type: 'ParagraphNode',
//     children: [{
//       type: 'SentenceNode',
//       children: [
//         { type: 'WordNode', children: [{ type: 'TextNode', value: 'Hello' }] },
//         { type: 'WhiteSpaceNode', value: ' ' },
//         { type: 'WordNode', children: [{ type: 'TextNode', value: 'world' }] },
//         { type: 'PunctuationNode', value: '!' },
//       ],
//     }],
//   }],
// }

.fromAst()

Likewise, NLCST trees can be ingested into compromise, like so:

const doc = nlp(`wow windows! I don't think I can afford this place`)
const tree = doc.toAst()

const doc2 = nlp.fromAst(tree)
doc2.match('#Verb').text()
// "don't think can afford"

Import preserves the tree’s word and sentence boundaries, even if they differ from compromise’s usual tokenization. It does not run the tokenizer or tagger. Re-exporting is not yet a structurally identical round-trip.

Tags in WordNode.data.compromise.tags are copied onto the imported terms. Pass { tags: false } to skip them. If not, you may want to run .tagger() on the document, after creating it:

import { ParseEnglish } from 'parse-english'
import nlp from 'compromise'
import plugin from 'compromise-unist'
nlp.plugin(plugin)

// use wooorm's parser
const tree = new ParseEnglish().parse(`wow windows! I don't think I can afford this place`)
// ship it to compromise (no tags)
const doc = nlp.fromAst(tree)
// ╭─────────
// │ 'wow'      -
// │ 'windows'  -
// ╭─────────
// │ 'I'        -
// │ 'don't'    -
// │ 'think'    -
// │ 'I'        -
// │ 'can'      -
// │ 'afford'   -
// │ 'this'     -
// │ 'place'    -

// (tag it)
doc.tagger()
// ╭─────────
// │ 'wow'      - Expression
// │ 'windows'  - Noun, Plural
// ╭─────────
// │ 'I'        - Noun, Pronoun
// │ 'don't'    - Verb, PresentTense, Infinitive
// │ 'think'    - Verb, PresentTense, Infinitive
// │ 'I'        - Noun, Pronoun
// │ 'can'      - Verb, Modal, Auxiliary
// │ 'afford'   - Verb, PresentTense, Infinitive
// │ 'this'     - Determiner
// │ 'place'    - Noun, Singular

Known limitations

Contraction text survives export and import, but implicit expansion terms and their tags do not. For example, compromise represents can't with an implicit not term tagged Negative. That term has no written text and is omitted from the tree, so the imported document loses that tag:

const original = nlp("I can't go.")
const restored = nlp.fromAst(original.toAst())

restored.text()             // "I can't go."
original.has('#Negative')  // true
restored.has('#Negative')  // false

The importer keeps the supplied WordNode boundaries; it does not recreate implicit contraction terms. Until expansion metadata is preserved, text round-trips should not be treated as preserving all linguistic analysis.

PRs welcome

MIT