Snapdragon Utilities

5.0.1 · active · verified Tue Apr 21

snapdragon-util provides a focused collection of utility functions designed to interact with the Abstract Syntax Tree (AST) nodes generated or consumed by the `snapdragon` parser/compiler ecosystem. Key functionalities include robust node type checking (`isNode`), efficient value extraction from nodes (`value`), and critical compiler middleware helpers such as `noop` (for ignoring nodes), `identity` (for direct value appending), `append` (for custom value appending), and `toNoop` (for transforming nodes into empty text nodes without re-indexing). The package is currently at version `5.0.1` and is part of a stable, actively maintained ecosystem primarily targeting Node.js environments. Its core differentiation lies in its tight integration with `snapdragon-node` instances, making it indispensable for extending or customizing `snapdragon`-based parsers and compilers.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `snapdragon-util` including node identification, value extraction, and conceptual use of the `append` compiler middleware.

const util = require('snapdragon-util');
const Node = require('snapdragon-node');

// Create a simple AST node
const textNode = new Node({ type: 'text', value: 'Hello' });
const wildcardNode = new Node({ type: 'star', val: '*' });

// Use isNode to check node type
console.log('Is textNode a Snapdragon Node?', util.isNode(textNode));
console.log('Is a plain object a Snapdragon Node?', util.isNode({}));

// Extract value using the value utility
console.log('Value of textNode:', util.value(textNode));
console.log('Value of wildcardNode:', util.value(wildcardNode));

// Example of using 'append' for a compiler (conceptual)
// In a real snapdragon compiler, you'd set this as middleware
const mockCompiler = { output: '' };
const appendGreeting = util.append('Hello World');

// Simulate calling the append middleware with a node and a compiler context
appendGreeting.call(mockCompiler, { type: 'greeting' });
console.log('Mock compiler output after append:', mockCompiler.output);

view raw JSON →