Skip to content

Commit

Permalink
Add romanize() string util and ScrollingValue example
Browse files Browse the repository at this point in the history
  • Loading branch information
techniq committed Nov 15, 2023
1 parent 8fc9472 commit ebd429e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-bugs-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-ux': patch
---

Add `romanize()` string util and ScrollingValue example
29 changes: 29 additions & 0 deletions packages/svelte-ux/src/lib/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,32 @@ export function truncate(text: string, totalChars: number, endChars: number = 0)
return text;
}
}

export function romanize(value: number) {
const lookup = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
XC: 90,
L: 50,
XL: 40,
X: 10,
IX: 9,
V: 5,
IV: 4,
I: 1,
};

let result = '';
let i;
for (i in lookup) {
while (value >= lookup[i]) {
result += i;
value -= lookup[i];
}
}

return result;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Field from '$lib/components/Field.svelte';
import { mdiMinus, mdiPlus } from '@mdi/js';
import ButtonGroup from '$lib/components/ButtonGroup.svelte';
import { timerStore } from '$lib';
import { romanize, timerStore } from '$lib';
let value = 0;
Expand Down Expand Up @@ -75,7 +75,17 @@
{/each}
</Preview>

<h2>Formatted</h2>
<h2>Formatted (roman numerals)</h2>

<Preview>
<ScrollingValue
bind:value
format={(value) => romanize(Math.abs(value)) || 0}
class="text-3xl tabular-nums"
/>
</Preview>

<h2>Formatted (list)</h2>

<Preview>
<span class="text-3xl">
Expand Down

1 comment on commit ebd429e

@vercel
Copy link

@vercel vercel bot commented on ebd429e Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.