Overview
CommonFormats is a collection of pre-configured FormatDefinition objects that provide standard definitions for common file formats. These definitions reduce boilerplate when creating format handlers and ensure consistency across the codebase.
Import
FormatDefinition Class
Each entry inCommonFormats is an instance of the FormatDefinition class, which contains metadata about a file format.
Constructor
Long, descriptive name of the format (e.g., “Portable Network Graphics”)
Short, formal name used for identification (e.g., “png”)
File extension without the dot (e.g., “png”)
MIME type (e.g., “image/png”)
One or more categories from the
Category constant. Formats can belong to multiple categories.Categories
TheCategory object defines standard format categories:
Builder Pattern
TheFormatDefinition class provides a fluent builder API to create FileFormat objects for use in handlers.
Builder Methods
Creates a builder with the specified internal reference identifier
Marks format as supporting conversion FROM this format. Defaults to
trueMarks format as supporting conversion TO this format. Defaults to
trueMarks the format as lossless in this context. Defaults to
trueOverride the format name
Override the format identifier
Override the file extension
Override the MIME type
Replace the format category
Apply multiple overrides at once using a partial object
Alternative: supported() Method
For simple cases, use thesupported() method instead of the builder:
Available Formats
Image Formats
Portable Network Graphics
- Format:
png - Extension:
.png - MIME:
image/png - Category:
IMAGE
Joint Photographic Experts Group JFIF
- Format:
jpeg - Extension:
.jpg - MIME:
image/jpeg - Category:
IMAGE
WebP
- Format:
webp - Extension:
.webp - MIME:
image/webp - Category:
IMAGE
Tagged Image File Format
- Format:
tiff - Extension:
.tiff - MIME:
image/tiff - Category:
IMAGE
Microsoft Windows bitmap image
- Format:
bmp - Extension:
.bmp - MIME:
image/bmp - Category:
IMAGE
CompuServe Graphics Interchange Format (GIF)
- Format:
gif - Extension:
.gif - MIME:
image/gif - Categories:
IMAGE,VIDEO
Scalable Vector Graphics
- Format:
svg - Extension:
.svg - MIME:
image/svg+xml - Categories:
IMAGE,VECTOR,DOCUMENT
Data Formats
JavaScript Object Notation
- Format:
json - Extension:
.json - MIME:
application/json - Category:
DATA
Extensible Markup Language
- Format:
xml - Extension:
.xml - MIME:
application/xml - Category:
DATA
YAML Ain’t Markup Language
- Format:
yaml - Extension:
.yml - MIME:
application/yaml - Category:
DATA
Comma Separated Values
- Format:
csv - Extension:
.csv - MIME:
text/csv - Category:
DATA
Text & Document Formats
Plain Text
- Format:
text - Extension:
.txt - MIME:
text/plain - Category:
TEXT
Hypertext Markup Language
- Format:
html - Extension:
.html - MIME:
text/html - Categories:
DOCUMENT,TEXT
Markdown Document
- Format:
markdown - Extension:
.markdown - MIME:
text/markdown - Categories:
DOCUMENT,TEXT
Portable Document Format
- Format:
pdf - Extension:
.pdf - MIME:
application/pdf - Category:
DOCUMENT
Code Formats
Windows Batch file
- Format:
batch - Extension:
.bat - MIME:
text/windows-batch - Category:
TEXT
Python Script
- Format:
py - Extension:
.py - MIME:
text/x-python - Category:
CODE
Shell Script
- Format:
sh - Extension:
.sh - MIME:
application/x-sh - Category:
TEXT
Windows Portable Executable
- Format:
exe - Extension:
.exe - MIME:
application/vnd.microsoft.portable-executable - Category:
CODE
Audio Formats
MP3 Audio
- Format:
mp3 - Extension:
.mp3 - MIME:
audio/mpeg - Category:
AUDIO
Waveform Audio File Format
- Format:
wav - Extension:
.wav - MIME:
audio/wav - Category:
AUDIO
Ogg Audio
- Format:
ogg - Extension:
.ogg - MIME:
audio/ogg - Category:
AUDIO
Free Lossless Audio Codec
- Format:
flac - Extension:
.flac - MIME:
audio/flac - Category:
AUDIO
Video Formats
MPEG-4 Part 14
- Format:
mp4 - Extension:
.mp4 - MIME:
video/mp4 - Category:
VIDEO
Archive Formats
ZIP Archive
- Format:
zip - Extension:
.zip - MIME:
application/zip - Category:
ARCHIVE
Microsoft Office Formats
WordprocessingML Document
- Format:
docx - Extension:
.docx - MIME:
application/vnd.openxmlformats-officedocument.wordprocessingml.document - Category:
DOCUMENT
SpreadsheetML Workbook
- Format:
xlsx - Extension:
.xlsx - MIME:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet - Categories:
SPREADSHEET,DOCUMENT
PresentationML Presentation
- Format:
pptx - Extension:
.pptx - MIME:
application/vnd.openxmlformats-officedocument.presentationml.presentation - Categories:
PRESENTATION,DOCUMENT
Font Formats
TrueType Font
- Format:
ttf - Extension:
.ttf - MIME:
font/ttf - Category:
FONT
OpenType Font
- Format:
otf - Extension:
.otf - MIME:
font/otf - Category:
FONT
Web Open Font Format
- Format:
woff - Extension:
.woff - MIME:
font/woff - Category:
FONT
Web Open Font Format 2.0
- Format:
woff2 - Extension:
.woff2 - MIME:
font/woff2 - Category:
FONT
Music Notation Formats
MusicXML
- Format:
musicxml - Extension:
.musicxml - MIME:
application/vnd.recordare.musicxml+xml - Category:
DOCUMENT
MusicXML Compressed
- Format:
mxl - Extension:
.mxl - MIME:
application/vnd.recordare.musicxml - Category:
DOCUMENT
Usage Examples
Basic Usage with Builder Pattern
Font Handler Example
Real-world example from the font handler (src/handlers/font.ts:232):Using supported() Method
Overriding Format Properties
Working with Categories
See Also
- FormatHandler - Main handler interface
- normalizeMimeType - MIME type normalization utility