mobx-keystone
Version:
A MobX powered state management solution based on data trees with first class support for TypeScript, snapshots, patches and much more
33 lines (26 loc) • 1.05 kB
text/typescript
import type { ModelPropTransform } from "../modelShared/prop"
import { ImmutableDate } from "./ImmutableDate"
const _timestampToDateTransform: ModelPropTransform<number, Date> = {
transform({ originalValue, cachedTransformedValue }) {
return cachedTransformedValue ?? new ImmutableDate(originalValue)
},
untransform({ transformedValue, cacheTransformedValue }) {
if (transformedValue instanceof ImmutableDate) {
cacheTransformedValue()
}
return +transformedValue
},
}
export const timestampToDateTransform = () => _timestampToDateTransform
const _isoStringToDateTransform: ModelPropTransform<string, Date> = {
transform({ originalValue, cachedTransformedValue }) {
return cachedTransformedValue ?? new ImmutableDate(originalValue)
},
untransform({ transformedValue, cacheTransformedValue }) {
if (transformedValue instanceof ImmutableDate) {
cacheTransformedValue()
}
return transformedValue.toISOString()
},
}
export const isoStringToDateTransform = () => _isoStringToDateTransform