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

yieldparser

v0.4.1

Published

<div align="center"> <h1>👑 🌿 yieldparser</h1> <p>Parse using composable generator functions. It’s like components for parsing.</p> <a href="https://bundlephobia.com/result?p=yieldparser"> <img src="https://badgen.net/bundlephobia/minzip/yieldp

Downloads

11

Readme

Installation

npm add yieldparser

Overview

Yieldparser parses a source chunk-by-chunk. You define a generator function that yields each chunk to be found. This chunk can be a string, a RexExp, or another generator function. Your generator function receives replies from parsing that chunk, for example a regular expression would receive a reply with the matches that were found. You then use this information to build a result: the value that your generator function returns. This could be a simple value, or it could be an entire AST (abstract syntax tree).

If you yield an array of choices, then each choice is tested and the first one that matches is used.

If your chunks don’t match the input string, then an error result is returned with the remaining string and the chunk that it failed on. If it succeeds, then a success result is returned with the return value of the generator function, and the remaining string (if there is anything remaining).

Run parse(input, yourGeneratorIterable) to take an input string and parse into a result.

Run invert(output, yourGeneratorIterable) to take an expected result and map it back to a source string.

Examples

Routes parser

Define a generator function for each route you have, and then define a top level Routes generator function. Then parse your path using parse().

You can also map from a route object back to a path string using invert().

import { parse, mustEnd, invert } from "yieldparser";

type Route =
  | { type: "home" }
  | { type: "about" }
  | { type: "terms" }
  | { type: "blog" }
  | { type: "blogArticle"; slug: string };

function* Home() {
  yield "/";
  yield mustEnd;
  return { type: "home" } as Route;
}

function* About() {
  yield "/about";
  yield mustEnd;
  return { type: "about" } as Route;
}

function* Terms() {
  yield "/legal";
  yield "/terms";
  yield mustEnd;
  return { type: "terms" } as Route;
}

function* blogPrefix() {
  yield "/blog";
}

function* BlogHome() {
  yield blogPrefix;
  yield mustEnd;
  return { type: "blog" };
}

function* BlogArticle() {
  yield blogPrefix;
  yield "/";
  const [slug]: [string] = yield /^.+/;
  return { type: "blogArticle", slug };
}

function* BlogRoutes() {
  return yield [BlogHome, BlogArticle];
}

function* Routes() {
  return yield [Home, About, Terms, BlogRoutes];
}

parse("/", Routes()) // result: { type: "home" }, success: true, remaining: "" }
parse("/about", Routes()) // result: { type: "about" }, success: true, remaining: "" }
parse("/legal/terms", Routes()) // result: { type: "terms" }, success: true, remaining: "" }
parse("/blog", Routes()) // result: { type: "blog" }, success: true, remaining: "" }
parse("/blog/happy-new-year", Routes()) // result: { type: "blogArticle", slug: "happy-new-year" }, success: true, remaining: "" }

invert({ type: "home" }, Routes()) // "/"
invert({ type: "about" }, Routes()) // "/about"
invert({ type: "terms" }, Routes()) // "/legal/terms"
invert({ type: "blog" }, Routes()) // "/blog"
invert({ type: "blogArticle", slug: "happy-new-year" }, Routes()) // "/blog/happy-new-year"

IP Address parser

import { parse, mustEnd } from 'yieldparser';

function* Digit() {
  const [digit]: [string] = yield /^\d+/;
  const value = parseInt(digit, 10);
  if (value < 0 || value > 255) {
    return new Error(`Digit must be between 0 and 255, was ${value}`);
  }
  return value;
}

function* IPAddress() {
  const first = yield Digit;
  yield '.';
  const second = yield Digit;
  yield '.';
  const third = yield Digit;
  yield '.';
  const fourth = yield Digit;
  yield mustEnd;
  return [first, second, third, fourth];
}

parse('1.2.3.4', IPAddress());
/*
{
  success: true,
  result: [1, 2, 3, 4],
  remaining: '',
}
*/

parse('1.2.3.256', IPAddress());
/*
{
  success: false,
  failedOn: {
    nested: [
      {
        yielded: new Error('Digit must be between 0 and 255, was 256'),
      },
    ],
  },
  remaining: '256',
}
*/

Basic CSS parser

import { parse, hasMore, has } from 'yieldparser';

type Selector = string;
interface Declaraction {
  property: string;
  value: string;
}
interface Rule {
  selectors: Array<Selector>;
  declarations: Array<Declaraction>;
}

const whitespaceMay = /^\s*/;

function* PropertyParser() {
  const [name]: [string] = yield /[-a-z]+/;
  return name;
}

function* ValueParser() {
  const [rawValue]: [string] = yield /(-?\d+(rem|em|%|px|)|[-a-z]+)/;
  return rawValue;
}

function* DeclarationParser() {
  const name = yield PropertyParser;
  yield whitespaceMay;
  yield ':';
  yield whitespaceMay;
  const rawValue = yield ValueParser;
  yield whitespaceMay;
  yield ';';
  return { name, rawValue };
}

function* RuleParser() {
  const declarations: Array<Declaraction> = [];

  const [selector]: [string] = yield /(:root|[*]|[a-z][\w]*)/;

  yield whitespaceMay;
  yield '{';
  yield whitespaceMay;
  while ((yield has('}')) === false) {
    yield whitespaceMay;
    declarations.push(yield DeclarationParser);
    yield whitespaceMay;
  }

  return { selector, declarations };
}

function* RulesParser() {
  const rules = [];

  yield whitespaceMay;
  while (yield hasMore) {
    rules.push(yield RuleParser);
    yield whitespaceMay;
  }
  return rules;
}

const code = `
:root {
  --first-var: 42rem;
  --second-var: 15%;
}

* {
  font: inherit;
  box-sizing: border-box;
}

h1 {
  margin-bottom: 1em;
}
`;

parse(code, RulesParser());

/*
{
  success: true,
  result: [
    {
      selector: ':root',
      declarations: [
        {
          name: '--first-var',
          rawValue: '42rem',
        },
        {
          name: '--second-var',
          rawValue: '15%',
        },
      ],
    },
    {
      selector: '*',
      declarations: [
        {
          name: 'font',
          rawValue: 'inherit',
        },
        {
          name: 'box-sizing',
          rawValue: 'border-box',
        },
      ],
    },
    {
      selector: 'h1',
      declarations: [
        {
          name: 'margin-bottom',
          rawValue: '1em',
        },
      ],
    },
  ],
  remaining: '',
}
*/