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

tagparse

v0.1.0

Published

A way to parse tags.

Downloads

170

Readme

🏷️ tagparse

Only suppports ESM

tagparse is a high-performance, stream-based lexer and parser library for Node.js, designed to handle asynchronous, memory-efficient parsing of custom tag-based syntax. It’s ideal for high-concurrency applications and real-time data processing where non-blocking, scalable parsing is essential.

Use Case: Quickly parse and process custom tags (e.g., {variable}, {function:arg1|arg2}) for templating engines, dashboards, and real-time applications.


✨ Features

  • Custom Tag Parsing: Handles tags with complex argument structures (e.g., {var}, {func:arg1|arg2}).
  • Stream-Based, Asynchronous Design: Perfect for processing large datasets with minimal memory usage, supporting real-time and high-concurrency environments.
  • Strict Mode for Syntax Validation: Ensures robust syntax adherence with optional strict validation.
  • Flexible & Extensible: Customize tokens, tag formats, and parsing logic as needed.
  • Ideal for Real-Time Applications: Powers templating engines, dynamic dashboards, and high-performance real-time data processing.

📦 Installation

Install via npm:

npm install tagparse

🚀 Usage

tagparse is particularly useful for custom syntax processing in templating engines, real-time chat systems, or data stream processing applications.

🛠️ Basic Example

import { Lexer, Parser, NodeType } from "tagparse";

const inputText = "Hello {user}, your balance is {balance}!";

// Step 1: Initialize the Parser
const parser = new Parser(inputText, {
    lexerOptions: { tagStart: "{{", tagEnd: "}}" },
    parseTags: true,
    strict: true,
});

// Step 2: Parse the text
(async () => {
    const nodes = await parser.parse();
    console.log(nodes); // Parsed tokens and nodes
})();

🔧 Customizing Tags and Syntax

Easily define custom tag formats, enable strict mode for syntax validation, and configure tag handling:

const parser = new Parser("Hello {{user}}", {
    lexerOptions: { tagStart: "{{", tagEnd: "}}" },
    parseTags: true,
    strict: true,
});

📄 Example Use Case: Template Processing

This example demonstrates how to use the parser to replace placeholders dynamically within a template:

(async () => {
    const values = {
        name: "John",
        orderId: "12345",
    };

    const parser = new Parser(inputText);
    const nodes = await parser.parse();

    // Implement an on-the-go interpreter via `variableParser` & `functionParser`
    const result = nodes
        .map((node) => {
            if (node.type === NodeType.Variable && node.raw in values) {
                return values[node.raw];
            }
            return node.value;
        })
        .join("");

    console.log(result); // Outputs: "Hello, John! Your order #12345 is confirmed."
})();

📚 API Reference

Lexer

The Lexer class tokenizes a given input string, identifying tags, literals, and other specified tokens.

  • Constructor: new Lexer(input: string, options: LexerOptions)
    • input: The string to tokenize.
    • options: Configuration options, like tagStart and tagEnd.

Parser

The Parser class consumes tokens from the lexer and constructs a structured representation of the text (AST).

  • Constructor: new Parser(input: string, options: ParserOptions)

    • input: The input text.
    • options: Configuration options, including:
      • parseTags: Enable/disable tag parsing.
      • strict: Enable strict mode for syntax validation.
      • functionParser and variableParser: Optional handlers for custom tag logic.
  • Methods:

    • parse(): Asynchronously parses the input and returns an array of nodes.

🤝 Contributing

Contributions are welcomed! If you find a bug or want to suggest an improvement, feel free to open an issue or submit a pull request.


📄 License

This library is licensed under the MIT License. See LICENSE for more information.


🙏 Acknowledgments

Special thanks to @aelxxs/ikigai for inspiration.