UNPKG

@lab900/typescript-serializer

Version:

JSON serialization/deserialization of Typescript objects

23 lines (22 loc) 673 B
export function dateDeserializer(sourceObject, type, key) { if (!sourceObject) { return; } if (typeof sourceObject === 'number') { const isInteger = sourceObject % 1 === 0; if (!isInteger) { throw new TypeError(`Could not deserialize ${key} as Date:` + ` expected an integer, got a number with decimal places.`); } return new Date(sourceObject); } else if (typeof sourceObject === 'string') { return new Date(sourceObject); } else if (sourceObject instanceof Date) { return sourceObject; } else { throw Error(`Type mismatch on ${key}`); } }