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

tentoast

v0.0.2

Published

Experimental package, inspired by unified, for creating custom (abstract) syntax trees using javascript template expressions.

Downloads

4

Readme

Template Expression to AST (tentoast)

Experimental package, inspired by unified, for creating custom (abstract) syntax trees using javascript template strings.

About the Trees

Just like a unist, nodes are objects containing a string type field and optionally a children array (all "parent" nodes should have it set, even if it's empty). Nodes should use the value field if they have one and should not use the data field.

Unlike unists though, the tree roots are simply arrays, making it easier to merge them together at the same level without losing any potential associated fields.

Usage

Import the package and create an instance, optionally providing configuration:

import tentoast from 'tentoast'

const ttt = tentoast({
  // Defaults:
  converter: (val) => val, // Should return an array of nodes/ values, can return a single node/ non-array value.
  noSmartText: false,
  providers: {
    s: sectionProvider
  }
})

The returned function can then be used as a tag for template strings:

const tree = ttt`This is some ${'text'}. And here are some ${{type: 'strong', children: [{type: 'text', value: 'nodes'}]}}${{type: 'text', value: '!'}}`

The function runs all the template values through converter, merges the results together, and then converts any non-nodes to text nodes with a value of String(value). Finally, any neighboring text nodes are merged together and empty ones are removed (assuming noSmartText isn't truthy). The example above would return (with defaults):

[
  {
    "type": "text",
    "value": "This is some text. And here are some "
  },
  {
    "type": "strong",
    "children": [
      {
        "type": "text",
        "value": "nodes"
      }
    ]
  },
  {
    "type": "text",
    "value": "!"
  }
]

Node Interaction Providers

Tentoast also provides a nice way to expose other functions that interact with nodes but rely on tentoast functionality via the providers option. These should be functions that consume a tentoast instance and options object and return a value (generally a function) to be exposed on the instance under the same key as their provider from options. If you want to minimize typing while using these, you can always add a bit of boilerplate:

const {s, ps, ...} = ttt

These are the defaults (providers are also exported from the package), you can add/ override them with your own:

Sections (s: sectionProvider)

This produces section nodes composed of one section-header and one section-body children. The initial call passes the parameters to the tentoast instace to get the header children and returns a function that consumes the body arguments to produce the full section node in an array. For example:

const tree = 
ttt.s`This is a Section Header``\
This is the body!
It would usually have many lines, or possibly other ${{type: 'text', value: 'things.'}}`

produces:

[
  {
    "type": "section",
    "children": [
      {
        "type": "section-header",
        "children": [
          {
            "type": "text",
            "value": "This is a Section Header"
          }
        ]
      },
      {
        "type": "section-body",
        "children": [
          {
            "type": "text",
            "value": "This is the body!\nIt would usually have many lines, or possibly other things."
          }
        ]
      }
    ]
  }
]

For reference on creating your own interaction providers, here's the whole source for sectionProvider:

function sectionProvider(ttt, options) {
  return function(headerStrings, ...headerValues) {
    return function(bodyStrings, ...bodyValues) {
      return {
        "type": "section" ,
        "children": [
          {
            "type": "section-header",
            "children": ttt(headerStrings, ...headerValues)
          },
          {
            "type": "section-body",
            "children": ttt(bodyStrings, ...bodyValues)
          }
        ]
      }
    }
  }
}

Helper Functions

The tentoast package also exports a few helper functions that are used internally:

isNode(value)

Determines if a value is a node.

massageToArray(value)

Returns value if it's an array, otherwise returns [ value ].