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

tspc

v1.1.2

Published

An extremely simple parser combinator for TypeScript

Downloads

3,458

Readme

tspc

A Strongly-typed parser combinator written in TypeScript, heavily inspired by fnparse.js.

Installation

npm install tspc

Example

const part = map(many(num), (arr) => parseInt(arr.join("")))
const ipString = seq(part, token("."), part, token("."), part, token("."), part)
const ipParser = map(ipString, ([a, _, b, __, c, ___, d]) => [a, b, c, d])
ipParser("192.168.0.1", 0).value // [192, 168, 0, 1]

What is tspc

It is a simple and very flexible parser combinator, capable of converting parsed results not only to strings, but also to complex arrays and objects.

// Parse hex color or color name
// returns color value in number
const parser = or(
  map(seq(token("#"), map(regexp(/[0-9a-f]/), str => parseInt(str, 16)), arr => arr[1]),
  map(token("red"), () => 0xff0000)
)
parser("#c0ffee", 0).value // 0xc0ffee
parser("red", 0).value // 0xff0000
parser("hello", 0).success // false
console.log(parser("hello", 0).error) // prints parsing error

Strongly Typed

Careful design ensures that type information is kept complete, so even complex parsers can automatically determine the type of the result.

const attr = (name: string) =>
  map(
    seq(token(name), token('="'), regexp(/[a-zA-Z0-9]/), token('"')), // Parser<string, [string, '="', string, '"']>
    (result) => ({ [name]: result[2] })
  ) // Parser<string, {[key: string]: string}>
const aTagParser = map(
  seq(token("<a"), many(or(attr("href"), attr("target"))), token(">")), // Parser<string, ["<a", {[key: string]: string}[], ">"]>
  (result) => ({
    tagName: name,
    attributes: result[1],
  })
) // Parser<string, { tagName: string, attributes: {[key: string]: string}[] }>

Generic

Parser input is not limited to strings. Any type that can represent the parse position numerically, such as an array of numbers, can be used.

const

Core API

Parser

The type for parser functions. Parser takes a input and the reading position.

type Parser<T, S> = (target: T, position: number) => Result<S>

type Result<T> =
  | {
      success: true
      position: number
      value: T
    }
  | {
      success: false
      position: number
      error: string
    }

or

Parser that succeeds if any of the given parsers succeeds.

const bit = or(token("0"), token("1"))

many

Parser that iterates until the given parser fails and returns as an array.

const binary = many(bit) // Parser<string, string[]>

seq

Returns Parser from the sequence of Parsers.

const parser = seq(token("0b"), binary)

map

Convert the result of the given parser.

const parser = map(binary, (str) => parseInt(str, 2)) // Parser<string, number>

opt

Parser that returns a given parser as a success even if it fails. The value will be nullable.

const parser = seq(token("http"), opt("s"), token("://")) // Parser<string, [string, string | null, string]>

vec

Repeats the given parser a specified number of times. Unlike many, if parsing fails before the specified number of times, it fails.

const byte = vec(bit, 8)

seqMap

Parser that generates and applies a parser from the results of a given parser.

// Parser that reads the remaining byte sequence for the number read in the first byte
const parser = seqMap(byte, (size) => vec(byte, size))

transform

Parser that passes the result of the first parser as input to the second parser.

const parser = transform(byteToStringParser, jsonParser)

lazy

Parser that returns the parser generated by the given closure. Typically used to create a recursive parser.

fail

Parser that always fails.

pass

Parser that always succeeds.

terminate

Parser that always succeeds. Unlike pass, it does not advance the position.

String API

token

Parser that succeeds if it matches the given string.

const parser = token("hello")
parser("hello, world!", 0).value // "hello"

regexp

Parser that succeeds if it matches the given regular expression.

const parser = regexp(/[a-zA-Z]+/)
parser("hello, world!", 0).value // "hello"

num

Parser matching a single numeric character.

num("31415") // "3"

uint

Parser to match positive decimal integers. Results are number.

uint("926535").value // 926535

int

Parser to match decimal integers. Results are number.

int("-12345").value // -12345