Skip to content

Commit

Permalink
feat: add more rounding options
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperorb committed Mar 12, 2024
1 parent 8421fac commit c7c1e6c
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 56 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"svelte-highlight": "^7.5.0",
"svelte-preprocess": "^5.1.3",
"tslib": "^2.6.2",
"typescript": "^5.3.3",
"typescript": "^5.4.2",
"vite": "^5.0.12",
"vitest": "^1.2.2"
}
Expand Down
86 changes: 43 additions & 43 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,52 @@ declare namespace Intl {
prototype: DurationFormat;
new(locales?: LocalesArgument, options?: DurationFormatOptions): DurationFormat;
};

// Borrowing from: https://github.com/microsoft/TypeScript/blob/e66049084b40ce85e28278c29cdd9784c7a9033a/src/lib/es2023.intl.d.ts
interface NumberFormatOptionsUseGroupingRegistry {
min2: never;
auto: never;
always: never;
}

interface NumberFormatOptionsSignDisplayRegistry {
negative: never;
}

interface NumberFormatOptions {
roundingPriority?: "auto" | "morePrecision" | "lessPrecision" | undefined;
roundingIncrement?: 1 | 2 | 5 | 10 | 20 | 25 | 50 | 100 | 200 | 250 | 500 | 1000 | 2000 | 2500 | 5000 | undefined;
roundingMode?: "ceil" | "floor" | "expand" | "trunc" | "halfCeil" | "halfFloor" | "halfExpand" | "halfTrunc" | "halfEven" | undefined;
trailingZeroDisplay?: "auto" | "stripIfInteger" | undefined;
}

interface ResolvedNumberFormatOptions {
roundingPriority: "auto" | "morePrecision" | "lessPrecision";
roundingMode: "ceil" | "floor" | "expand" | "trunc" | "halfCeil" | "halfFloor" | "halfExpand" | "halfTrunc" | "halfEven";
roundingIncrement: 1 | 2 | 5 | 10 | 20 | 25 | 50 | 100 | 200 | 250 | 500 | 1000 | 2000 | 2500 | 5000;
trailingZeroDisplay: "auto" | "stripIfInteger";
}

interface NumberRangeFormatPart extends NumberFormatPart {
source: "startRange" | "endRange" | "shared";
}

interface NumberFormat {
formatRange(start: number | bigint, end: number | bigint): string;
formatRangeToParts(start: number | bigint, end: number | bigint): NumberRangeFormatPart[];
}

interface PluralRulesOptions {
roundingPriority?: "auto" | "morePrecision" | "lessPrecision" | undefined;
roundingIncrement?: 1 | 2 | 5 | 10 | 20 | 25 | 50 | 100 | 200 | 250 | 500 | 1000 | 2000 | 2500 | 5000 | undefined;
roundingMode?: "ceil" | "floor" | "expand" | "trunc" | "halfCeil" | "halfFloor" | "halfExpand" | "halfTrunc" | "halfEven" | undefined;
trailingZeroDisplay?: "auto" | "stripIfInteger" | undefined;
}

interface ResolvedPluralRulesOptions {
roundingPriority: "auto" | "morePrecision" | "lessPrecision";
roundingMode: "ceil" | "floor" | "expand" | "trunc" | "halfCeil" | "halfFloor" | "halfExpand" | "halfTrunc" | "halfEven";
roundingIncrement: 1 | 2 | 5 | 10 | 20 | 25 | 50 | 100 | 200 | 250 | 500 | 1000 | 2000 | 2500 | 5000;
trailingZeroDisplay: "auto" | "stripIfInteger";
}
}
3 changes: 2 additions & 1 deletion src/lib/components/ui/Select.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
export let fullWidth: boolean | undefined = undefined;
export let removeEmpty: boolean | undefined = undefined;
export let onChange: ((event: Event) => void) | undefined = undefined;
const getValue = (value: Type) => (value as string | number).toString();
</script>

<div>
Expand All @@ -30,7 +31,7 @@
{/if}
{#each items as [key, value]}
{#if value !== undefined}
<option value={key}>{value}</option>
<option value={getValue(key)}>{value}</option>
{/if}
{/each}
</select>
Expand Down
4 changes: 4 additions & 0 deletions src/lib/format-options/common.options.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export const style = [undefined, 'long', 'short', 'narrow'] as const;
export const localeMatcher = [undefined, 'best fit', 'lookup'] as const;
export const roundingPriority = ["auto", "morePrecision", "lessPrecision", undefined] as const;
export const roundingIncrement = [1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000, undefined] as const
export const roundingMode = ["ceil", "floor", "expand", "trunc", "halfCeil", "halfFloor", "halfExpand", "halfTrunc", "halfEven", undefined] as const;
export const trailingZeroDisplay = ["auto", "stripIfInteger", undefined] as const;
6 changes: 5 additions & 1 deletion src/lib/format-options/number-format.options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { currencies } from '$lib/locale-data/currencies';
import { units } from '$lib/locale-data/units';
import { defaultNumberRange } from '$lib/utils/format-utils';
import { localeMatcher, style } from './common.options';
import { localeMatcher, roundingIncrement, roundingMode, roundingPriority, trailingZeroDisplay, style } from './common.options';

export const numberFormatOptionsCommon = {
style: ['currency', 'unit'],
Expand All @@ -12,6 +12,10 @@ export const numberFormatOptionsCommon = {
maximumFractionDigits: [...defaultNumberRange, undefined],
minimumSignificantDigits: [...defaultNumberRange, undefined],
maximumSignificantDigits: [...defaultNumberRange, undefined],
roundingIncrement,
roundingMode,
roundingPriority,
trailingZeroDisplay,
localeMatcher
} as const;

Expand Down
Loading

0 comments on commit c7c1e6c

Please sign in to comment.