Skip to content

Commit

Permalink
feat: Provide default value helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Jan 30, 2022
1 parent 69ed7ed commit 83f1146
Showing 1 changed file with 55 additions and 13 deletions.
68 changes: 55 additions & 13 deletions src/defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,80 @@ export type Serializers<T> = {
serialize: (value: T) => string
}

export type QueryTypeMap = {
string: Serializers<string>
integer: Serializers<number>
float: Serializers<number>
boolean: Serializers<boolean>
timestamp: Serializers<Date>
isoDateTime: Serializers<Date>
export type SerializersWithDefaultFactory<T> = Serializers<T> & {
withDefault: (defaultValue: T) => Serializers<T> & {
readonly defaultValue: T
}
}

export type QueryTypeMap = Readonly<{
string: SerializersWithDefaultFactory<string>
integer: SerializersWithDefaultFactory<number>
float: SerializersWithDefaultFactory<number>
boolean: SerializersWithDefaultFactory<boolean>
timestamp: SerializersWithDefaultFactory<Date>
isoDateTime: SerializersWithDefaultFactory<Date>
}>

export const queryTypes: QueryTypeMap = {
string: {
parse: v => v,
serialize: v => `${v}`
serialize: v => `${v}`,
withDefault(defaultValue) {
return {
...this,
defaultValue
}
}
},
integer: {
parse: v => parseInt(v),
serialize: v => Math.round(v).toFixed()
serialize: v => Math.round(v).toFixed(),
withDefault(defaultValue) {
return {
...this,
defaultValue
}
}
},
float: {
parse: v => parseFloat(v),
serialize: v => v.toString()
serialize: v => v.toString(),
withDefault(defaultValue) {
return {
...this,
defaultValue
}
}
},
boolean: {
parse: v => v === 'true',
serialize: (v: boolean) => (v ? 'true' : 'false')
serialize: v => (Boolean(v) ? 'true' : 'false'),
withDefault(defaultValue) {
return {
...this,
defaultValue
}
}
},
timestamp: {
parse: v => new Date(parseInt(v)),
serialize: (v: Date) => v.valueOf().toString()
serialize: (v: Date) => v.valueOf().toString(),
withDefault(defaultValue) {
return {
...this,
defaultValue
}
}
},
isoDateTime: {
parse: v => new Date(v),
serialize: (v: Date) => v.toISOString()
serialize: (v: Date) => v.toISOString(),
withDefault(defaultValue) {
return {
...this,
defaultValue
}
}
}
}

0 comments on commit 83f1146

Please sign in to comment.