UNPKG

plywood

Version:
101 lines (100 loc) 4.29 kB
import { __extends } from "tslib"; import { Duration, Timezone } from 'chronoshift'; import { immutableEqual } from 'immutable-class'; import { Range } from '../datatypes'; import { TimeRange } from '../datatypes/timeRange'; import { pluralIfNeeded } from '../helper/utils'; import { ChainableExpression, Expression } from './baseExpression'; import { HasTimezone } from './mixins/hasTimezone'; var TimeRangeExpression = (function (_super) { __extends(TimeRangeExpression, _super); function TimeRangeExpression(parameters) { var _this = _super.call(this, parameters, dummyObject) || this; _this.duration = parameters.duration; _this.step = parameters.step || TimeRangeExpression.DEFAULT_STEP; _this.timezone = parameters.timezone; _this.bounds = parameters.bounds; _this._ensureOp('timeRange'); _this._checkOperandTypes('TIME'); if (!(_this.duration instanceof Duration)) { throw new Error('`duration` must be a Duration'); } _this.type = 'TIME_RANGE'; return _this; } TimeRangeExpression.fromJS = function (parameters) { var value = ChainableExpression.jsToValue(parameters); value.duration = Duration.fromJS(parameters.duration); value.step = parameters.step; value.bounds = parameters.bounds; if (parameters.timezone) value.timezone = Timezone.fromJS(parameters.timezone); return new TimeRangeExpression(value); }; TimeRangeExpression.prototype.valueOf = function () { var value = _super.prototype.valueOf.call(this); value.duration = this.duration; value.step = this.step; if (this.timezone) value.timezone = this.timezone; if (this.bounds) value.bounds = this.bounds; return value; }; TimeRangeExpression.prototype.toJS = function () { var js = _super.prototype.toJS.call(this); js.duration = this.duration.toJS(); js.step = this.step; if (this.timezone) js.timezone = this.timezone.toJS(); if (this.bounds) js.bounds = this.bounds; return js; }; TimeRangeExpression.prototype.equals = function (other) { return (_super.prototype.equals.call(this, other) && this.duration.equals(other.duration) && this.step === other.step && Range.areEquivalentBounds(this.bounds, other.bounds) && immutableEqual(this.timezone, other.timezone)); }; TimeRangeExpression.prototype._toStringParameters = function (_indent) { var ret = [this.duration.toString(), this.step.toString()]; if (this.timezone) ret.push(Expression.safeString(this.timezone.toString())); return ret; }; TimeRangeExpression.prototype.getQualifiedDurationDescription = function (capitalize) { var step = Math.abs(this.step); var durationDescription = this.duration.getDescription(capitalize); return step !== 1 ? pluralIfNeeded(step, durationDescription) : durationDescription; }; TimeRangeExpression.prototype._calcChainableHelper = function (operandValue) { var duration = this.duration; var step = this.step; var timezone = this.getTimezone(); if (operandValue === null) return null; var other = duration.shift(operandValue, timezone, step); if (step > 0) { return new TimeRange({ start: operandValue, end: other, bounds: this.bounds }); } else { return new TimeRange({ start: other, end: operandValue, bounds: this.bounds }); } }; TimeRangeExpression.prototype._getSQLChainableHelper = function (_dialect, _operandSQL) { throw new Error('implement me'); }; TimeRangeExpression.prototype.changeBounds = function (bounds) { var value = this.valueOf(); value.bounds = bounds; return Expression.fromValue(value); }; TimeRangeExpression.DEFAULT_STEP = 1; TimeRangeExpression.op = 'TimeRange'; return TimeRangeExpression; }(ChainableExpression)); export { TimeRangeExpression }; Expression.applyMixins(TimeRangeExpression, [HasTimezone]); Expression.register(TimeRangeExpression);