asn1-ts
Version:
ASN.1 encoding and decoding, including BER, CER, and DER.
86 lines (85 loc) • 3.23 kB
JavaScript
import * as errors from "../../errors.mjs";
import datetimeComponentValidator from "../../validators/datetimeComponentValidator.mjs";
export default class DURATION_EQUIVALENT {
constructor(years, months, weeks, days, hours, minutes, seconds, fractional_part) {
this.years = years;
this.months = months;
this.weeks = weeks;
this.days = days;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.fractional_part = fractional_part;
if (typeof weeks !== "undefined"
&& (years || months || days || hours || minutes || seconds)) {
throw new errors.ASN1Error("DURATION-EQUIVALENT may not combine week components and date-time components.");
}
if (years) {
datetimeComponentValidator("year", 0, Number.MAX_SAFE_INTEGER)("DURATION-EQUIVALENT", years);
}
if (months) {
datetimeComponentValidator("month", 0, Number.MAX_SAFE_INTEGER)("DURATION-EQUIVALENT", months);
}
if (weeks) {
datetimeComponentValidator("week", 0, Number.MAX_SAFE_INTEGER)("DURATION-EQUIVALENT", weeks);
}
if (days) {
datetimeComponentValidator("day", 0, Number.MAX_SAFE_INTEGER)("DURATION-EQUIVALENT", days);
}
if (hours) {
datetimeComponentValidator("hour", 0, Number.MAX_SAFE_INTEGER)("DURATION-EQUIVALENT", hours);
}
if (minutes) {
datetimeComponentValidator("minute", 0, Number.MAX_SAFE_INTEGER)("DURATION-EQUIVALENT", minutes);
}
if (seconds) {
datetimeComponentValidator("second", 0, Number.MAX_SAFE_INTEGER)("DURATION-EQUIVALENT", seconds);
}
if (fractional_part && !Number.isSafeInteger(fractional_part.fractional_value)) {
throw new errors.ASN1Error("Malformed DURATION-EQUIVALENT fractional part.");
}
}
toString() {
let ret = "DURATION { ";
if (this.years !== undefined) {
ret += `years ${this.years}`;
}
if (this.months !== undefined) {
ret += `months ${this.months}`;
}
if (this.weeks !== undefined) {
ret += `weeks ${this.weeks}`;
}
if (this.days !== undefined) {
ret += `days ${this.days}`;
}
if (this.hours !== undefined) {
ret += `hours ${this.hours}`;
}
if (this.minutes !== undefined) {
ret += `minutes ${this.minutes}`;
}
if (this.seconds !== undefined) {
ret += `seconds ${this.seconds}`;
}
ret += "}";
return ret;
}
toJSON() {
return {
years: this.years,
months: this.months,
weeks: this.weeks,
days: this.days,
hours: this.hours,
minutes: this.minutes,
seconds: this.seconds,
fractional_part: this.fractional_part
? {
number_of_digits: this.fractional_part.number_of_digits,
fractional_value: this.fractional_part.fractional_value,
}
: undefined,
};
}
}