@olenbetong/appframe-ds 1.7.1 → 1.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,79 +1,79 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import { clamp, formatNumber, getDefaultCulture, parseNumber, roundTo } from "./numberFormat.js";
4
-
5
- describe("formatNumber", () => {
6
- it("returns null for nothing", () => {
7
- expect(formatNumber(null)).toBeNull();
8
- expect(formatNumber(undefined)).toBeNull();
9
- expect(formatNumber(Number.NaN)).toBeNull();
10
- });
11
-
12
- it("falls back to nb-NO when there is no Appframe session", () => {
13
- expect(getDefaultCulture()).toBe("nb-NO");
14
- expect(formatNumber(8)).toBe("8,00");
15
- });
16
-
17
- it("follows the culture given", () => {
18
- expect(formatNumber(8, { culture: "en-GB" })).toBe("8.00");
19
- expect(formatNumber(8, { culture: "de-DE" })).toBe("8,00");
20
- });
21
-
22
- it("pads and rounds to the fraction digits", () => {
23
- expect(formatNumber(1.005, { decimalDigits: 0 })).toBe("1");
24
- expect(formatNumber(1.5, { decimalDigits: 3 })).toBe("1,500");
25
- });
26
-
27
- it("leaves out thousands separators unless asked for them", () => {
28
- expect(formatNumber(1234.5)).toBe("1234,50");
29
- expect(formatNumber(1234.5, { culture: "en-GB" })).toBe("1234.50");
30
- expect(formatNumber(1234.5, { culture: "en-GB", grouping: true })).toBe("1,234.50");
31
- // nb-NO groups with a (narrow no-break) space
32
- expect(formatNumber(1234.5, { grouping: true })).toMatch(/^1\s234,50$/);
33
- });
34
-
35
- it("round trips through parseNumber in any culture", () => {
36
- expect(parseNumber(formatNumber(1234.5))).toBe(1234.5);
37
- expect(parseNumber(formatNumber(1234.5, { culture: "en-GB" }))).toBe(1234.5);
38
- });
39
- });
40
-
41
- describe("parseNumber", () => {
42
- it("returns null for empty text", () => {
43
- expect(parseNumber("")).toBeNull();
44
- expect(parseNumber(" ")).toBeNull();
45
- expect(parseNumber(null)).toBeNull();
46
- expect(parseNumber(undefined)).toBeNull();
47
- });
48
-
49
- it("reads both comma and period as the decimal separator", () => {
50
- expect(parseNumber("1,5")).toBe(1.5);
51
- expect(parseNumber("1.5")).toBe(1.5);
52
- expect(parseNumber("1,")).toBe(1);
53
- expect(parseNumber(".5")).toBe(0.5);
54
- });
55
-
56
- it("accepts the typographic minus and ignores whitespace", () => {
57
- expect(parseNumber("−2")).toBe(-2);
58
- expect(parseNumber(" 1 234 ")).toBe(1234);
59
- });
60
-
61
- it("returns NaN for text that is not a number yet", () => {
62
- expect(parseNumber("-")).toBeNaN();
63
- expect(parseNumber("abc")).toBeNaN();
64
- });
65
- });
66
-
67
- describe("roundTo and clamp", () => {
68
- it("rounds away floating point noise", () => {
69
- expect(roundTo(0.1 + 0.2, 2)).toBe(0.3);
70
- expect(roundTo(2.675, 0)).toBe(3);
71
- });
72
-
73
- it("clamps only against the bounds given", () => {
74
- expect(clamp(5)).toBe(5);
75
- expect(clamp(5, 0, 3)).toBe(3);
76
- expect(clamp(-1, 0)).toBe(0);
77
- expect(clamp(2, undefined, 1)).toBe(1);
78
- });
79
- });
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { clamp, formatNumber, getDefaultCulture, parseNumber, roundTo } from "./numberFormat.js";
4
+
5
+ describe("formatNumber", () => {
6
+ it("returns null for nothing", () => {
7
+ expect(formatNumber(null)).toBeNull();
8
+ expect(formatNumber(undefined)).toBeNull();
9
+ expect(formatNumber(Number.NaN)).toBeNull();
10
+ });
11
+
12
+ it("falls back to nb-NO when there is no Appframe session", () => {
13
+ expect(getDefaultCulture()).toBe("nb-NO");
14
+ expect(formatNumber(8)).toBe("8,00");
15
+ });
16
+
17
+ it("follows the culture given", () => {
18
+ expect(formatNumber(8, { culture: "en-GB" })).toBe("8.00");
19
+ expect(formatNumber(8, { culture: "de-DE" })).toBe("8,00");
20
+ });
21
+
22
+ it("pads and rounds to the fraction digits", () => {
23
+ expect(formatNumber(1.005, { decimalDigits: 0 })).toBe("1");
24
+ expect(formatNumber(1.5, { decimalDigits: 3 })).toBe("1,500");
25
+ });
26
+
27
+ it("leaves out thousands separators unless asked for them", () => {
28
+ expect(formatNumber(1234.5)).toBe("1234,50");
29
+ expect(formatNumber(1234.5, { culture: "en-GB" })).toBe("1234.50");
30
+ expect(formatNumber(1234.5, { culture: "en-GB", grouping: true })).toBe("1,234.50");
31
+ // nb-NO groups with a (narrow no-break) space
32
+ expect(formatNumber(1234.5, { grouping: true })).toMatch(/^1\s234,50$/);
33
+ });
34
+
35
+ it("round trips through parseNumber in any culture", () => {
36
+ expect(parseNumber(formatNumber(1234.5))).toBe(1234.5);
37
+ expect(parseNumber(formatNumber(1234.5, { culture: "en-GB" }))).toBe(1234.5);
38
+ });
39
+ });
40
+
41
+ describe("parseNumber", () => {
42
+ it("returns null for empty text", () => {
43
+ expect(parseNumber("")).toBeNull();
44
+ expect(parseNumber(" ")).toBeNull();
45
+ expect(parseNumber(null)).toBeNull();
46
+ expect(parseNumber(undefined)).toBeNull();
47
+ });
48
+
49
+ it("reads both comma and period as the decimal separator", () => {
50
+ expect(parseNumber("1,5")).toBe(1.5);
51
+ expect(parseNumber("1.5")).toBe(1.5);
52
+ expect(parseNumber("1,")).toBe(1);
53
+ expect(parseNumber(".5")).toBe(0.5);
54
+ });
55
+
56
+ it("accepts the typographic minus and ignores whitespace", () => {
57
+ expect(parseNumber("−2")).toBe(-2);
58
+ expect(parseNumber(" 1 234 ")).toBe(1234);
59
+ });
60
+
61
+ it("returns NaN for text that is not a number yet", () => {
62
+ expect(parseNumber("-")).toBeNaN();
63
+ expect(parseNumber("abc")).toBeNaN();
64
+ });
65
+ });
66
+
67
+ describe("roundTo and clamp", () => {
68
+ it("rounds away floating point noise", () => {
69
+ expect(roundTo(0.1 + 0.2, 2)).toBe(0.3);
70
+ expect(roundTo(2.675, 0)).toBe(3);
71
+ });
72
+
73
+ it("clamps only against the bounds given", () => {
74
+ expect(clamp(5)).toBe(5);
75
+ expect(clamp(5, 0, 3)).toBe(3);
76
+ expect(clamp(-1, 0)).toBe(0);
77
+ expect(clamp(2, undefined, 1)).toBe(1);
78
+ });
79
+ });
@@ -1,94 +1,94 @@
1
- /*
2
- * Culture-aware number formatting and parsing for inputs that edit a number as
3
- * text. Formatting follows the user's culture, read from the Appframe session
4
- * when the function runs so a page that sets it after this module loads still
5
- * formats correctly. Parsing is deliberately culture-agnostic: a Norwegian user
6
- * typing `1.5` means the same as `1,5`.
7
- */
8
-
9
- export const DEFAULT_CULTURE = "nb-NO";
10
-
11
- export type FormatNumberOptions = {
12
- /**
13
- * Number of fraction digits, used as both minimum and maximum so `8` is
14
- * shown as `8,00`. Defaults to 2.
15
- */
16
- decimalDigits?: number;
17
- /**
18
- * Whether to use thousands separators. Off by default, since the result
19
- * is often fed back into `parseNumber`, which does not understand them.
20
- */
21
- grouping?: boolean;
22
- /**
23
- * BCP 47 culture to format in. Defaults to the Appframe session's culture,
24
- * and to `nb-NO` when there is no session (tests, Storybook).
25
- */
26
- culture?: string;
27
- };
28
-
29
- const formatters = new Map<string, Intl.NumberFormat>();
30
-
31
- /**
32
- * The culture numbers are formatted in when none is given: the Appframe
33
- * session's, or `nb-NO` outside a session.
34
- */
35
- export function getDefaultCulture(): string {
36
- return globalThis.af?.userSession?.culture ?? DEFAULT_CULTURE;
37
- }
38
-
39
- /**
40
- * Formats a number with a fixed number of fraction digits in the user's culture.
41
- * Returns `null` for `null`/`undefined` so the result can be fed straight into an input value.
42
- */
43
- export function formatNumber(value: number | null | undefined, options: FormatNumberOptions = {}): string | null {
44
- if (value == null || Number.isNaN(value)) return null;
45
-
46
- let decimalDigits = options.decimalDigits ?? 2;
47
- let grouping = options.grouping ?? false;
48
- let culture = options.culture ?? getDefaultCulture();
49
- let key = `${culture}|${decimalDigits}|${grouping}`;
50
- let formatter = formatters.get(key);
51
-
52
- if (!formatter) {
53
- formatter = new Intl.NumberFormat(culture, {
54
- minimumFractionDigits: decimalDigits,
55
- maximumFractionDigits: decimalDigits,
56
- useGrouping: grouping,
57
- });
58
- formatters.set(key, formatter);
59
- }
60
-
61
- return formatter.format(value);
62
- }
63
-
64
- /**
65
- * Parses text typed by a user into a number, whatever their culture. Both `,`
66
- * and `.` are always read as the decimal separator (never as thousands
67
- * separators), whitespace is the only thousands separator understood
68
- * (`1 234,5`, including the no-break spaces `Intl` groups with), and the
69
- * typographic minus is accepted.
70
- *
71
- * Returns `null` for an empty string, and `NaN` for text that is not (yet) a
72
- * number, such as a lone `-`.
73
- */
74
- export function parseNumber(text: string | null | undefined): number | null {
75
- if (text == null) return null;
76
-
77
- let normalized = text.replace(/\s/g, "").replace("−", "-").replace(",", ".");
78
- if (normalized === "") return null;
79
-
80
- return Number(normalized);
81
- }
82
-
83
- /**
84
- * Rounds to a number of fraction digits, avoiding the floating point noise of
85
- * repeated additions (`0.1 + 0.2`).
86
- */
87
- export function roundTo(value: number, decimalDigits: number): number {
88
- let factor = 10 ** decimalDigits;
89
- return Math.round(value * factor) / factor;
90
- }
91
-
92
- export function clamp(value: number, min = Number.NEGATIVE_INFINITY, max = Number.POSITIVE_INFINITY): number {
93
- return Math.min(Math.max(value, min), max);
94
- }
1
+ /*
2
+ * Culture-aware number formatting and parsing for inputs that edit a number as
3
+ * text. Formatting follows the user's culture, read from the Appframe session
4
+ * when the function runs so a page that sets it after this module loads still
5
+ * formats correctly. Parsing is deliberately culture-agnostic: a Norwegian user
6
+ * typing `1.5` means the same as `1,5`.
7
+ */
8
+
9
+ export const DEFAULT_CULTURE = "nb-NO";
10
+
11
+ export type FormatNumberOptions = {
12
+ /**
13
+ * Number of fraction digits, used as both minimum and maximum so `8` is
14
+ * shown as `8,00`. Defaults to 2.
15
+ */
16
+ decimalDigits?: number;
17
+ /**
18
+ * Whether to use thousands separators. Off by default, since the result
19
+ * is often fed back into `parseNumber`, which does not understand them.
20
+ */
21
+ grouping?: boolean;
22
+ /**
23
+ * BCP 47 culture to format in. Defaults to the Appframe session's culture,
24
+ * and to `nb-NO` when there is no session (tests, Storybook).
25
+ */
26
+ culture?: string;
27
+ };
28
+
29
+ const formatters = new Map<string, Intl.NumberFormat>();
30
+
31
+ /**
32
+ * The culture numbers are formatted in when none is given: the Appframe
33
+ * session's, or `nb-NO` outside a session.
34
+ */
35
+ export function getDefaultCulture(): string {
36
+ return globalThis.af?.userSession?.culture ?? DEFAULT_CULTURE;
37
+ }
38
+
39
+ /**
40
+ * Formats a number with a fixed number of fraction digits in the user's culture.
41
+ * Returns `null` for `null`/`undefined` so the result can be fed straight into an input value.
42
+ */
43
+ export function formatNumber(value: number | null | undefined, options: FormatNumberOptions = {}): string | null {
44
+ if (value == null || Number.isNaN(value)) return null;
45
+
46
+ let decimalDigits = options.decimalDigits ?? 2;
47
+ let grouping = options.grouping ?? false;
48
+ let culture = options.culture ?? getDefaultCulture();
49
+ let key = `${culture}|${decimalDigits}|${grouping}`;
50
+ let formatter = formatters.get(key);
51
+
52
+ if (!formatter) {
53
+ formatter = new Intl.NumberFormat(culture, {
54
+ minimumFractionDigits: decimalDigits,
55
+ maximumFractionDigits: decimalDigits,
56
+ useGrouping: grouping,
57
+ });
58
+ formatters.set(key, formatter);
59
+ }
60
+
61
+ return formatter.format(value);
62
+ }
63
+
64
+ /**
65
+ * Parses text typed by a user into a number, whatever their culture. Both `,`
66
+ * and `.` are always read as the decimal separator (never as thousands
67
+ * separators), whitespace is the only thousands separator understood
68
+ * (`1 234,5`, including the no-break spaces `Intl` groups with), and the
69
+ * typographic minus is accepted.
70
+ *
71
+ * Returns `null` for an empty string, and `NaN` for text that is not (yet) a
72
+ * number, such as a lone `-`.
73
+ */
74
+ export function parseNumber(text: string | null | undefined): number | null {
75
+ if (text == null) return null;
76
+
77
+ let normalized = text.replace(/\s/g, "").replace("−", "-").replace(",", ".");
78
+ if (normalized === "") return null;
79
+
80
+ return Number(normalized);
81
+ }
82
+
83
+ /**
84
+ * Rounds to a number of fraction digits, avoiding the floating point noise of
85
+ * repeated additions (`0.1 + 0.2`).
86
+ */
87
+ export function roundTo(value: number, decimalDigits: number): number {
88
+ let factor = 10 ** decimalDigits;
89
+ return Math.round(value * factor) / factor;
90
+ }
91
+
92
+ export function clamp(value: number, min = Number.NEGATIVE_INFINITY, max = Number.POSITIVE_INFINITY): number {
93
+ return Math.min(Math.max(value, min), max);
94
+ }