diff --git a/package.json b/package.json index 10206fc..1945c77 100644 --- a/package.json +++ b/package.json @@ -40,12 +40,12 @@ }, "peerDependencies": { "@sveltejs/kit": "^2.20.2", - "svelte": "^5.0.0", - "tailwindcss": "^4.1.11", - "tailwindcss-animate": "^1.0.7", "@tailwindcss/container-queries": "^0.1.1", "@tailwindcss/forms": "^0.5.10", - "@tailwindcss/vite": "^4.1.11" + "@tailwindcss/vite": "^4.1.11", + "svelte": "^5.0.0", + "tailwindcss": "^4.1.11", + "tailwindcss-animate": "^1.0.7" }, "dependencies": { "@internationalized/date": "^3.8.2", @@ -56,7 +56,8 @@ "match-sorter": "^8.0.0", "melt": "^0.37.0", "moment": "^2.30.1", - "phosphor-svelte": "^3.0.1" + "phosphor-svelte": "^3.0.1", + "tinyduration": "^3.4.1" }, "devDependencies": { "@eslint/compat": "^1.2.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2488323..929f85b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -59,6 +59,9 @@ importers: tailwindcss-animate: specifier: ^1.0.7 version: 1.0.7(tailwindcss@4.1.11) + tinyduration: + specifier: ^3.4.1 + version: 3.4.1 devDependencies: '@eslint/compat': specifier: ^1.2.5 @@ -1539,6 +1542,9 @@ packages: resolution: {integrity: sha512-5S7Va8hKfV7W5U6g3aYxXmlPoZVAwUMy9AOKyF2fVuZa2UD3qZjg578OrLRt8PcNN1PleVaL/5/yYATNL0ICUw==} engines: {node: '>=18'} + tinyduration@3.4.1: + resolution: {integrity: sha512-NemFoamVYn7TmtwZKZ3OiliM9fZkr6EWiTM+wKknco6POSy2gS689xx/pXip0JYp40HXpUw6k65CUYHWYUXdaA==} + tinyglobby@0.2.14: resolution: {integrity: sha512-tX5e7OM1HnYr2+a2C/4V0htOcSQcoSTH9KgJnVvNm5zm/cyEWKJ7j7YutsH9CxMdtOkkLFy2AHrMci9IM8IPZQ==} engines: {node: '>=12.0.0'} @@ -2917,6 +2923,8 @@ snapshots: mkdirp: 3.0.1 yallist: 5.0.0 + tinyduration@3.4.1: {} + tinyglobby@0.2.14: dependencies: fdir: 6.4.6(picomatch@4.0.3) diff --git a/src/lib/DurationInput.svelte b/src/lib/DurationInput.svelte index 6533faf..be1523d 100644 --- a/src/lib/DurationInput.svelte +++ b/src/lib/DurationInput.svelte @@ -26,12 +26,7 @@ * @returns The ISO 8601 duration string. */ export const durationToISO8601 = (duration: TimeDuration): string => { - let str = 'P'; - if (duration.hours || duration.minutes || duration.seconds) str += 'T'; - if (duration.hours) str += `${duration.hours}H`; - if (duration.minutes) str += `${duration.minutes}M`; - if (duration.seconds) str += `${duration.seconds}S`; - return str; + return serialize(duration); }; /** @@ -40,16 +35,7 @@ * @returns The TimeDuration object. */ export const iso8601ToDuration = (str: string): TimeDuration => { - const regex = /^P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/; - const matches = str.match(regex); - if (!matches) { - throw new Error('Invalid ISO 8601 duration string'); - } - return { - hours: matches[2] ? parseInt(matches[2], 10) : 0, - minutes: matches[3] ? parseInt(matches[3], 10) : 0, - seconds: matches[4] ? parseInt(matches[4], 10) : 0 - }; + return parse(str); }; @@ -62,6 +48,7 @@ import Label from './Label.svelte'; import { liveValidator, validate } from '@svelte-toolkit/validate'; import StyledRawInput from './StyledRawInput.svelte'; + import { parse, serialize } from 'tinyduration'; interface Props { /**