Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .eslintrc.json

This file was deleted.

18 changes: 0 additions & 18 deletions .github/eslint.json

This file was deleted.

30 changes: 0 additions & 30 deletions .github/workflows/node.js.yml

This file was deleted.

25 changes: 25 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Publish

on:
workflow_dispatch: {}
release:
types: [published]

permissions:
id-token: write
contents: read

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-node@v6
with:
node-version: 24
registry-url: 'https://registry.npmjs.org'
cache: yarn
- run: yarn --frozen-lockfile
- run: yarn test
- run: yarn lint
- run: npm publish
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Test

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-node@v6
with:
node-version: 24
cache: yarn
- run: yarn --frozen-lockfile
- run: yarn test
- run: yarn lint
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2010-2021 Mike Bostock
Copyright 2010-2026 Mike Bostock

Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ This module formats numbers for human consumption, with a consistent output allo
## Resources

- [Documentation](https://d3js.org/d3-format)
- [Examples](https://observablehq.com/collection/@d3/d3-format)
- [Examples](https://observablehq.com/@d3/d3-format)
- [Releases](https://github.com/d3/d3-format/releases)
- [Getting help](https://d3js.org/community)
8 changes: 8 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import js from "@eslint/js";
import globals from "globals";
import {defineConfig} from "eslint/config";

export default defineConfig([
{files: ["**/*.{js,ts,jsx,tsx}"], plugins: {js}, extends: ["js/recommended"]},
{files: ["**/*.{js,ts,jsx,tsx}"], languageOptions: {globals: globals.browser}}
]);
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@
"./src/defaultLocale.js"
],
"devDependencies": {
"eslint": "8",
"mocha": "9",
"rollup": "2",
"rollup-plugin-terser": "7"
"@rollup/plugin-terser": "^0.4.4",
"eslint": "^9.39.2",
"globals": "^17.0.0",
"rollup": "^4.55.1",
"vitest": "^4.0.17"
},
"scripts": {
"test": "mocha 'test/**/*-test.js' && eslint src test",
"prepublishOnly": "rm -rf dist && yarn test && rollup -c",
"postpublish": "git push && git push --tags && cd ../d3.github.com && git pull && cp ../${npm_package_name}/dist/${npm_package_name}.js ${npm_package_name}.v${npm_package_version%%.*}.js && cp ../${npm_package_name}/dist/${npm_package_name}.min.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git add ${npm_package_name}.v${npm_package_version%%.*}.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git commit -m \"${npm_package_name} ${npm_package_version}\" && git push && cd -"
"test": "vitest",
"lint": "eslint src test",
"prepublishOnly": "rm -rf dist && rollup -c"
},
"engines": {
"node": ">=12"
Expand Down
6 changes: 3 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {readFileSync} from "fs";
import {terser} from "rollup-plugin-terser";
import * as meta from "./package.json";
import {readFileSync} from "node:fs";
import terser from "@rollup/plugin-terser";
import meta from "./package.json" with {type: "json"};

// Extract copyrights from the LICENSE.
const copyright = readFileSync("./LICENSE", "utf-8")
Expand Down
10 changes: 0 additions & 10 deletions test/.eslintrc.json

This file was deleted.

58 changes: 29 additions & 29 deletions test/arabicLocale-test.js
Original file line number Diff line number Diff line change
@@ -1,115 +1,115 @@
import assert from "assert";
import {readFileSync} from "fs";
import {readFileSync} from "node:fs";
import {assert, test} from "vitest";
import {formatLocale} from "../src/index.js";

function locale(locale) {
return formatLocale(JSON.parse(readFileSync(`./locale/${locale}.json`, "utf8")));
}

it("formatLocale() can format numbers using ar-001 locale", () => {
test("formatLocale() can format numbers using ar-001 locale", () => {
assert.strictEqual(locale("ar-001").format("$,.2f")(-1234.56), "−١٬٢٣٤٫٥٦");
});

it("formatLocale() can format numbers using ar-AE locale", () => {
test("formatLocale() can format numbers using ar-AE locale", () => {
assert.strictEqual(locale("ar-AE").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ د.إ.");
});

it("formatLocale() can format numbers using ar-BH locale", () => {
test("formatLocale() can format numbers using ar-BH locale", () => {
assert.strictEqual(locale("ar-BH").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ د.ب.");
});

it("formatLocale() can format numbers using ar-DJ locale", () => {
test("formatLocale() can format numbers using ar-DJ locale", () => {
assert.strictEqual(locale("ar-DJ").format("$,.2f")(1234.56), "\u200fFdj ١٬٢٣٤٫٥٦");
});

it("formatLocale() can format numbers using ar-DZ locale", () => {
test("formatLocale() can format numbers using ar-DZ locale", () => {
assert.strictEqual(locale("ar-DZ").format("$,.2f")(1234.56), "د.ج. 1.234,56");
});

it("formatLocale() can format numbers using ar-EG locale", () => {
test("formatLocale() can format numbers using ar-EG locale", () => {
assert.strictEqual(locale("ar-EG").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ ج.م.");
});

it("formatLocale() can format numbers using ar-EH locale", () => {
test("formatLocale() can format numbers using ar-EH locale", () => {
assert.strictEqual(locale("ar-EH").format("$,.2f")(1234.56), "د.م. 1,234.56");
});

it("formatLocale() can format numbers using ar-ER locale", () => {
test("formatLocale() can format numbers using ar-ER locale", () => {
assert.strictEqual(locale("ar-ER").format("$,.2f")(1234.56), "Nfk ١٬٢٣٤٫٥٦");
});

it("formatLocale() can format numbers using ar-IL locale", () => {
test("formatLocale() can format numbers using ar-IL locale", () => {
assert.strictEqual(locale("ar-IL").format("$,.2f")(1234.56), "₪ ١٬٢٣٤٫٥٦");
});

it("formatLocale() can format numbers using ar-IQ locale", () => {
test("formatLocale() can format numbers using ar-IQ locale", () => {
assert.strictEqual(locale("ar-IQ").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ د.ع.");
});

it("formatLocale() can format numbers using ar-JO locale", () => {
test("formatLocale() can format numbers using ar-JO locale", () => {
assert.strictEqual(locale("ar-JO").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ د.أ.");
});

it("formatLocale() can format numbers using ar-KM locale", () => {
test("formatLocale() can format numbers using ar-KM locale", () => {
assert.strictEqual(locale("ar-KM").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ ف.ج.ق.");
});

it("formatLocale() can format numbers using ar-KW locale", () => {
test("formatLocale() can format numbers using ar-KW locale", () => {
assert.strictEqual(locale("ar-KW").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ د.ك.");
});

it("formatLocale() can format numbers using ar-LB locale", () => {
test("formatLocale() can format numbers using ar-LB locale", () => {
assert.strictEqual(locale("ar-LB").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ ل.ل.");
});

it("formatLocale() can format numbers using ar-MA locale", () => {
test("formatLocale() can format numbers using ar-MA locale", () => {
assert.strictEqual(locale("ar-MA").format("$,.2f")(1234.56), "د.م. 1.234,56");
});

it("formatLocale() can format numbers using ar-MR locale", () => {
test("formatLocale() can format numbers using ar-MR locale", () => {
assert.strictEqual(locale("ar-MR").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ أ.م.");
});

it("formatLocale() can format numbers using ar-OM locale", () => {
test("formatLocale() can format numbers using ar-OM locale", () => {
assert.strictEqual(locale("ar-OM").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ ر.ع.");
});

it("formatLocale() can format numbers using ar-PS locale", () => {
test("formatLocale() can format numbers using ar-PS locale", () => {
assert.strictEqual(locale("ar-PS").format("$,.2f")(1234.56), "₪ ١٬٢٣٤٫٥٦");
});

it("formatLocale() can format numbers using ar-QA locale", () => {
test("formatLocale() can format numbers using ar-QA locale", () => {
assert.strictEqual(locale("ar-QA").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ ر.ق.");
});

it("formatLocale() can format numbers using ar-SA locale", () => {
test("formatLocale() can format numbers using ar-SA locale", () => {
assert.strictEqual(locale("ar-SA").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ ر.س.");
});

it("formatLocale() can format numbers using ar-SD locale", () => {
test("formatLocale() can format numbers using ar-SD locale", () => {
assert.strictEqual(locale("ar-SD").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ ج.س.");
});

it("formatLocale() can format numbers using ar-SO locale", () => {
test("formatLocale() can format numbers using ar-SO locale", () => {
assert.strictEqual(locale("ar-SO").format("$,.2f")(1234.56), "‏S ١٬٢٣٤٫٥٦");
});

it("formatLocale() can format numbers using ar-SS locale", () => {
test("formatLocale() can format numbers using ar-SS locale", () => {
assert.strictEqual(locale("ar-SS").format("$,.2f")(1234.56), "£ ١٬٢٣٤٫٥٦");
});

it("formatLocale() can format numbers using ar-SY locale", () => {
test("formatLocale() can format numbers using ar-SY locale", () => {
assert.strictEqual(locale("ar-SY").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ ل.س.");
});

it("formatLocale() can format numbers using ar-TD locale", () => {
test("formatLocale() can format numbers using ar-TD locale", () => {
assert.strictEqual(locale("ar-TD").format("$,.2f")(1234.56), "\u200fFCFA ١٬٢٣٤٫٥٦");
});

it("formatLocale() can format numbers using ar-TN locale", () => {
test("formatLocale() can format numbers using ar-TN locale", () => {
assert.strictEqual(locale("ar-TN").format("$,.2f")(1234.56), "د.ت. 1.234,56");
});

it("formatLocale() can format numbers using ar-YE locale", () => {
test("formatLocale() can format numbers using ar-YE locale", () => {
assert.strictEqual(locale("ar-YE").format("$,.2f")(1234.56), "١٬٢٣٤٫٥٦ ر.ى.");
});
8 changes: 4 additions & 4 deletions test/defaultLocale-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from "assert";
import {assert, test} from "vitest";
import {format, formatPrefix, formatDefaultLocale} from "../src/index.js";

const enUs = {
Expand All @@ -16,7 +16,7 @@ const frFr = {
percent: "\u202f%"
};

it("formatDefaultLocale(definition) returns the new default locale", () => {
test("formatDefaultLocale(definition) returns the new default locale", () => {
const locale = formatDefaultLocale(frFr);
try {
assert.strictEqual(locale.format("$,.2f")(12345678.90), "12.345.678,90 €");
Expand All @@ -26,7 +26,7 @@ it("formatDefaultLocale(definition) returns the new default locale", () => {
}
});

it("formatDefaultLocale(definition) affects format", () => {
test("formatDefaultLocale(definition) affects format", () => {
const locale = formatDefaultLocale(frFr);
try {
assert.strictEqual(format, locale.format);
Expand All @@ -36,7 +36,7 @@ it("formatDefaultLocale(definition) affects format", () => {
}
});

it("formatDefaultLocale(definition) affects formatPrefix", () => {
test("formatDefaultLocale(definition) affects formatPrefix", () => {
const locale = formatDefaultLocale(frFr);
try {
assert.strictEqual(formatPrefix, locale.formatPrefix);
Expand Down
16 changes: 8 additions & 8 deletions test/format-test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import assert from "assert";
import {assert, test} from "vitest";
import {format} from "../src/index.js";

it("format(specifier)(number) returns a string", () => {
test("format(specifier)(number) returns a string", () => {
assert.strictEqual(typeof format("d")(0), "string");
});

it("format(specifier).toString() returns the normalized specifier", () => {
test("format(specifier).toString() returns the normalized specifier", () => {
assert.strictEqual(format("d") + "", " >-d");
});

it("format(specifier) throws an error for invalid formats", () => {
test("format(specifier) throws an error for invalid formats", () => {
assert.throws(() => { format("foo"); }, /invalid format: foo/);
assert.throws(() => { format(".-2s"); }, /invalid format: \.-2s/);
assert.throws(() => { format(".f"); }, /invalid format: \.f/);
});

it("format(\",.\") unreasonable precision values are clamped to reasonable values", () => {
test("format(\",.\") unreasonable precision values are clamped to reasonable values", () => {
assert.strictEqual(format(".30f")(0), "0.00000000000000000000");
assert.strictEqual(format(".0g")(1), "1");
});

it("format(\"s\") handles very small and very large values", () => {
test("format(\"s\") handles very small and very large values", () => {
assert.strictEqual(format("s")(Number.MIN_VALUE), "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005y");
assert.strictEqual(format("s")(Number.MAX_VALUE), "179769000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000Y");
});

it("format(\"n\") is equivalent to format(\",g\")", () => {
test("format(\"n\") is equivalent to format(\",g\")", () => {
assert.strictEqual(format("n")(123456.78), "123,457");
assert.strictEqual(format(",g")(123456.78), "123,457");
});

it("format(\"012\") is equivalent to format(\"0=12\")", () => {
test("format(\"012\") is equivalent to format(\"0=12\")", () => {
assert.strictEqual(format("012")(123.456), "00000123.456");
assert.strictEqual(format("0=12")(123.456), "00000123.456");
});
Loading