Skip to content

Formatting

format(track, options?) renders a parsed track back to a string. In Python it is format_track(track, **options). In Rust it is format_track(&track), or format_track_with(&track, &options) with options. In Go it is trackparse.Format(track), or trackparse.FormatWith(track, options). In C it is trackparse_format(track), or trackparse_format_with(track, options), and the string it returns is freed with trackparse_string_free (trackparse.h). Use it to bring a messy library to one style, or to build a display title without the junk. The rules are R11 in the spec.

import { format, parse } from "trackparse";
const t = parse(
"01. Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix) [Official Video]",
);
format(t);
// 'Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)'

Junk is never written back. Everything else is, by default, the way it was written. The playground has a format() panel with this track loaded.

Option Values Default
feat "source" keep features where they were, "artist" move them left of the dash, "title" move them into the title, "omit" drop them (R11.2) "source"
featMarker the word to write before features the one from the input
joiners "original" as written, "canonical" normalized (and → &, ; → ,, ft. → feat.; R11.5) "original"
versions "all" or "none" "all"
producers write (prod. by …) credits true
position write the track number back false
label write the label back after the title, • VISION (R11.3) true

Python takes the options as keyword arguments, so featMarker is feat_marker. Rust takes a FormatOptions value built with one setter per option, and the values are enums (FeatPlacement, JoinerStyle, VersionsOption). Go takes a FormatOptions struct whose zero value is the default, and the values are typed string constants (FeatPlacement, JoinerStyle, VersionsOption); trackparse.Ptr sets FeatMarker. Since a Go bool field defaults to false, producers becomes OmitProducers and label becomes OmitLabel. C takes a trackparse_format_options handle from trackparse_format_options_new(), with one setter per option and C enums for the values; an option you don't set keeps its default, and trackparse_format_options_free releases the handle. Each tab below starts with the names in that language.

The names and values are those of the table above, as keys of one object.

format(t, { feat: "title" });
// 'Wilkinson - Afterglow (ft. Becky Hill & Tom Cane) (Sub Focus VIP Remix)'
format(t, { feat: "title", joiners: "canonical" });
// 'Wilkinson - Afterglow (feat. Becky Hill & Tom Cane) (Sub Focus VIP Remix)'
format(t, { feat: "artist", featMarker: "featuring" });
// 'Wilkinson featuring Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)'
format(t, { feat: "omit" }); // 'Wilkinson - Afterglow (Sub Focus VIP Remix)'
format(t, { versions: "none" }); // 'Wilkinson ft. Becky Hill & Tom Cane - Afterglow'
format(t, { position: true }); // '01. Wilkinson ft. Becky Hill & Tom Cane - Afterglow (Sub Focus VIP Remix)'
format(parse("Future - Mask Off (prod. by Metro Boomin)"), { producers: false });
// 'Future - Mask Off'

A label after a bullet is written back after the versions and producers, as " • " + label. Notes are junk, so they are not (try it):

const l = parse("Noisia - concussion • VISION • VISION RADIO RELOAD");
format(l); // 'Noisia - concussion • VISION'
format(l, { label: false }); // 'Noisia - concussion'

Parsing the formatted string gives back the same artist names and roles, the same title, the same version types and remixers and the same label, as long as the first parse raised no warnings. The spec makes this an invariant (R11.6), and the property tests check it on generated input:

const s = "Noisia feat. Foreign Beggars - Shellshock (Phace Remix)";
// same artists, title and versions as parse(s):
const again = parse(format(parse(s)));
again.fullTitle; // 'Shellshock (Phace Remix)'

The label has two exceptions: it only comes back when the formatted string has an artist part and a title. ID - Stigma • VISION has an unknown artist, so it formats to Stigma • VISION. That string has no dash, and without one the bullet is not read as a label: the second parse gives the title Stigma • VISION, no label and a noSeparator warning. Noisia - [HD] • VISION has an empty title (the [HD] is junk), so it formats to Noisia - • VISION, and a bullet right after the dash starts no label either: the second parse gives the title • VISION.

The playground has a format() panel if you want to try the options on this track or your own strings. Its code view writes the call for each language.