Skip to content

parse and ParsedTrack

import { parse } from "trackparse";
parse(input: string, options?: ParseOptions): ParsedTrack

parse never fails on a string. Any string gives a ParsedTrack, the empty string included, and unknown or malformed options fall back to their defaults. What differs between the languages:

  • JavaScript (trackparse on npm): never throws on a string.
  • Python (trackparse on PyPI): raises TypeError if input isn't a str (parse() expects str input, got int), and nothing else.
  • Rust: takes &str, returns the value itself (no Result), and never panics. Use parse for the defaults and parse_with to pass options.
  • Go: takes a string, returns the Track itself (no error), and never panics. Use Parse for the defaults and ParseWith to pass options. Bytes that are not valid UTF-8 are read as U+FFFD, one per maximal invalid sequence, as Rust's String::from_utf8_lossy and the browser's TextDecoder do.
  • C (one header, trackparse.h): takes UTF-8 as a pointer and a byte length, and returns a trackparse_track * that you free with trackparse_track_free. Pass TRACKPARSE_NUL_TERMINATED as the length for a C string; with an explicit length, U+0000 is read as a character like any other. Invalid UTF-8 is read as U+FFFD with the same rule as Go and Rust. trackparse_parse_with takes an options handle, and NULL there means the defaults. The only failure is running out of memory, reported as NULL; no input makes it crash.

The time grows with the length of the input, and a track string is short. If you parse text that someone else controls, on a server for example, cap its length first: a few thousand characters is far more than any real title needs. A very long knownArtists name makes a long input slower still, so cap those names too if they come from outside.

The options are on the Options page. The other functions (parseArtists, format, createParser) are under Other functions, and the output model is defined in the spec. To see the whole result for any input, paste it into the playground and open its JSON view.

Every key is always present.

interface ParsedTrack {
input: string;
mode: Mode;
position: Position | null; // { raw: string; number: number }
timestamp: Timestamp | null; // { raw: string; seconds: number }
artists: Artist[];
title: string;
fullTitle: string;
versions: Version[];
year: number | null;
label: string | null; // the record label after a spaced bullet
flags: Flags; // { explicit, clean, unknownArtist, unknownTitle }
junk: Junk[]; // { raw: string; kind: JunkKind }
warnings: Warning[];
}

The table below uses the JavaScript and JSON names. Python has the same fields as attributes of a frozen dataclass, in snake_case (full_title, flags.unknown_artist), with tuples instead of arrays and None instead of null. Rust has a struct (ParsedTrack) with the same snake_case names as public fields: Vec<T> for arrays, Option<T> where the table says null, and enums for mode, roles, kinds and warnings. Go has a Track struct with exported CamelCase fields (FullTitle, Flags.UnknownArtist): slices for arrays, pointers where the table says null, and typed strings for mode, roles, kinds and warnings. C has a read-only trackparse_track struct with the same snake_case names as members: a pointer and a _count member for arrays (versions, versions_count), pointers that are NULL where the table says null, C enums for mode, roles, kinds and warnings, and trackparse_str for strings (a ptr and a len; ptr is also NUL-terminated, so printf("%s") works on text without U+0000). The track owns everything it points to, so nothing in it outlives trackparse_track_free.

Field Type Description
input string The original string, untouched (not normalized)
mode "clean" | "youtube" | "filename" | "tracklist" The mode that ran (R2)
position { raw: string, number: number } | null Track number. raw is the digits as written ("01") (R4)
timestamp { raw: string, seconds: number } | null Leading cue time, "1:02:03" → 3723 (R4)
artists Artist[] Primary, featured and producer credits. Remixers are in versions
title string The clean title. Brackets it doesn't recognize stay in (R8)
fullTitle string title with the versions put back in their original brackets (R9.4)
versions Version[] Remixes and versions, left to right
year number | null From (2015) or - 2015, the first in text order (R9.6)
label string | null The record label after a spaced bullet: Stigma • VISION → "VISION". As written, no lookup. Not the junk kind label (R8.7)
flags { explicit, clean, unknownArtist, unknownTitle } All booleans (R5.4, R8.6)
junk { raw: string, kind: JunkKind }[] What was taken out because it is not part of the track's identity (noise, promo text, platform names, a bracketed label, notes after a label), with its kind (R5.1)
warnings Warning[] Which heuristics fired. See Warnings

The guides show each field at work: Modes for mode, Junk, flags and prefixes for position, timestamp, year, label, flags and junk, Artists and credits for artists, and Versions and remixes for versions.

{ name: string; role: ArtistRole; joiner: string | null; source: ArtistSource }
Field Values
role primary, featured, remixer, producer
joiner the text before the name as written ("&", ",", "ft.", "prod. by"), or null for the first name in a list
source artist (left of the dash), title (right of it), version (inside a version)

&, x and vs. are joiners, not roles. Everyone they join is primary. In Rust, role and source are ArtistRole::Primary, ArtistSource::Artist and so on; in Go they are trackparse.RolePrimary, trackparse.SourceArtist and so on; in C they are TRACKPARSE_ROLE_PRIMARY, TRACKPARSE_SOURCE_ARTIST and so on, and trackparse_role_name() and trackparse_source_name() give the spec's strings. The splitting rules are R7. Artists and credits has examples.

{
type: VersionType;
raw: string;
artists: Artist[];
modifiers: VersionType[];
descriptor: string | null;
year: number | null;
unknownArtist: boolean;
delimiter: "(" | "[" | "{" | "-";
}
Field
type one of remix bootleg vip edit flip refix rework mashup blend dub mix extended radio club original instrumental acapella live acoustic remaster demo reprise cover version spedUp slowed nightcore
raw the text inside the brackets, as written
artists remixers (role remixer, source version)
modifiers type words that didn't become the type: Extended VIP Mix → vip, ["extended"]
descriptor leftover words: "Taylor's", "at Wembley", "D'n'B"
year a year inside the version: Remastered 2015 → 2015
unknownArtist true for ID Remix and the like
delimiter the bracket it was in, or - for a dash suffix

Python uses unknown_artist. Rust uses unknown_artist and version_type, because type is a reserved word, and delimiter is VersionDelimiter::Paren, Square, Curly or Dash. Go uses Type (a VersionType), UnknownArtist and the Delimiter constants DelimiterParen, DelimiterSquare, DelimiterCurly and DelimiterDash. C uses type (a trackparse_version_type) and type_name (the spec's string, which differs from trackparse_version_type_name(type) only for a type that keywords introduced), unknown_artist, and the delimiters TRACKPARSE_DELIMITER_PAREN, _BRACKET, _BRACE and _DASH. The version rules are R5.7. Versions and remixes explains how the type is chosen.

video, audio, lyrics, quality, promo, platform, label, genre, other, note. Examples are in Junk, flags and prefixes.

Three things are called a label or a note, and they are different:

  • the field label is the record label after a spaced bullet (Stigma • VISION → "VISION");
  • the junk kind label is a label name in brackets, [NCS Release], and leaves the field null;
  • the junk kind note is a segment after a second bullet (• VISION RADIO RELOAD), while the tracklist entry kind note is a whole tracklist line without a time that is not a header, a link or blank. The vocabulary is in spec/data. In Rust the kind is the enum JunkKind (JunkKind::Video and so on), in Go the typed string JunkKind (trackparse.JunkKindVideo and so on), in C the enum trackparse_junk_kind (TRACKPARSE_JUNK_VIDEO and so on) plus the string kind_name.

All five packages write the same JSON, byte for byte, on every fixture input. The keys are the camelCase ones of the spec (fullTitle, unknownArtist), also in Python, Rust, Go and C.

import { parse } from "trackparse";
const track = parse("Noisia - Stigma (2015)");
JSON.stringify(track);
// '{"input":"Noisia - Stigma (2015)","mode":"clean","position":null,"timestamp":null,"artists":[{"name":"Noisia","role":"primary","joiner":null,"source":"artist"}],"title":"Stigma","fullTitle":"Stigma","versions":[],"year":2015,"label":null,"flags":{"explicit":false,"clean":false,"unknownArtist":false,"unknownTitle":false},"junk":[],"warnings":[]}'

The result is a plain object, so JSON.stringify is all you need, and JSON.parse gives the object back.

Tracklist results serialize the same way: JSON.stringify(list), tl.to_dict(), list.to_json(), json.Marshal(list) and trackparse_tracklist_to_json(list). See the Tracklist API and the JSON section of the tracklist guide.

All of these are exported from trackparse as TypeScript types:

ParsedTrack, Artist, ArtistRole, ArtistSource, Version, VersionType, VersionDelimiter, Junk, JunkKind, Flags, Position, Timestamp, Warning, Mode, ModeOption, ParseOptions, KeywordOptions, FormatOptions, and Parser, the type createParser returns.

The JSON shape is also published as a JSON Schema, parsed-track.schema.json.