UNPKG

ifc-expressions

Version:

Parsing and evaluation of IFC expressions

143 lines (142 loc) 5.43 kB
import { Type } from "../type/Types.js"; import { isNullish } from "../util/IfcExpressionUtils.js"; import { DateFormatException } from "../error/value/DateFormatException.js"; import Decimal from "decimal.js"; export 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.set({ defaults: true }); let match = value.match(this.regex); if (isNullish(match)) { throw new DateFormatException("Not an IfcDateTime: " + value); } let [wholeString, yearsVal, monthsVal, weeksVal, daysVal, hoursVal, minutesVal, secondsVal,] = match; var years = new Decimal(isNullish(yearsVal) ? "0" : yearsVal); var months = new Decimal(isNullish(monthsVal) ? "0" : monthsVal); var weeks = new Decimal(isNullish(weeksVal) ? "0" : weeksVal); var days = new Decimal(isNullish(daysVal) ? "0" : daysVal); var hours = new Decimal(isNullish(hoursVal) ? "0" : hoursVal); var minutes = new Decimal(isNullish(minutesVal) ? "0" : minutesVal); var seconds = new Decimal(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(0); } static of(value) { return new IfcDurationValue(value); } getValue() { return this; } getType() { return Type.IFC_DURATION; } static isIfcDurationValueType(arg) { return (arg instanceof IfcDurationValue || (!isNullish(arg.stringRepresentation) && !isNullish(arg.stringRepresentation.match(IfcDurationValue.regex)) && !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.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; } } IfcDurationValue.MINUTE = new Decimal(60); IfcDurationValue.HOUR = IfcDurationValue.MINUTE.mul(60); IfcDurationValue.DAY = IfcDurationValue.HOUR.mul(24); IfcDurationValue.WEEK = IfcDurationValue.DAY.mul(7); IfcDurationValue.YEAR = new Decimal(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