Functional Weave
Code in Python

charts.format@1.0.0

vectors.json

6,862 bytes · view raw

[
  { "fn": "formatTick", "name": "whole-number step, grouped thousands", "args": [1000, 200], "expect": "1,000" },
  { "fn": "formatTick", "name": "a half step gives every label one place, zero included", "args": [0, 0.5], "expect": "0.0" },
  { "fn": "formatTick", "name": "a half step", "args": [2.5, 0.5], "expect": "2.5" },
  { "fn": "formatTick", "name": "a quarter step needs two places", "args": [1.5, 0.25], "expect": "1.50" },
  { "fn": "formatTick", "name": "binary noise is not printed (String gives 0.30000000000000004)", "args": [0.30000000000000004, 0.1], "expect": "0.3" },
  { "fn": "formatTick", "name": "a negative tick", "args": [-2000, 500], "expect": "-2,000" },
  { "fn": "formatTick", "name": "millions", "args": [1234567, 1000000], "expect": "1,234,567" },
  { "fn": "formatTick", "name": "a tiny step is written out, never 1e-5", "args": [0.00003, 0.00001], "expect": "0.00003" },
  { "fn": "formatTick", "name": "a step of two tenths needs one place", "args": [0.7, 0.2], "expect": "0.7" },
  { "fn": "formatTick", "name": "a descending axis has a negative step", "args": [5, -1], "expect": "5" },
  { "fn": "formatTick", "name": "a zero step (one tick) uses the value's own places", "args": [12.5, 0], "expect": "12.5" },
  { "fn": "formatTick", "name": "a value too large to write exactly is an error", "args": [1e15, 0.5], "expectError": "value is too large to format" },
  { "fn": "formatSi", "name": "thousands", "args": [1200, 2], "expect": "1.2k" },
  { "fn": "formatSi", "name": "millions", "args": [3400000, 2], "expect": "3.4M" },
  { "fn": "formatSi", "name": "trailing zeros are dropped", "args": [1000, 3], "expect": "1k" },
  { "fn": "formatSi", "name": "rounding up into the next prefix gives 1k, not 1000", "args": [999.96, 3], "expect": "1k" },
  { "fn": "formatSi", "name": "one significant digit rounds up into the next prefix too", "args": [950, 1], "expect": "1k" },
  { "fn": "formatSi", "name": "milli", "args": [0.0015, 2], "expect": "1.5m" },
  { "fn": "formatSi", "name": "micro uses the micro sign", "args": [0.000001234, 3], "expect": "1.23µ" },
  { "fn": "formatSi", "name": "micro, whole", "args": [0.000025, 2], "expect": "25µ" },
  { "fn": "formatSi", "name": "a half is 500m, as d3 writes it", "args": [0.5, 2], "expect": "500m" },
  { "fn": "formatSi", "name": "negative", "args": [-45600, 3], "expect": "-45.6k" },
  { "fn": "formatSi", "name": "fewer digits than the whole part rounds to tens", "args": [12, 1], "expect": "10" },
  { "fn": "formatSi", "name": "and to hundreds, half away from zero", "args": [150, 1], "expect": "200" },
  { "fn": "formatSi", "name": "four significant digits of a large number", "args": [123456789, 4], "expect": "123.5M" },
  { "fn": "formatSi", "name": "yotta is the largest prefix", "args": [5e25, 2], "expect": "50Y" },
  { "fn": "formatSi", "name": "no prefix between 1 and 999", "args": [42, 2], "expect": "42" },
  { "fn": "formatSi", "name": "zero", "args": [0, 3], "expect": "0" },
  { "fn": "formatSi", "name": "zero significant digits is an error", "args": [1200, 0], "expectError": "significant must be a whole number from 1 to 15" },
  { "fn": "formatSi", "name": "sixteen significant digits is an error", "args": [1200, 16], "expectError": "significant must be a whole number from 1 to 15" },
  { "fn": "formatPercent", "name": "one place", "args": [0.123, 1], "expect": "12.3%" },
  { "fn": "formatPercent", "name": "0.145 is 15%: 0.145 * 100 is 14.4999..., which a plain round makes 14%", "args": [0.145, 0], "expect": "15%" },
  { "fn": "formatPercent", "name": "0.07 * 100 is 7.000000000000001, printed 7%", "args": [0.07, 0], "expect": "7%" },
  { "fn": "formatPercent", "name": "all of it", "args": [1, 0], "expect": "100%" },
  { "fn": "formatPercent", "name": "fixed places are kept", "args": [0.5, 2], "expect": "50.00%" },
  { "fn": "formatPercent", "name": "negative", "args": [-0.05, 1], "expect": "-5.0%" },
  { "fn": "formatPercent", "name": "zero", "args": [0, 0], "expect": "0%" },
  { "fn": "formatPercent", "name": "more than 100%, no grouping", "args": [12.5, 0], "expect": "1250%" },
  { "fn": "formatPercent", "name": "two places of a four-place fraction", "args": [1.2345, 2], "expect": "123.45%" },
  { "fn": "formatPercent", "name": "a tiny share rounds up to the last place", "args": [0.00005, 2], "expect": "0.01%" },
  { "fn": "formatPercent", "name": "more than 12 places is an error", "args": [0.5, 13], "expectError": "decimals must be a whole number from 0 to 12" },
  { "fn": "formatDate", "name": "a day tick", "args": ["2026-09-23", "day"], "expect": "23 Sep" },
  { "fn": "formatDate", "name": "no leading zero on the day", "args": ["2026-09-03", "day"], "expect": "3 Sep" },
  { "fn": "formatDate", "name": "a week tick reads like a day", "args": ["2026-09-21", "week"], "expect": "21 Sep" },
  { "fn": "formatDate", "name": "a month tick", "args": ["2026-09-01", "month"], "expect": "Sep 2026" },
  { "fn": "formatDate", "name": "a quarter tick", "args": ["2026-07-01", "quarter"], "expect": "Q3 2026" },
  { "fn": "formatDate", "name": "the first day of the year is Q1", "args": ["2026-01-01", "quarter"], "expect": "Q1 2026" },
  { "fn": "formatDate", "name": "the last day of the year is Q4", "args": ["2026-12-31", "quarter"], "expect": "Q4 2026" },
  { "fn": "formatDate", "name": "a year tick", "args": ["2026-01-01", "year"], "expect": "2026" },
  { "fn": "formatDate", "name": "a leap day", "args": ["2024-02-29", "day"], "expect": "29 Feb" },
  { "fn": "formatDate", "name": "a day that never existed is an error", "args": ["2026-02-30", "day"], "expectError": "is not a real calendar date" },
  { "fn": "formatDate", "name": "an unknown label is an error", "args": ["2026-09-23", "hour"], "expectError": "unknown date label" },
  { "fn": "formatMoneyTick", "name": "pounds from pence", "args": [50000, "GBP"], "expect": "£500.00" },
  { "fn": "formatMoneyTick", "name": "a tick between pence rounds half away from zero", "args": [123456.5, "GBP"], "expect": "£1,234.57" },
  { "fn": "formatMoneyTick", "name": "a negative half rounds away from zero too", "args": [-0.5, "GBP"], "expect": "-£0.01" },
  { "fn": "formatMoneyTick", "name": "euros below zero", "args": [-2500, "EUR"], "expect": "-€25.00" },
  { "fn": "formatMoneyTick", "name": "yen have no minor unit", "args": [1500, "JPY"], "expect": "¥1,500" },
  { "fn": "formatMoneyTick", "name": "three-digit currency", "args": [1234, "KWD"], "expect": "KD 1.234" },
  { "fn": "formatMoneyTick", "name": "zero", "args": [0, "GBP"], "expect": "£0.00" },
  { "fn": "formatMoneyTick", "name": "an unknown currency is an error", "args": [100, "XYZ"], "expectError": "no formatting rules" },
  { "fn": "formatMoneyTick", "name": "a lower-case code is an error", "args": [100, "gbp"], "expectError": "is not an uppercase ISO 4217 currency code" }
]