All files / src/elm interval.coffee

89% Statements 186/209
82.64% Branches 119/144
100% Functions 48/48
88.94% Lines 185/208

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 46685x 1x 1x 1x 1x 1x 1x     1x   14809x 14809x 14809x 14809x 14809x       1x                       1x       1x       1x       1x       1x     1x   343x 343x     49x     1x   343x 343x     49x     1x   343x 343x     49x     1x   153x 153x     24x     1x   210x 210x     26x     1x   210x 210x     26x       1x       1x       1x     1x   32x         1x   80x     37x 1x 36x   1x   49x     7x 1x 6x   1x   70x 70x     14x     1x   70x 70x     14x     1x   156x   156x   8x     5x     161x 161x                           74x   2x           28x   2x           21x   2x           24x   4x           4x                 156x     1x   2057x       156x 156x   5x     151x       102x 102x   21x 21x   28x 28x       151x 151x         10x     73x 73x   146x 146x 33x 113x   108x       8x 8x   3x   26x   71x 71x     71x 71x           71x           34x         37x     32x 101x 101x   39x 92x 92x 92x         45x 71x         1x   19x 20x 20x 20x     2x   18x     18x 41x 41x 18x     2x 24x       42x 42x 8x 8x 42x   42x 42x   1x   786x       49x 49x   1x   200x 200x     260x                           15x 15x   6x   11x     1x 1x       1x         12x     54x   30x 7x   18x 1x       4x     1x 1x     1x               54x 54x 54x   54x       17x       6x 6x     11x   5x 5x     15x   4x 4x 62x 54x    
{ Expression, UnimplementedExpression } = require './expression'
{ ThreeValuedLogic } = require '../datatypes/logic'
{ build } = require './builder'
{ Quantity, doAddition, doSubtraction, compare_units, convert_value } = require './quantity'
{ successor, predecessor, MIN_FLOAT_PRECISION_VALUE } = require '../util/math'
dtivl = require '../datatypes/interval'
cmp = require '../util/comparison'
 
 
module.exports.Interval = class Interval extends Expression
  constructor: (json) ->
    super
    @lowClosed = json.lowClosed
    @highClosed = json.highClosed
    @low = build(json.low)
    @high = build(json.high)
 
  # Define a simple getter to allow type-checking of this class without instanceof
  # and in a way that survives minification (as opposed to checking constructor.name)
  Object.defineProperties @prototype,
    isInterval:
      get: -> true
 
  exec: (ctx) ->
    new dtivl.Interval(@low.execute(ctx), @high.execute(ctx), @lowClosed, @highClosed)
 
# Equal is completely handled by overloaded#Equal
 
# NotEqual is completely handled by overloaded#Equal
 
# Delegated to by overloaded#Contains and overloaded#In
module.exports.doContains = (interval, item, precision) ->
  interval.contains item, precision
 
# Delegated to by overloaded#Includes and overloaded#IncludedIn
module.exports.doIncludes = doIncludes = (interval, subinterval, precision) ->
  interval.includes subinterval, precision
 
# Delegated to by overloaded#ProperIncludes and overloaded@ProperIncludedIn
module.exports.doProperIncludes = (interval, subinterval, precision) ->
  interval.properlyIncludes subinterval, precision
 
# Delegated to by overloaded#After
module.exports.doAfter = (a, b, precision) ->
  a.after b, precision
 
# Delegated to by overloaded#Before
module.exports.doBefore = (a, b, precision) ->
  a.before b, precision
 
module.exports.Meets = class Meets extends Expression
  constructor: (json) ->
    super
    @precision = json.precision?.toLowerCase()
 
  exec: (ctx) ->
    [a, b] = @execArgs ctx
    if a? and b? then a.meets(b, @precision) else null
 
module.exports.MeetsAfter = class MeetsAfter extends Expression
  constructor: (json) ->
    super
    @precision = json.precision?.toLowerCase()
 
  exec: (ctx) ->
    [a, b] = @execArgs ctx
    if a? and b? then a.meetsAfter(b, @precision) else null
 
module.exports.MeetsBefore = class MeetsBefore extends Expression
  constructor: (json) ->
    super
    @precision = json.precision?.toLowerCase()
 
  exec: (ctx) ->
    [a, b] = @execArgs ctx
    if a? and b? then a.meetsBefore(b, @precision) else null
 
module.exports.Overlaps = class Overlaps extends Expression
  constructor: (json) ->
    super
    @precision = json.precision?.toLowerCase()
 
  exec: (ctx) ->
    [a, b] = @execArgs ctx
    if a? and b? then a.overlaps(b, @precision) else null
 
module.exports.OverlapsAfter = class OverlapsAfter extends Expression
  constructor: (json) ->
    super
    @precision = json.precision?.toLowerCase()
 
  exec: (ctx) ->
    [a, b] = @execArgs ctx
    if a? and b? then a.overlapsAfter(b, @precision) else null
 
module.exports.OverlapsBefore = class OverlapsBefore extends Expression
  constructor: (json) ->
    super
    @precision = json.precision?.toLowerCase()
 
  exec: (ctx) ->
    [a, b] = @execArgs ctx
    if a? and b? then a.overlapsBefore(b, @precision) else null
 
# Delegated to by overloaded#Union
module.exports.doUnion = (a, b) ->
  a.union(b)
 
# Delegated to by overloaded#Except
module.exports.doExcept = (a, b) ->
  if a? and b? then a.except(b) else null
 
# Delegated to by overloaded#Intersect
module.exports.doIntersect = (a, b) ->
  if a? and b? then a.intersect(b) else null
 
module.exports.Width = class Width extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    @arg.execute(ctx)?.width()
 
module.exports.Start = class Start extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    interval = @arg.execute(ctx)
    return null if !interval?
    return interval.start()
 
module.exports.End = class End  extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    interval = @arg.execute(ctx)
    return null if !interval?
    return interval.end()
 
module.exports.Starts = class Starts extends Expression
  constructor: (json) ->
    super
    @precision = json.precision?.toLowerCase()
 
  exec: (ctx) ->
    [a, b] = @execArgs ctx
    if a? and b? then a.starts(b, @precision) else null
 
module.exports.Ends = class Ends extends Expression
  constructor: (json) ->
    super
    @precision = json.precision?.toLowerCase()
 
  exec: (ctx) ->
    [a, b] = @execArgs ctx
    if a? and b? then a.ends(b, @precision) else null
 
intervalListType = (intervals) ->
  # Returns one of null, 'time', 'date', 'datetime', 'quantity', 'integer', 'decimal' or 'mismatch'
  type = null;
 
  for itvl in intervals
    if !itvl?
      continue
 
    if !itvl.low? and !itvl.high? #can't really determine type from this
      continue
 
    # if one end is null (but not both), the type can be determined from the other end
    low = itvl.low ? itvl.high
    high = itvl.high ? itvl.low
 
    if (low.isTime?() and high.isTime?())
      if !type?
        type = 'time'
      else if type is 'time'
        continue
      else
        return 'mismatch'
 
    # if an interval mixes date and datetime, type is datetime (for implicit conversion)
    else if (low.isDateTime or high.isDateTime) and
    (low.isDateTime or low.isDate) and (high.isDateTime or high.isDate)
      if (!type? or type is 'date')
        type = 'datetime'
      else if type is 'datetime'
        continue
      else
        return 'mismatch'
 
    else if (low.isDate and high.isDate)
      if !type?
        type = 'date'
      else if type is 'date' or 'datetime'
        continue
      else
        return 'mismatch'
 
    else if (low.isQuantity and high.isQuantity)
      if !type?
        type = 'quantity'
      else if type is 'quantity'
        continue
      else
        return 'mismatch'
 
    else if (Number.isInteger(low) and Number.isInteger(high))
      if !type?
        type = 'integer'
      else if type is 'integer' or 'decimal'
        continue
      else
        return 'mismatch'
 
    else if (typeof low is 'number' and typeof high is 'number')
      if (!type? or type is 'integer')
        type = 'decimal'
      else if type is 'decimal'
        continue
      else
        return 'mismatch'
    #if we are here ends are mismatched
    else
      return 'mismatch'
 
  return type
 
 
module.exports.Expand = class Expand extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    # expand(argument List<Interval<T>>, per Quantity) List<Interval<T>>
    [intervals, per] = @execArgs ctx
    type = intervalListType(intervals)
    throw new Error("List of intervals contains mismatched types.") if type is 'mismatch'
    return null if !type?
 
    # this step collapses overlaps, and also returns a clone of intervals so we can feel free to mutate
    intervals = collapseIntervals(intervals, per)
    return [] if intervals.length == 0
 
    if type in ["time", "date", "datetime"]
      expandFunction = @expandDTishInterval
      defaultPer = (interval) -> new Quantity(value: 1, unit: interval.low.getPrecision())
    else if type in ["quantity"]
      expandFunction = @expandQuantityInterval
      defaultPer = (interval) -> new Quantity(value: 1, unit: interval.low.unit)
    else if type in ["integer", "decimal"]
      expandFunction = @expandNumericInterval
      defaultPer = (interval) -> new Quantity(value: 1, unit: '1')
    else
      throw new Error("Interval list type not yet supported.")
 
    results = []
    for interval in intervals
      if !interval?
        continue
      # We do not support open ended intervals since result would likely be too long
      if !interval.low? or !interval.high?
        return null
 
      if type is 'datetime' #support for implicitly converting dates to datetime
        interval.low = interval.low.getDateTime()
        interval.high = interval.high.getDateTime()
 
      per = per ? defaultPer(interval)
      items = expandFunction.call(@,interval,per)
      return null if items == null
      results.push(items...)
 
    return results
 
  expandDTishInterval: (interval, per) ->
    if per.unit in ['week', 'weeks']
      per.value *= 7
      per.unit = 'day'
    # return null if precision not applicable (e.g. gram, or minutes for dates)
    return null if per.unit not in interval.low.constructor.FIELDS
    # return null if per is more precise than the low end
    return null if interval.low.isLessPrecise(per.unit)
 
    low = if interval.lowClosed then interval.low else interval.low.successor()
    high = if interval.highClosed then interval.high else interval.high.predecessor()
    return [] if low.after(high)
 
    current_low = low
    results = []
    # Check if interval should be a point interval, for example "expand [@2000-01-01, @2000-01-02]
    # per 1 day" results in intervals with the same start and end: { [@2000-01-01, @2000-01-01],
    # [@2000-01-02, @2000-01-02] }. However "expand [@2000-01-01, @2000-03-01] per 1 month" results
    # in { [@2000-01-01, @2000-01-31], [@2000-02-01, @2000-02-28] }. This check allows us
    # to skip the unnecessary date arithmetic in the first case.
    point_intervals = current_low.add(per.value, per.unit).predecessor().equals(current_low)
 
    # count is how many items to put in the result - take duration and then add a one for cases
    # where e.g. the high point ends a month, like 2018-03-31. Then divide by the per value.
    # We do this to avoid a date comparison operation for each item in the result list.
    if per.unit == low.getPrecision()
      count = Math.floor((low.durationBetween(high,per.unit).high + 1) / per.value)
    # if precisions are equal, we want the high uncertainty (e.g. [2000, 2002] per year should
    # give a list of 3) else, take the low uncertainty (e.g. [2012-01-01T13:00:00, 2012-01-02T12:59]
    # per day should give {} since we dont include any uncertain portions)
    else
      count = Math.floor((low.durationBetween(high,per.unit).low + 1) / per.value)
 
    if point_intervals
      for i in [1..count + 1] #point intervals result in one extra item (see above example)
        results.push(new dtivl.Interval(current_low, current_low.copy(), true, true))
        current_low = current_low.add(per.value, per.unit)
    else
      for i in [1..count]
        current_high = current_low.add(per.value, per.unit).predecessor()
        results.push(new dtivl.Interval(current_low, current_high, true, true))
        current_low = current_low.add(per.value, per.unit)
    # because of the +1 in the count declaration, we might have added an extraneous item at the end.
    # we use 'not sameOrBefore' instead of 'after' since we do not include items in the results
    # if they just *might* not be after (due to uncertainty)
    if results.length > 0 and !results[results.length-1].high.sameOrBefore(high)
      results.pop()
    return results
    
  expandQuantityInterval: (interval, per) ->
    # we want to convert everything to the more precise of the interval.low or per
    if compare_units(interval.low.unit, per.unit) > 0 #interval.low.unit is 'bigger' aka les precise
      result_units = per.unit
    else
      result_units = interval.low.unit
    low_value = convert_value(interval.low.value, interval.low.unit, result_units)
    high_value = convert_value(interval.high.value, interval.high.unit, result_units)
    per_value = convert_value(per.value, per.unit, result_units)
 
    # return null if unit conversion failed, must have mismatched units
    return null if not (low_value? and high_value? and per_value?)
 
    results = @makeNumericIntervalList(
      low_value, high_value, interval.lowClosed, interval.highClosed, per_value)
 
    for itvl in results
      itvl.low = new Quantity(value: itvl.low, unit:result_units)
      itvl.high = new Quantity(value: itvl.high, unit:result_units)
    return results
 
  expandNumericInterval: (interval, per) ->
    return null if per.unit != '1'
    return @makeNumericIntervalList(
      interval.low, interval.high, interval.lowClosed, interval.highClosed, per.value)
 
  makeNumericIntervalList: (low, high, lowClosed, highClosed, per) ->
    point_intervals = Number.isInteger(low) and Number.isInteger(high) and Number.isInteger(per)
    gap = if point_intervals then 1 else MIN_FLOAT_PRECISION_VALUE
    low = low + gap if !lowClosed
    high = high - gap if !highClosed
    width = per - gap
    return [] if low > high
    results = (new dtivl.Interval(x, x + width, true, true) for x in [low..(high - width)] by per)
    return results
 
module.exports.Collapse = class Collapse extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    # collapse(argument List<Interval<T>>, per Quantity) List<Interval<T>>
    [intervals, perWidth] = @execArgs ctx
    return collapseIntervals(intervals, perWidth)
 
collapseIntervals = (intervals, perWidth) ->
  # Clone intervals so this function remains idempotent
  intervalsClone = []
  for interval in intervals
    # The spec says to ignore null intervals
    if interval?
      intervalsClone.push interval.copy()
 
  # If the list is null, return null
  if !intervals?
    null
  else if intervalsClone?.length <= 1
    intervalsClone
  else
    # If the per argument is null, the default unit interval for the point type
    # of the intervals involved will be used (i.e. the interval that has a
    # width equal to the result of the successor function for the point type).
    if !perWidth?
      if intervalsClone[0].low?
        if intervalsClone[0].low.isDateTime
          precisionUnits = intervalsClone[0].low.getPrecision()
          perWidth = new Quantity(value: 1, unit: precisionUnits)
        else if intervalsClone[0].low.isQuantity
          perWidth = doSubtraction(successor(intervalsClone[0].low), intervalsClone[0].low)
        else
          perWidth = successor(intervalsClone[0].low) - intervalsClone[0].low
      else if intervalsClone[0].high?
        if intervalsClone[0].high.isDateTime
          precisionUnits = intervalsClone[0].high.getPrecision()
          perWidth = new Quantity(value: 1, unit: precisionUnits)
        else if intervalsClone[0].high.isQuantity
          perWidth = doSubtraction(successor(intervalsClone[0].high), intervalsClone[0].high)
        else
          perWidth = successor(intervalsClone[0].high) - intervalsClone[0].high
      else
        throw new Error("Point type of intervals provided to collapse cannot be determined.")
 
      if typeof perWidth is 'number'
        perWidth = new Quantity(value: perWidth, unit: '1')
 
    # sort intervalsClone by start
    intervalsClone.sort (a,b)->
      if typeof a.low?.before == 'function'
        return -1 if b.low? and a.low.before b.low
        return 1 if !b.low? or a.low.after b.low
      else if a.low? and b.low?
        return -1 if a.low < b.low
        return 1 if a.low > b.low
      else if a.low? and !b.low?
        return 1
      else if !a.low? and b.low?
        return -1
      # if both lows are undefined, sort by high
      if typeof a.high?.before == 'function'
        return -1 if !b.high? or a.high.before b.high
        return 1 if a.high.after b.high
      else if a.high? and b.high?
        return -1 if a.high < b.high
        return 1 if a.high > b.high
      else if a.high? and !b.high?
        return -1
      else if !a.high? and b.high?
        return 1
      0
 
    # collapse intervals as necessary
    collapsedIntervals = []
    a = intervalsClone.shift()
    b = intervalsClone.shift()
 
    while b
      if typeof b.low?.durationBetween == 'function'
        # handle DateTimes using durationBetween
        if a.high?.sameOrAfter b.low # overlap
          a.high = b.high if !b.high? or b.high.after a.high
        else if a.high?.durationBetween(b.low, perWidth.unit).high <= perWidth.value
          a.high = b.high
        else
          collapsedIntervals.push a
          a = b
      else if typeof b.low?.sameOrBefore == 'function'
        if a.high? and b.low.sameOrBefore doAddition(a.high, perWidth)
          a.high = b.high if !b.high? or b.high.after a.high
        else
          collapsedIntervals.push a
          a = b
      else
        if (b.low - a.high) <= perWidth.value
          a.high = b.high if b.high > a.high || !b.high?
        else
          collapsedIntervals.push a
          a = b
      b = intervalsClone.shift()
    collapsedIntervals.push a
    collapsedIntervals