micromark Character Utilities

2.1.1 · active · verified Sun Apr 19

micromark-util-character is a utility package within the micromark ecosystem, providing a collection of pure functions to efficiently check whether a given character code belongs to various predefined groups, such as ASCII alphanumeric, punctuation, or Markdown-specific line endings and spaces. It is currently at version 2.1.1. As part of the larger micromark project, its release cadence is tied to the main project's development. This package is crucial for developers building custom micromark extensions or parsers who need granular control and performant character classification, differentiating itself by offering a specialized, low-level API for fundamental parsing operations rather than general-purpose string manipulation.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use several utility functions to check character codes for various properties, such as being an ASCII alpha, Markdown line ending, or Unicode punctuation. It also shows a simple custom function combining these utilities.

import { asciiAlpha, markdownLineEnding, unicodePunctuation, asciiDigit } from 'micromark-util-character';

console.log('--- Character Checks ---');

const charA = 65; // 'A'
const charNewline = 10; // '\n'
const charAmpersand = 38; // '&'
const charDigit = 50; // '2'
const charSpace = 32; // ' '
const charUnicodePunctuation = 8212; // '—' (em dash)

console.log(`Is '${String.fromCharCode(charA)}' an ASCII alpha character? ${asciiAlpha(charA)}`);
console.log(`Is '${String.fromCharCode(charNewline)}' a Markdown line ending? ${markdownLineEnding(charNewline)}`);
console.log(`Is '${String.fromCharCode(charAmpersand)}' a Unicode punctuation character? ${unicodePunctuation(charAmpersand)}`);
console.log(`Is '${String.fromCharCode(charDigit)}' an ASCII digit character? ${asciiDigit(charDigit)}`);
console.log(`Is '${String.fromCharCode(charSpace)}' a Markdown space? ${markdownLineEnding(charSpace)}`);
console.log(`Is '${String.fromCharCode(charUnicodePunctuation)}' a Unicode punctuation character? ${unicodePunctuation(charUnicodePunctuation)}`);

// Demonstrating a common pattern for custom parsers:
function isStartOfWord(code) {
  return asciiAlpha(code) || asciiDigit(code);
}

const testCode = 'H'.charCodeAt(0);
console.log(`Is '${String.fromCharCode(testCode)}' a start of word? ${isStartOfWord(testCode)}`);

view raw JSON →