Skip to content

Commit

Permalink
fix: add nullable string/number attr converters
Browse files Browse the repository at this point in the history
  • Loading branch information
mihar-22 committed Aug 9, 2023
1 parent 43e84f7 commit dada1f5
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/maverick/src/element/attrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ export interface Attribute<Value = unknown> extends SignalOptions<Value> {
}

export const STRING: AttributeConverter<string | null> = (v) => (v === null ? '' : v + '');
export const NULLABLE_STRING: AttributeConverter<string | null> = (v) =>
v === null ? null : v + '';
export const NUMBER: AttributeConverter<number | null> = (v) => (v === null ? 0 : Number(v));
export const NULLABLE_NUMBER: AttributeConverter<number | null> = (v) =>
v === null ? null : Number(v);
export const BOOLEAN: AttributeConverter<boolean | null> = (v) => v !== null;
export const FUNCTION: AttributeConverter<(() => void) | null> = () => null;
export const ARRAY: AttributeConverter<unknown[] | null> = (v) => (v === null ? [] : JSON.parse(v));
export const OBJECT: AttributeConverter<object | null> = (v) => (v === null ? {} : JSON.parse(v));

export function inferAttributeConverter(value: unknown): AttributeConverter<any> {
if (value === null) return STRING;
if (value === null) return NULLABLE_STRING;
switch (typeof value) {
case 'undefined':
return STRING;
Expand Down

0 comments on commit dada1f5

Please sign in to comment.