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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ansi-tools/parser

v1.0.12

Published

Tokenize and parse strings containing ANSI escape sequences and control codes

Readme

@ansi-tools/parser

Parser for ANSI escape sequences.

Features

  • ✅ Supports CSI, OSC, DCS, ESC, APC, SOS, PM, etc.
  • ✅ Handles 7-bit (\x1b or \u001b) and 8-bit (\u009b) introducers
  • ✅ Handles octal (\033) and shorthand \e introducers (only escaped)
  • ✅ Multiple string terminators (\x1b\\, \x07)
  • ✅ Zero dependencies
  • ✅ Separate optimized modules for raw and escaped input

Used by ansi.tools.

Installation

npm install @ansi-tools/parser

Usage

import { parse } from "@ansi-tools/parser";

const input = "\x1b[31mHello\x1b[0m World";

for (const code of parse(input)) {
  console.log(code);
}

There is a difference between escaped and unescaped input. Only with an escaped input string the raw input and the positions can be preserved in the tokens and control codes. See the example below for the default and the /escaped import.

The default and unescaped tokenization is roughly ~30% faster. Use this if you just need the control codes.

Examples

Default (raw/unescaped)

import { parse } from "@ansi-tools/parser";

parse(`\x1b[31mHello\x1b[0m`);

// result:
[
  {
    type: "CSI",
    pos: 0,
    raw: "\u001b[31m",
    command: "m",
    params: ["31"],
  },
  {
    type: "TEXT",
    pos: 5,
    raw: "Hello",
  },
  {
    type: "CSI",
    pos: 10,
    raw: "\u001b[0m",
    command: "m",
    params: ["0"],
  },
];

Escaped

import { parse } from "@ansi-tools/parser/escaped";

parse(String.raw`\x1b[31mHello\x1b[0m`);

// result:
[
  {
    type: "CSI",
    pos: 0,
    raw: "\\x1b[31m",
    command: "m",
    params: ["31"],
  },
  {
    type: "TEXT",
    pos: 8,
    raw: "Hello",
  },
  {
    type: "CSI",
    pos: 13,
    raw: "\\x1b[0m",
    command: "m",
    params: ["0"],
  },
];

Tokenizer & generators

The tokenizer and generators are also available, for both the default and the /escaped versions.

tokenize

import { tokenize } from "@ansi-tools/parser";

const input = "\x1b[31m";

for (const token of tokenize(input)) {
  console.log(token);
}

Generators

import { tokenizer, parser } from "@ansi-tools/parser";

const input = "\x1b[31mHello\x1b[0m";

const tokens = tokenizer(input);

const codes = parser(tokens);

for (const code of codes) {
  console.log(code);
}

Type Definitions

function tokenize(input: string): TOKEN[];
function parse(input: string): CODE[];

function* tokenizer(input: string): Generator<TOKEN>;
function* parser(tokens: Generator<TOKEN>): Generator<CODE>;

type TOKEN = {
  type: "INTRODUCER" | "DATA" | "FINAL" | "TEXT";
  pos: number;
  raw: string;
  code?: string;
  intermediate?: string;
};

type CONTROL_CODE = {
  type: "CSI" | "DCS" | "DEC" | "ESC" | "OSC" | "SGR" | "STRING" | "PRIVATE";
  command: string;
  raw: string;
  params: string[];
  pos: number;
};

type TEXT = {
  type: "TEXT";
  raw: string;
  pos: number;
};

type CODE = CONTROL_CODE | TEXT;

License

ISC