format

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2026 License: MIT Imports: 6 Imported by: 0

README

encode/format

Subset port of d3-format + a strftime-style mini-mapping for time formats. Pure stdlib; no external date library.

Supported number specifiers

Spec Input Output Notes
f 1234.5 1234.500000 Default 6-decimal fixed point.
.2f 1234.5 1234.50 Fixed-point with 2 decimals.
,.2f 1234.5 1,234.50 Thousands separator + 2 decimals.
% 0.123 12.3% Default 1-decimal percent.
.0% 0.123 12% Zero-decimal percent.
,d 1234567 1,234,567 Integer with thousands.
d 3.6 4 Integer (round-to-nearest).
.3e 1234.5 1.235e+03 Scientific with 3 decimals.
.0s 1234 1k SI prefix (no decimals).
.2s 1234 1.23k SI prefix with 2 decimals.
(empty) 3.14159 3.14159 Default %g rendering.

Supported time directives

Time specs are detected by the presence of any %X directive other than a bare % or the number-percent N%.

Directive Maps to Go layout Renders
%Y 2006 4-digit year
%m 01 2-digit month
%d 02 2-digit day
%H 15 2-digit hour (24h)
%M 04 2-digit minute
%S 05 2-digit second
%L 000 3-digit millisecond
%j 002 3-digit day-of-year
%% % literal %

Examples

Parse(",.2f").Apply(1234.5)       → "1,234.50"
Parse(".0%").Apply(0.123)         → "12%"
Parse("%Y-%m-%d").Apply(t)        → "2026-05-19"
Parse("%H:%M").Apply(t)           → "14:30"

Error handling

A malformed spec returns PRISM_SPEC_011 with the spec string + parse reason in the error context. Empty spec → no-op (default %g).

Out of scope

The full d3-format grammar (fill, align, sign, symbol, width, type 'b', type 'r', etc.) is not ported. Add a specifier branch + test when a real fixture needs it. The 90% subset above covers every test gate in P06.

Documentation

Overview

Package format implements a small subset of the d3-format mini-DSL plus a strftime-style mini-mapping for time formats. Pure stdlib. See README.md for the supported specifier set.

Number specifier grammar (subset):

[,]?[.<int>]?[Nf|N%|%|d|Ne|Ns]

,   thousands separator
.N  precision (digits after decimal, or significant figures for s)
f   fixed-point ("1234.50")
%   percent (multiply by 100, append "%")
d   integer
e   scientific ("1.234e+03")
s   SI prefix ("1k", "1.2M")

Time specifier grammar (subset of strftime):

%Y  4-digit year
%m  2-digit month
%d  2-digit day-of-month
%H  2-digit hour (24h)
%M  2-digit minute
%S  2-digit second
%L  3-digit millisecond
%j  3-digit day-of-year

The discriminator between number/time format is the presence of a `%X` directive that is not a bare "%" or "N%" — i.e. anything containing `%Y`, `%m`, etc. is a time format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NumberSpec

type NumberSpec struct {
	Thousands bool
	Precision int  // -1 = unset
	HasPrec   bool // distinguishes ".0f" from "f"
	Type      byte // 'f' | '%' | 'd' | 'e' | 's'
}

NumberSpec captures the parsed number format.

type Spec

type Spec struct {
	Raw    string
	Number *NumberSpec
	Time   *TimeSpec
}

Spec is a parsed format specifier. Either Number or Time is set, never both.

func MustParse

func MustParse(s string) *Spec

MustParse is Parse + panic; for static fixture builders + tests.

func Parse

func Parse(s string) (*Spec, error)

Parse parses a format specifier. Returns PRISM_SPEC_011 on a malformed spec. An empty spec parses as a no-op number spec (callers fall back to default %g rendering).

func (*Spec) Apply

func (sp *Spec) Apply(v any) string

Apply renders v with the parsed spec. v may be a numeric type, a time.Time, or a string (passed through).

type TimeSpec

type TimeSpec struct {
	Layout string // converted to Go's reference-time layout
}

TimeSpec captures the parsed time format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL