Tagged Comment Parser

1.3.8 · active · verified Tue Apr 21

This package, `tagged-comment-parser`, provides a straightforward utility for extracting structured data from specially formatted string comments. Its current stable version is 1.3.8. The library focuses on simplicity, offering two main functions: `parse` for strict parsing which throws errors on invalid input, and `tryParse` for a more resilient approach that returns an empty object on invalid input rather than an exception. It handles various tag syntaxes, including boolean flags (`@tag`), key-value pairs (`@tag:value`), and array-like values (`@tag(val1, "val2")`). The library ships with TypeScript types, enhancing developer experience. It appears to have a stable, though not rapid, release cadence, characteristic of a focused utility. Its key differentiator is its minimal API and focused scope on comment parsing, contrasting with larger AST parsing libraries.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `parse` and `tryParse` with various tag syntaxes and input types, logging the structured output.

import { parse, tryParse } from "tagged-comment-parser";

// Example 1: Basic tags
const comment1 = "@cached @alias:foo this comment is tagged!";
const result1 = parse(comment1);
console.log("Result 1:", result1);
/* Expected: { comment: 'this comment is tagged!', tags: { cached: true, alias: 'foo' } } */

// Example 2: Tags with array values
const comment2 = "@auth(admin, user) @roles:editor A comment with multiple roles.";
const result2 = parse(comment2);
console.log("Result 2:", result2);
/* Expected: { comment: 'A comment with multiple roles.', tags: { auth: ['admin', 'user'], roles: 'editor' } } */

// Example 3: Handling invalid input gracefully with tryParse
const invalidComment = null;
const result3 = tryParse(invalidComment);
console.log("Result 3 (tryParse null):", result3);
/* Expected: { comment: undefined, tags: {} } */

// Example 4: Parsing a string that is just a tag
const tagOnly = "@onlytag";
const result4 = parse(tagOnly);
console.log("Result 4 (tag only):", result4);
/* Expected: { comment: undefined, tags: { onlytag: true } } */

// Example 5: Handling a comment without any tags
const noTagComment = "This is a plain comment.";
const result5 = parse(noTagComment);
console.log("Result 5 (no tags):", result5);
/* Expected: { comment: 'This is a plain comment.', tags: {} } */

view raw JSON →