projects/wms-framework/src/lib/baseframework/TypeSerializers/TimeRangeSerializer.ts
TimeRange serializer for WCF Request
Properties |
|
Methods |
Private reISO |
Default value : /^(-)?P(\d+D)?(T(\d{1,2}H)?(\d{1,2}M)?((\d+)([.]\d+)?S)?)?$/
|
Regex used to deserialize a ISO8601 Duration. |
Deserialize | ||||||
Deserialize(obj: unknown)
|
||||||
Deserialize a TimeRange from string
Parameters :
Returns :
TimeRange
|
Serialize | ||||||
Serialize(obj: TimeRange)
|
||||||
Serialize a TimeRange to string with format compatible with WCF
Parameters :
Returns :
any
{*} |
import { TimeRange } from '../TimeRange';
import { SerializerAndDeserializer } from './SerializerFactory';
/**
* TimeRange serializer for WCF Request
*
* @export
* @class TimeRangeSerializer
* @implements {SerializerAndDeserializer<TimeRange>}
*/
export class TimeRangeSerializer
implements SerializerAndDeserializer<TimeRange>
{
/**
* Regex used to deserialize a ISO8601 Duration.
*
* @private
* @memberof TimeRangeSerializer
*/
private reISO = /^(-)?P(\d+D)?(T(\d{1,2}H)?(\d{1,2}M)?((\d+)([.]\d+)?S)?)?$/;
/**
* Serialize a TimeRange to string with format compatible with WCF
*
* @param {TimeRange} obj
* @return {*} {*}
* @memberof TimeRangeSerializer
*/
Serialize(obj: TimeRange): any {
const tdays = Math.abs(obj.Days);
const thours = Math.abs(obj.Hours);
const tmins = Math.abs(obj.Minutes);
const tsecs = Math.abs(obj.Seconds);
const tmills = Math.abs(obj.Milliseconds);
if (
tdays === 0 &&
thours === 0 &&
tmins === 0 &&
tsecs === 0 &&
tmills === 0
) {
return 'P0D';
}
const sign = obj.Ticks < 0 ? '-' : '';
const days = tdays > 0 ? `${tdays}D` : '';
const time = thours > 0 || tmins > 0 || tsecs > 0 || tmills > 0 ? 'T' : '';
const hours = thours > 0 ? `${thours}H` : '';
const mins = tmins > 0 ? `${tmins}M` : '';
const mills = tmills > 0 ? `.${tmills.toString().padStart(3, '0')}` : '';
const secs = tsecs > 0 || tmills > 0 ? `${tsecs}${mills}S` : '';
return `${sign}P${days}${time}${hours}${mins}${secs}`;
}
/**
* Deserialize a TimeRange from string
*
* @param {unknown} obj
* @returns {TimeRange}
* @memberof TimeRangeSerializer
*/
Deserialize(obj: unknown): TimeRange {
if (typeof obj == 'string') {
const timeWcfISO = this.reISO.exec(obj);
if (timeWcfISO) {
const appliedNegative = false;
const days =
timeWcfISO[2] !== undefined
? timeWcfISO[1] !== undefined
? -Number(timeWcfISO[2].replace('D', ''))
: Number(timeWcfISO[2].replace('D', ''))
: 0;
const hours =
timeWcfISO[4] !== undefined
? timeWcfISO[1] !== undefined
? -Number(timeWcfISO[4].replace('H', ''))
: Number(timeWcfISO[4].replace('H', ''))
: 0;
const minutes =
timeWcfISO[5] !== undefined
? timeWcfISO[1] !== undefined
? -Number(timeWcfISO[5].replace('M', ''))
: Number(timeWcfISO[5].replace('M', ''))
: 0;
const seconds =
timeWcfISO[7] !== undefined
? timeWcfISO[1] !== undefined
? -Number(timeWcfISO[7])
: Number(timeWcfISO[7])
: 0;
const milliseconds =
timeWcfISO[8] !== undefined
? timeWcfISO[1] !== undefined
? -Number(timeWcfISO[8].replace('.', ''))
: Number(timeWcfISO[8].replace('.', ''))
: 0;
return new TimeRange(days, hours, minutes, seconds, milliseconds);
}
return null;
} else {
return null;
}
}
}