ifc-expressions
Version:
Parsing and evaluation of IFC expressions
150 lines (149 loc) • 6.29 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IfcDurationValue = void 0;
const Types_js_1 = require("../type/Types.js");
const IfcExpressionUtils_js_1 = require("../util/IfcExpressionUtils.js");
const DateFormatException_js_1 = require("../error/value/DateFormatException.js");
const decimal_js_1 = __importDefault(require("decimal.js"));
class IfcDurationValue {
constructor(value) {
const { years, months, weeks, days, hours, minutes, seconds, fractionAsSeconds, totalSeconds, } = IfcDurationValue.parseIfcDuration(value);
this.years = years;
this.months = months;
this.weeks = weeks;
this.days = days;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.totalSeconds = totalSeconds;
this.fractionAsSeconds = fractionAsSeconds;
this.stringRepresentation = value;
}
static parseIfcDuration(value) {
decimal_js_1.default.set({ defaults: true });
let match = value.match(this.regex);
if ((0, IfcExpressionUtils_js_1.isNullish)(match)) {
throw new DateFormatException_js_1.DateFormatException("Not an IfcDateTime: " + value);
}
let [wholeString, yearsVal, monthsVal, weeksVal, daysVal, hoursVal, minutesVal, secondsVal,] = match;
var years = new decimal_js_1.default((0, IfcExpressionUtils_js_1.isNullish)(yearsVal) ? "0" : yearsVal);
var months = new decimal_js_1.default((0, IfcExpressionUtils_js_1.isNullish)(monthsVal) ? "0" : monthsVal);
var weeks = new decimal_js_1.default((0, IfcExpressionUtils_js_1.isNullish)(weeksVal) ? "0" : weeksVal);
var days = new decimal_js_1.default((0, IfcExpressionUtils_js_1.isNullish)(daysVal) ? "0" : daysVal);
var hours = new decimal_js_1.default((0, IfcExpressionUtils_js_1.isNullish)(hoursVal) ? "0" : hoursVal);
var minutes = new decimal_js_1.default((0, IfcExpressionUtils_js_1.isNullish)(minutesVal) ? "0" : minutesVal);
var seconds = new decimal_js_1.default((0, IfcExpressionUtils_js_1.isNullish)(secondsVal) ? "0" : secondsVal);
var fractionAsSeconds = IfcDurationValue.getFractionAsSeconds(years, months, weeks, days, hours, minutes, seconds);
var totalSeconds = years
.mul(IfcDurationValue.YEAR)
.plus(months.mul(IfcDurationValue.MONTH))
.plus(weeks.mul(IfcDurationValue.WEEK))
.plus(days.mul(IfcDurationValue.DAY))
.plus(hours.mul(IfcDurationValue.HOUR))
.plus(minutes.mul(IfcDurationValue.MINUTE))
.plus(seconds);
return {
years,
months,
weeks,
days,
hours,
minutes,
seconds,
fractionAsSeconds,
totalSeconds,
};
}
static getFractionAsSeconds(years, months, weeks, days, hours, minutes, seconds) {
if (!seconds.isZero() && !seconds.isInteger()) {
return seconds.minus(seconds.divToInt(1));
}
if (!minutes.isZero() && !minutes.isInteger()) {
return minutes.minus(minutes.divToInt(1)).times(IfcDurationValue.MINUTE);
}
if (!hours.isZero() && !hours.isInteger()) {
return hours.minus(hours.divToInt(1)).times(IfcDurationValue.HOUR);
}
if (!days.isZero() && !days.isInteger()) {
return days.minus(days.divToInt(1)).times(IfcDurationValue.DAY);
}
if (!weeks.isZero() && !weeks.isInteger()) {
return weeks.minus(weeks.divToInt(1)).times(IfcDurationValue.WEEK);
}
if (!months.isZero() && !months.isInteger()) {
return months.minus(months.divToInt(1)).times(IfcDurationValue.MONTH);
}
if (!years.isZero() && !years.isInteger()) {
return years.minus(years.divToInt(1)).times(IfcDurationValue.YEAR);
}
return new decimal_js_1.default(0);
}
static of(value) {
return new IfcDurationValue(value);
}
getValue() {
return this;
}
getType() {
return Types_js_1.Type.IFC_DURATION;
}
static isIfcDurationValueType(arg) {
return (arg instanceof IfcDurationValue ||
(!(0, IfcExpressionUtils_js_1.isNullish)(arg.stringRepresentation) &&
!(0, IfcExpressionUtils_js_1.isNullish)(arg.stringRepresentation.match(IfcDurationValue.regex)) &&
!(0, IfcExpressionUtils_js_1.isNullish)(arg.totalSeconds)));
}
static isValidStringRepresentation(str) {
return this.regex.test(str);
}
equals(other) {
return (IfcDurationValue.isIfcDurationValueType(other) &&
this.totalSeconds.eq(other.totalSeconds));
}
toString() {
return this.stringRepresentation;
}
compareTo(other) {
var diff = this.totalSeconds.minus(other.totalSeconds);
return decimal_js_1.default.sign(diff);
}
getYears() {
return this.years;
}
getMonths() {
return this.months;
}
getWeeks() {
return this.weeks;
}
getDays() {
return this.days;
}
getHours() {
return this.hours;
}
getMinutes() {
return this.minutes;
}
getSeconds() {
return this.seconds;
}
getTotalSeconds() {
return this.totalSeconds;
}
getFractionAsSeconds() {
return this.fractionAsSeconds;
}
}
exports.IfcDurationValue = IfcDurationValue;
IfcDurationValue.MINUTE = new decimal_js_1.default(60);
IfcDurationValue.HOUR = IfcDurationValue.MINUTE.mul(60);
IfcDurationValue.DAY = IfcDurationValue.HOUR.mul(24);
IfcDurationValue.WEEK = IfcDurationValue.DAY.mul(7);
IfcDurationValue.YEAR = new decimal_js_1.default(31557600);
IfcDurationValue.MONTH = IfcDurationValue.YEAR.div(12);
IfcDurationValue.regex = /^P(?!$)(?:(\d+(?:\.\d+(?=Y$))?)Y)?(?:(\d+(?:\.\d+(?=M$))?)M)?(?:(\d+(?:\.\d+(?=W$))?)W)?(?:(\d+(?:\.\d+(?=D$))?)D)?(?:T(?!$)(?:(\d+(?:\.\d+(?=H$))?)H)?(?:(\d+(?:\.\d+(?=M$))?)M)?(?:(\d+(?:\.\d+(?=S$))?)S)?)?$/;
//# sourceMappingURL=IfcDurationValue.js.map