type-r2
Version:
Serializable, validated, and observable data layer for modern JS applications
25 lines (19 loc) • 749 B
text/typescript
import {DateType, ChainableAttributeSpec, type} from "type-r";
const msDatePattern = /\/Date\(([0-9]+)\)\//;
export class MicrosoftDateType extends DateType {
convert( next ) {
if( typeof next === 'string' ){
const msDate = msDatePattern.exec( next );
if( msDate ){
return new Date( Number( msDate[ 1 ] ) );
}
}
return DateType.prototype.convert.apply( this, arguments );
}
toJSON( value ) { return value && `/Date(${ value.getTime() })/`; }
}
export const MicrosoftDate = new ChainableAttributeSpec({
type : Date,
_metatype: MicrosoftDateType
})
export const Timestamp = type( Date ).toJSON( x => x && x.getTime() );