sveltekit-superforms
Advanced tools
Comparing version 0.5.12 to 0.5.13
import { type Writable } from 'svelte/store'; | ||
type DefaultOptions = { | ||
trueStringValue: string; | ||
dateFormat: 'date-local' | 'datetime-local' | 'time-local' | 'iso'; | ||
}; | ||
export declare function intProxy<T extends Record<string, unknown>, K extends keyof T>(form: Writable<T>, field: K): T[K] extends number ? Writable<string> : never; | ||
export declare function booleanProxy<T extends Record<string, unknown>>(form: Writable<T>, field: keyof T): Writable<string>; | ||
export declare function booleanProxy<T extends Record<string, unknown>>(form: Writable<T>, field: keyof T, options?: Pick<DefaultOptions, 'trueStringValue'>): Writable<string>; | ||
export declare function numberProxy<T extends Record<string, unknown>>(form: Writable<T>, field: keyof T): Writable<string>; | ||
export declare function dateProxy<T extends Record<string, unknown>>(form: Writable<T>, field: keyof T): Writable<string>; | ||
export declare function dateProxy<T extends Record<string, unknown>>(form: Writable<T>, field: keyof T, options?: { | ||
format: 'date-local' | 'datetime-local' | 'time-local' | 'iso'; | ||
}): Writable<string>; | ||
export declare function jsonProxy<K, T extends Record<string, unknown>, Field extends keyof T, S = T[Field] extends string ? T[Field] : never>(form: Writable<T>, field: Field, initialValue?: K): Writable<S extends never ? never : K>; | ||
export {}; |
import { derived } from 'svelte/store'; | ||
import { stringify, parse } from 'devalue'; | ||
const defaultOptions = { | ||
trueStringValue: 'true', | ||
dateFormat: 'iso' | ||
}; | ||
export function intProxy(form, field) { | ||
return stringProxy(form, field, 'int'); | ||
return stringProxy(form, field, 'int', defaultOptions); | ||
} | ||
export function booleanProxy(form, field) { | ||
return stringProxy(form, field, 'boolean'); | ||
export function booleanProxy(form, field, options = { | ||
trueStringValue: 'true' | ||
}) { | ||
return stringProxy(form, field, 'boolean', { | ||
...defaultOptions, | ||
...options | ||
}); | ||
} | ||
export function numberProxy(form, field) { | ||
return stringProxy(form, field, 'number'); | ||
return stringProxy(form, field, 'number', defaultOptions); | ||
} | ||
export function dateProxy(form, field) { | ||
return stringProxy(form, field, 'date'); | ||
export function dateProxy(form, field, options = { | ||
format: 'iso' | ||
}) { | ||
return stringProxy(form, field, 'date', { | ||
...defaultOptions, | ||
dateFormat: options.format | ||
}); | ||
} | ||
@@ -21,23 +35,46 @@ /** | ||
*/ | ||
function stringProxy(form, field, type) { | ||
function stringProxy(form, field, type, options) { | ||
function toValue(val) { | ||
if (typeof val === 'string') { | ||
if (type == 'number') | ||
return parseFloat(val); | ||
if (type == 'int') | ||
return parseInt(val, 10); | ||
if (type == 'boolean') | ||
return !!val; | ||
if (type == 'date') | ||
return new Date(val); | ||
if (typeof val !== 'string') | ||
throw new Error('stringProxy received a non-string value.'); | ||
if (type == 'number') | ||
return parseFloat(val); | ||
if (type == 'int') | ||
return parseInt(val, 10); | ||
if (type == 'boolean') | ||
return !!val; | ||
else { | ||
// date | ||
return new Date(val); | ||
} | ||
throw new Error('stringProxy received a non-string value.'); | ||
} | ||
const proxy = derived(form, ($form) => { | ||
const value = $form[field]; | ||
if (value === undefined || value === null) | ||
return ''; | ||
if (type == 'int' || type == 'number') { | ||
const num = $form[field]; | ||
const num = value; | ||
return isNaN(num) ? '' : String(num); | ||
} | ||
else if (type == 'date') { | ||
const date = value; | ||
if (isNaN(date)) | ||
return ''; | ||
if (options.dateFormat == 'date-local') { | ||
return localDate(date); | ||
} | ||
else if (options.dateFormat == 'datetime-local') { | ||
return localDate(date) + 'T' + localTime(date); | ||
} | ||
else if (options.dateFormat == 'time-local') { | ||
return localTime(date); | ||
} | ||
else { | ||
// iso | ||
return date.toISOString(); | ||
} | ||
} | ||
else { | ||
return $form[field] ? 'true' : ''; | ||
// boolean | ||
return value ? options.trueStringValue : ''; | ||
} | ||
@@ -86,1 +123,13 @@ }); | ||
} | ||
function localDate(date) { | ||
return (date.getFullYear() + | ||
'-' + | ||
String(date.getMonth() + 1).padStart(2, '0') + | ||
'-' + | ||
String(date.getDate()).padStart(2, '0')); | ||
} | ||
function localTime(date) { | ||
return (String(date.getHours()).padStart(2, '0') + | ||
':' + | ||
String(date.getMinutes()).padStart(2, '0')); | ||
} |
{ | ||
"name": "sveltekit-superforms", | ||
"version": "0.5.12", | ||
"version": "0.5.13", | ||
"author": "Andreas Söderlund <ciscoheat@gmail.com> (https://blog.encodeart.dev)", | ||
@@ -5,0 +5,0 @@ "description": "Supercharge your SvelteKit forms with this powerhouse of a library!", |
@@ -204,3 +204,3 @@ # sveltekit-superforms 💥 | ||
bind:value={$form.email} | ||
{...$constraints.name} | ||
{...$constraints.email} | ||
/> | ||
@@ -207,0 +207,0 @@ {#if $errors.email}<span class="invalid">{$errors.email}</span>{/if} |
Sorry, the diff of this file is not supported yet
81957
1326