UNPKG

diffusion

Version:

Diffusion JavaScript client

116 lines (88 loc) 3.11 kB
var requireNonNull = require('util/require-non-null'); function requireNonNegative(i, what) { if (i < 0 || i === undefined || i === null) { throw new Error(what + " is negative: " + i); } return i; } function Type(id, anchorOperator, spanOperator, unitsSuffix) { this.id = id; this.anchorOperator = anchorOperator; this.spanOperator = spanOperator; this.unitsSuffix = unitsSuffix; } var Types = { ABSOLUTE_START : new Type(0, "fromStart", "toStart", ""), ABSOLUTE_SEQUENCE : new Type(1, "from", "to", ""), ABSOLUTE_TIME : new Type(2, "from", "to", " ms"), OFFSET_SEQUENCE : new Type(3, "fromLast", "untilLast", ""), OFFSET_TIME : new Type(4, "fromLast", "untilLast", " ms"), NEXT_COUNT : new Type(5, null, "next", ""), NEXT_TIME : new Type(6, null, "next", " ms"), PREVIOUS_COUNT : new Type(7, null, "previous", ""), PREVIOUS_TIME : new Type(8, null, "previous", " ms") }; function Point(value, type) { this.value = value; this.type = type; this.isAbsolute = function() { return this.type.anchorOperator; }; this.equals = function(other) { if (other && other instanceof Point) { return this.value === other.value && this.type === other.type; } return false; }; function toOperatorDescription(operator) { var s = "." + operator + "("; if (type !== Types.ABSOLUTE_START) { s += value + type.unitsSuffix; } s += ")"; return s; } this.toAnchorDescription = function() { return toOperatorDescription(type.anchorOperator); }; this.toSpanDescription = function() { return toOperatorDescription(type.spanOperator); }; } Point.prototype.equals = function(other) { if (other && other instanceof Point) { return this.value === other.value && this.type === other.type; } return false; }; var START_POINT = new Point(0, Types.ABSOLUTE_START); Point.at = function(sequence) { if (sequence instanceof Date) { return new Point(requireNonNull(sequence, "Date").getTime(), Types.ABSOLUTE_TIME); } return new Point(requireNonNegative(sequence, "Sequence"), Types.ABSOLUTE_SEQUENCE); }; Point.atStart = function() { return START_POINT; }; Point.offset = function(count) { return new Point(requireNonNegative(count, "Count"), Types.OFFSET_SEQUENCE); }; Point.offsetMillis = function(timespan) { return new Point(requireNonNegative(timespan, "Timespan"), Types.OFFSET_TIME); }; Point.next = function(count) { return new Point(requireNonNegative(count, "Count"), Types.NEXT_COUNT); }; Point.nextMillis = function(timespan) { return new Point(requireNonNegative(timespan, "Timespan"), Types.NEXT_TIME); }; Point.previous = function(count) { return new Point(requireNonNegative(count, "Count"), Types.PREVIOUS_COUNT); }; Point.previousMillis = function(timespan) { return new Point(requireNonNegative(timespan, "Timespan"), Types.PREVIOUS_TIME); }; Point.Type = Types; module.exports = Point;