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

@dierkens.dev/ptcgl-battle-log-parser

v2.0.0

Published

Zero-dependency parser for Pokémon TCG Live battle-log exports. 7 locales, structured AST, no runtime deps.

Readme

@dierkens.dev/ptcgl-battle-log-parser

CI npm version coverage floor

Parse Pokémon TCG Live battle-log exports into a typed AST. Zero runtime dependencies, ESM-only, 7 locales.

import { analyzeBattleLog } from "@dierkens.dev/ptcgl-battle-log-parser";

// The text the game's in-app "Copy Log" button hands you.
const raw = `Setup
Alice drew 7 cards for the opening hand.

Alice's Turn
Alice ended their turn.`;

const result = analyzeBattleLog(raw, { playerName: "Alice" });
if (!result.ok) throw result.error;

result.value.locale;            // "en"
result.value.playerName;        // "Alice"
result.value.summary.turnCount; // 1

Every line you feed it is matched back to the blog_loc_* localization template it was rendered from. Nothing is approximated. Where the game uses identical text for different keys and the line cannot say which, templateKey is null and candidateKeys lists them, rather than guessing; see About the template bundles.


Why this exists

PTCG Live's in-app Copy Log button is the only way to get structured data out of a match. It hands you a wall of localized prose. Every tool built on top of it re-solves the same problem: strip the prefixes, match each line to a blog_loc_* template, and rebuild the tree.

This package is that parser, done once and done properly. It is extracted from PokeDojo's private codebase, rewritten to drop an effect system and a schema library, and published under MIT so anyone building a deck tool, a tournament tracker, or a log viewer can use it.

It does one thing. Parsing. There is no card database, no deck validation, no archetype detection, no network access. If you want analysis on top of the AST, build it — the AST is deliberately close to the log's own model.

Install

pnpm add @dierkens.dev/ptcgl-battle-log-parser
npm  install @dierkens.dev/ptcgl-battle-log-parser

Requires Node >= 22.12.0 — the first release where JSON import attributes are stable, which the locale bundles rely on. The raw syntax floor is lower (import attributes landed unflagged in 20.10), but 22.12 is the oldest line still receiving security fixes, so that is where the floor sits. ESM only — no CommonJS build.

The bundles are JSON modules imported with with { type: "json" }, so your toolchain has to understand import attributes. The floors: esbuild 0.19.7, Rollup 4, webpack 5.92, Vite 5, and TypeScript 5.3. Bun, and Deno >= 2.0, read it directly.

What you get

interface BattleLog {
  phases: Phase[];
}

// A phase is one of three kinds. Only a Turn belongs to a side.
type Phase = SetupPhase | CheckupPhase | TurnPhase;

interface SetupPhase {
  kind: "Setup";                       // the pre-turn opening block
  mainEntries: MainEntry[];
  plainTextPhaseTitle: string;
}

interface CheckupPhase {
  kind: "Checkup";                     // the between-turns block
  mainEntries: MainEntry[];
  plainTextPhaseTitle: string;
}

interface TurnPhase {
  kind: "Turn";
  side: "self" | "opponent";           // resolved, never observed
  sideSource: "declared" | "inferred"; // "declared" when you named the player;
                                       // "inferred" when the first turn header guessed
  playerName: string;                  // the player whose turn it is
  displayTurnNumber: number;           // the game's own (turn + 1) / 2
  mainEntries: MainEntry[];
  plainTextPhaseTitle: string;
}

interface MainEntry {
  event: TemplateEvent;
  subEntries: SubEntry[];
}

interface SubEntry {
  event: TemplateEvent;
  subString: string | null;          // free-form "   • " text, e.g. card lists
}

interface TemplateEvent {
  templateKey: string | null;        // "blog_loc_played_to_bench"; null when the line can't say
  candidateKeys: [string, ...string[]]; // every key not ruled out; [templateKey] when decided (ADR-0009)
  groups: Record<string, string>;     // { playerName: "cdierkens", cardName: "Slowpoke" }
  raw: string;                       // the line as parsed: trailing whitespace stripped,
                                    // never carrying leading padding (see ADR-0004)
}

The AST mirrors the exported log's own shape: BattleLog → Phase[] → MainEntry[] → SubEntry[]. parseBattleLog is the mathematical inverse of that export — feed it the text, get the tree back.

API

| Export | Purpose | |---|---| | analyzeBattleLog(raw, opts?) | The one-call path: detect locale → parse → identify players → summarise. | | parseBattleLog(raw, opts?) | Parse to the AST. Returns a Result. | | unwrap(result) | Get the value, or throw the error. Works on every fallible export. | | detectPlayers(parsed) | Infer who the local player was. | | deriveGameSummary(parsed, opts) | Per-side knockouts, prizes, winner. | | detectBattleLogLanguage(log) | Guess the locale from the text. | | compileTemplate, createTemplateMatcher | Match against your own template bundle. | | blogTemplateBundles, blogTemplateMatchers, ALL_BLOG_LOCALES | The shipped data, if you want to inspect it. |

Errors are values

Parsing fails in exactly one way — a line matches no shipped template — and that is a value you usually want to branch on, not an exception. Every fallible export returns a discriminated union on ok:

import { isErr } from "@dierkens.dev/ptcgl-battle-log-parser";

const result = parseBattleLog(raw, { locale: "de" });
if (isErr(result)) {
  console.error(result.error.lineNumber, result.error.line);
}

Errors are also real Error subclasses carrying an _tag, so instanceof and tag narrowing both work. To throw instead of branching, pass the result to unwrap — there is no separate *OrThrow twin to keep in sync:

import { unwrap, UnmatchedBattleLogLineError } from "@dierkens.dev/ptcgl-battle-log-parser";

try {
  const log = unwrap(parseBattleLog(raw));
} catch (e) {
  if (e instanceof UnmatchedBattleLogLineError) {
    console.error(`line ${e.lineNumber}: ${e.line}`);
  }
}

Player identity

A battle log names both players but never says which one is you. This package infers it from the opening-hand reveal: when the local player goes second, the client prints their mulligged cards back to the log. That is a strong signal, not a heuristic — the opponent's hand is never revealed.

When the local player went first, there is no reveal and inference is not possible from the log alone. You get PlayerDetectionError with reason: "ambiguous". Pass the name and the step is skipped:

analyzeBattleLog(raw, { playerName: "cdierkens" });

⚠️ Every count in GameSummary is attributed by comparing against this name. Supply the wrong one and the summary inverts silently rather than failing. There is a test pinning that behaviour (analyze-battle-log.test.ts, "mirrors self/opponent…") precisely because it is a footgun.

Recipes

Summarise a folder of logs. analyzeBattleLog detects the locale, parses, resolves the players and summarises in one call.

import { readFileSync, readdirSync } from "node:fs";
import { analyzeBattleLog, isErr } from "@dierkens.dev/ptcgl-battle-log-parser";

const rows = readdirSync("./logs").map((name) => {
  const result = analyzeBattleLog(readFileSync(`./logs/${name}`, "utf8"));
  if (isErr(result)) return { name, failed: result.error._tag };
  const { locale, summary } = result.value;
  return { name, locale, turns: summary.turnCount, winner: summary.winner };
});

Read every card that hit the Bench. The AST carries each line's template key, so you filter on that rather than on prose. The opening reveal in Setup is play_to_bench; a card played from hand during a turn is played_to_bench.

import { unwrap, parseBattleLog } from "@dierkens.dev/ptcgl-battle-log-parser";

const benchKeys = new Set(["blog_loc_play_to_bench", "blog_loc_played_to_bench"]);
const log = unwrap(parseBattleLog(raw, { locale: "en" }));
for (const phase of log.phases) {
  for (const main of phase.mainEntries) {
    const key = main.event.templateKey;
    if (key === null || !benchKeys.has(key)) continue;
    console.log(main.event.groups["playerName"], "→", main.event.groups["cardName"]);
  }
}

Throw instead of branching. Every fallible export returns a Result; unwrap is the one place that turns that back into a throw.

const log = unwrap(parseBattleLog(raw)); // throws UnmatchedBattleLogLineError

Match against a bundle you built yourself. matcher takes precedence over locale, so this works for a locale we do not ship, or a bundle you compiled from your own strings.

import { createTemplateMatcher, parseBattleLog } from "@dierkens.dev/ptcgl-battle-log-parser";

const matcher = createTemplateMatcher({
  blog_loc_phase_setup: { placeholders: [], template: "Begin." },
  // …
});
const result = parseBattleLog(raw, { matcher });

Stability

1.0.0 is a promise, not a milestone. The public API is a contract: once there are consumers, a breaking change to anything in Tier 1 needs a major version.

Pre-adoption, the promise is dormant. 1.1.0 shipped a shape correction — battlePhase became a kind/side split, see ADR-0005 — that would have been a major if anyone had adopted 1.0.0. Nobody had, so nothing broke: with no consumers there is no migration for a version number to signal. The promise binds unconditionally from the first adopted version, and 1.1.0 is the last pre-adoption correction.

2.0.0 replaced the key. templateKey: string could name a key the line did not determine, so it became null | string, and candidateKeys replaced 1.3.0's alternateKeys with no deprecation minor (ADR-0009). The only consumer is the author's own.

The exports do not all carry the same promise, because they are not all the same kind of thing:

| Tier | Exports | The promise | |---|---|---| | 1 — the contract | analyzeBattleLog · parseBattleLog · detectPlayers · deriveGameSummary · detectBattleLogLanguage · unwrap · ok · err · isOk · isErr · UnmatchedBattleLogLineError · PlayerDetectionError · the Result union and the options types · and the shapes BattleLog · Phase (SetupPhase · CheckupPhase · TurnPhase) · PhaseKind · Side · SideCounts · SideSource · MainEntry · SubEntry · TemplateEvent · BattleLogAnalysis · GameSummary · DetectedPlayers · AnalyzeBattleLogError · PlayerDetectionReason | Frozen. Semver applies to type shapes and observable behaviour. | | 2 — advanced | compileTemplate · createTemplateMatcher · blogTemplateBundles · blogTemplateMatchers · ALL_BLOG_LOCALES · BlogLocale · the template types (TemplateBundle, TemplateEntry, TemplateMatch, TemplateMatcher) | Public and stable, but outside the promise. A game-client change can move these in a minor — never silently. |

Excluded from the promise, and said plainly:

  • the shipped template strings and the template-key set — the game client can add or reword keys whenever it likes, and this package follows it;
  • raw and groups for any given log — they are whatever the client printed;
  • performance characteristics.

Deprecation policy. A Tier 1 export is deprecated in a minor with a changelog note, and removed only in a major — never without a full minor of warning. A Tier 2 change can land in a minor, but it still gets a changelog entry.

The reasoning, and the alternatives rejected, are in ADR-0002.

Localisation

Seven locales ship in the box, detected automatically:

de · en · es · es_la · fr · it · ptbr

Detection scores each locale's bundle against the log's non-blank lines and takes the best. es and es_la share phase headers, so they separate on body strings only. Detection always returns a usable locale — unrecognisable input falls back to "en" — so it is a heuristic, not a guarantee. Pass locale explicitly when you already know it.

About the template bundles

src/templates/ holds the blog_loc_* strings for each supported locale. The parser cannot match a battle log without them, and they are checked in so that npm install gives you a package that works out of the box. See LICENSE for how the strings sit alongside the MIT grant.

All seven bundles share the same 228 keys; only the values differ.

A key does not always name a distinguishable line. Some keys' templates differ at most in slot names, which never print, so they render byte-identical lines. Where the phase decides the key, the parser picks it (ADR-0008). Otherwise templateKey is null and candidateKeys lists every key the line could be (ADR-0009). Every locale has:

| Keys with identical rendered lines | templateKey | |---|---| | play_to_active · played_to_active | play_to_* in Setup (opening reveal), played_to_* elsewhere (played from hand) | | play_to_bench · played_to_bench | as above | | played_card · played_to_fallback | always played_card; the game never emits played_to_fallback | | discarded_from_board · discarded_from_hand | null — undecided. _hand covers own hand or deck; _board covers Active, Bench or stadium | | damage_blocked · effect_prevention_damage · effect_prevention_damagecounters | null — undecided | | effect_prevention_allsubactions · effect_prevention_onlyeffects | null — undecided | | damage_detail_conditional_is_property · damage_detail_conditional_is_type | null — undecided; slots [pokemonProperty] vs [pokemonType] |

Per locale:

  • every locale except fr: effect_prevention_allactioneffects · effect_prevention_allcardeffects (slots [attackName] vs [cardName]);
  • fr: healed · healed_dc ([numDamage] vs [numDamageCounters], both printed as "dégâts");
  • ptbr: shuffled_card_into_deck · shuffled_card_into_hand.

A key's distinguishability depends on the locale; read candidateKeys rather than this table. When templateKey is null, groups and the match's template use the slot names of candidateKeys[0].

What a line does not say

The parser reports the game's placeholder names as the game declares them. Some of those names do not describe what the game puts in them. See the notes below for where each claim comes from:

  • Placed damage counters (player_placed_dc, player_placed_single_dc). The leading [cardName] slot is the acting player, by the client's design (5,370 of 5,370 lines). [targetPlayerName] is the owner of the card whose effect placed the counters; the client falls back to the target's owner only when that owner is unknown. It held the owner of the most recent used line in the phase in 5,192 of the 5,268 lines that had one. The line does not name the target's owner; the target may be on either side.
  • used_attack ([player]'s [pokemon] used [attackName].) also renders Abilities, and the header of a modified or blocked action. The line cannot tell an attack from an Ability.

Corpus counts are over 411 stored English logs (2026-10-27). The client behaviour is from reading the client's battle-log code and key mapping; only the derived facts are recorded here.

Why blog_loc_

The prefix is the battle-log vocabulary every key here uses: "blog" is short for BattleLog, not blogging.

Keeping that naming means every key in the AST refers to exactly one key in the game's bundle. A non-null templateKey is one the line determines (see above).

The convention has exactly one exception, and it used to break this parser: the string that marks a drawn rock-paper-scissors flip does not carry the prefix, while its siblings blog_loc_rock / _paper / _scissors do. Filtering on the prefix alone dropped that one string, and a drawn flip produced an unparseable log. There is an explicit exceptions list, and a regression test guards it.

Adding non-English fixtures

This is the package's biggest gap, and the easiest thing to help with.

Every real fixture in src/__tests__/fixtures/ is English. PTCG Live never writes battle logs to disk — you copy them out by hand — so there is no way to harvest other locales in bulk. Three claims, kept separate on purpose:

  1. The locale tests build synthetic logs from the shipped template strings, and prove the matcher and the detector work for all seven locales. That is a genuine test of our data and our code.
  2. They do not prove that a client set to, say, German renders its log lines in exactly those shapes. Only a captured log can show that.
  3. Only a hand-captured fixture closes the gap — and the maintainers cannot produce one for a locale they do not play.

If you play in any language other than English, one match and one command is the whole contribution:

  1. Play a match. In-app → Copy Log. Save the text to a file.

  2. Run the fixture tool with your own handle:

    pnpm run fixtures:prepare -- --log ~/Downloads/meine-runde.log --player YourHandle

    It detects the locale, parses the log against the shipped bundle, redacts both player handles with fixed stand-ins, and writes src/__tests__/fixtures/<name>.log, its golden, and its shape file. It prints the CASES line to add to the golden test.

  3. Read the shape file before committing. It is ~120 lines on purpose: it shows phase shape, resolved player names, and how often each template key fired — which is exactly what a locale that renders a shape differently will move. The ~3,000-line golden beside it is the regression net, not something anyone reads end to end.

If the log does not parse, the tool stops and prints the single unmatched line and its number, and writes nothing. That line is the whole report — open a failing-log issue with that one line, player handles replaced, rather than the whole log. UnmatchedBattleLogLineError carries the line number and text precisely so a fix can start from evidence.

Why the handles are redacted

The opponent never agreed to appear in a public repository, and neither did you. Redaction happens in the tool, before anything is committed, and the parser treats handles as opaque strings — nothing downstream depends on what they were. Swapping all of them for TestPlayer / TestOpponent changes no behaviour the tests assert. It is deterministic, not best-effort.

Verification

The parser is a port of PokeDojo's Effect-TS implementation. Fidelity is not asserted — it is measured:

  • Four real battle-log fixtures are compared against the ported parser.
  • Each is asserted with toEqual and with a byte-exact JSON.stringify comparison, so key-order drift fails too.
  • Alongside each golden sits a <fixture>.shape.json: phase shape, resolved player names, and per-template-key counts, in ~120 lines rather than ~3,000. Reviewable in a diff; the full golden is not.
  • A sweep test asserts that every content line in every fixture matches some template — the property that makes the goldens possible at all.
  • Locale support is tested per locale: bundles are asserted to share the English 228-key set, and synthetic logs built from the shipped strings drive detection and parsing for all 7.
✓ src/__tests__/parse-battle-log.test.ts    (32 tests)
✓ src/__tests__/locale.test.ts             (55 tests)
✓ src/__tests__/template-matcher.test.ts   (31 tests)
✓ src/__tests__/analyze-battle-log.test.ts (22 tests)
✓ src/__tests__/detect-players.test.ts     ( 6 tests)
✓ src/__tests__/index.test.ts              ( 5 tests)
✓ src/__tests__/types.test.ts              ( 9 tests)
Tests  160 passed (160)

One test is a canary rather than a regression: no fixture may produce a used_attack whose attackName contains a digit. A swallowed damage clause contains one in every locale, so the check is language-independent — and it fails on the 1.1.0 behaviour, which a golden cannot do, because that golden was generated from the wrong parse.

Deliberate deviations from the original

Four behavioural differences from the PokeDojo implementation, all fixes:

  1. Player identity is resolved once, at parse time, onto Phase.playerName. The original re-matched a phase's raw header line to recover the player's name, and detectPlayers did so with the hardcoded English matcher — so on any non-English log it found no names at all. A log whose names appear only in turn headers (a battle cut off mid-play) reported no-players-found instead of ambiguous. The phase now carries the name it was resolved from, so nothing re-matches and the path is locale-independent.
  2. Duplicate placeholder declarations are ignored when ranking specificity. 50 shipped bundle entries listed a repeated placeholder twice, which inflated the computed literal length and mis-ranked those templates. The bundles are deduplicated. Golden output is unchanged.
  3. Straight and curly apostrophes are interchangeable when matching (ADR-0006). The client prints either form for the same template, and matching only the printed one silently downgraded a damage line to used_attack, with the damage clause swallowed into the attack name. Six lines in slowking-vs-beedrill.log change template key as a result. Captures are unchanged: a card name keeps whichever apostrophe the log printed.
  4. Identical templates are reported, and the phase decides placements (ADR-0007, ADR-0008). The original returned the first identical key in bundle order, so every bench placement during a turn came back as the Setup reveal play_to_bench, and every discard as discarded_from_board. Placements are now play_to_* in Setup and played_to_* in a turn; undecidable ties have a null templateKey and list every key in candidateKeys (ADR-0009).

Development

pnpm install
pnpm run verify     # typecheck + lint + test:coverage + build

Individual steps:

pnpm run typecheck
pnpm run test:unit
pnpm run test:coverage
pnpm run build

pnpm run build and pnpm run test are aggregators over the build:* and test:* scripts, so adding build:foo is enough to have it run.

Regenerating goldens:

pnpm run test:unit     # note: `--update` does nothing here

Goldens are readFileSync comparisons, not vitest snapshots, so there is nothing to update. src/__tests__/parse-battle-log.test.ts documents the regeneration procedure: parse the fixture, rewrite the <name>.json golden and the <name>.shape.json projection.

Versioning is changesets:

pnpm exec changeset          # add a changeset
pnpm run version             # apply changesets to package.json + CHANGELOG.md

Releases are automated from a tag — see docs/RELEASING.md.

Licence

MIT — see LICENSE. Pokémon and Pokémon TCG Live are trademarks of The Pokémon Company. This project is not affiliated with or endorsed by them.