import { Serializer , ValueObject } from "ddd-base" ; export const TimeStampSerializer = { fromJSON ( json ) { return TimeStamp . createTimeStampFromMillisecond ( json ) ; } , toJSON ( entity ) { return entity . millSecond ; } } ; export type TimeStampJSON = number ; export type TimeStampOperator = "==" | ">=" | "<=" | ">" | "<" ; export class TimeStamp extends ValueObject < { } > { value : number ; readonly date : Date ; static createTimeStampFromSecond ( second ) { return new TimeStamp ( second * 1000 ) ; } static createTimeStampFromMillisecond ( millisecond ) { return new TimeStamp ( millisecond ) ; } static createTimeStampFromMicrosecond ( microsecond ) { return new TimeStamp ( microsecond / 1000 ) ; } static now ( ) { return new TimeStamp ( Date . now ( ) ) ; } constructor ( millisecond ) { super ( millisecond ) ; this . value = millisecond ; this . date = new Date ( this . value ) ; } compare ( operator , other ) { switch ( operator ) { case "==" : return this . millSecond === other . millSecond ; case ">=" : return this . millSecond >= other . millSecond ; case "<=" : return this . millSecond <= other . millSecond ; case "<" : return this . millSecond < other . millSecond ; case ">" : return this . millSecond > other . millSecond ; default : throw new Error ( `operator(${ operator }} not supported` ) ; } } get isoString ( ) : string { return this . date . toISOString ( ) ; } get second ( ) { return this . value / 1000 ; } get millSecond ( ) { return this . value ; } }