All files / src/elm datetime.coffee

97.14% Statements 68/70
88.24% Branches 30/34
97.3% Functions 36/37
97.1% Lines 67/69

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 16785x 1x 1x 1x   1x 1x 20130x 20130x       1x         4204x     12060x   1627x 4204x     1x 1x 3705x 3705x       1x         670x   951x 670x     1x 1x   778x 1556x 1700x       1x         54x     1x   2x         1x   3x         1x   1x         1x   170x 170x     22x     1x   9x     3x     1x   9x     3x     1x   12x     4x     1x   819x 819x     69x     1x   819x 819x     69x       1x       1x     1x   732x 732x     74x       74x     1x   464x 464x     28x       28x    
{ Expression } = require './expression'
{ build } = require './builder'
{ Literal } = require './literal'
DT = require '../datatypes/datatypes'
 
module.exports.DateTime = class DateTime extends Expression
  @PROPERTIES = ['year', 'month', 'day', 'hour', 'minute', 'second', 'millisecond', 'timezoneOffset']
  constructor: (@json) ->
    super
 
  # 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,
    isDateTime:
      get: -> true
 
  exec: (ctx) ->
    for property in DateTime.PROPERTIES
      # if json does not contain 'timezoneOffset' set it to the executionDateTime from the context
      if @json[property]?
        @[property] = build @json[property]
      else if property == 'timezoneOffset' and ctx.getTimezoneOffset()?
        @[property] = Literal.from({"type": "Literal", "value": ctx.getTimezoneOffset(), "valueType": "{urn:hl7-org:elm-types:r1}Integer"})
    args = ((if @[p]? then @[p].execute(ctx)) for p in DateTime.PROPERTIES)
    new DT.DateTime(args...)
 
module.exports.Date = class Date extends Expression
  @PROPERTIES = ['year', 'month', 'day']
  constructor: (@json) ->
    super
 
  # 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,
    isDate:
      get: -> true
 
  exec: (ctx) ->
    for property in Date.PROPERTIES
      if @json[property]?
        @[property] = build @json[property]
    args = ((if @[p]? then @[p].execute(ctx)) for p in Date.PROPERTIES)
    new DT.Date(args...)
 
module.exports.Time = class Time extends Expression
  @PROPERTIES = ['hour', 'minute', 'second', 'millisecond', 'timezoneOffset']
  constructor: (json) ->
    super
    for property in Time.PROPERTIES
      if json[property]? then @[property] = build json[property]
 
  # 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,
    isTime:
      get: -> true
 
  exec: (ctx) ->
    args = ((if @[p]? then @[p].execute(ctx)) for p in Time.PROPERTIES)
    (new DT.DateTime(0, 1, 1, args...)).getTime()
 
module.exports.Today = class Today extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    ctx.getExecutionDateTime().getDate()
 
module.exports.Now = class Now extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    ctx.getExecutionDateTime()
 
module.exports.TimeOfDay = class TimeOfDay extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    ctx.getExecutionDateTime().getTime()
 
module.exports.DateTimeComponentFrom = class DateTimeComponentFrom extends Expression
  constructor: (json) ->
    super
    @precision = json.precision
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if arg? then arg[@precision.toLowerCase()] else null
 
module.exports.DateFrom = class DateFrom extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    date = @execArgs(ctx)
    if date? then date.getDate() else null
 
module.exports.TimeFrom = class TimeFrom extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    date = @execArgs(ctx)
    if date? then date.getTime() else null
 
module.exports.TimezoneFrom = class TimezoneFrom extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    date = @execArgs(ctx)
    if date? then date.timezoneOffset else null
 
module.exports.SameOrAfter = class SameOrAfter extends Expression
  constructor: (json) ->
    super
    @precision = json.precision
 
  exec: (ctx) ->
    [d1, d2] = @execArgs(ctx)
    if d1? and d2? then d1.sameOrAfter(d2, @precision?.toLowerCase()) else null
 
module.exports.SameOrBefore = class SameOrBefore extends Expression
  constructor: (json) ->
    super
    @precision = json.precision
 
  exec: (ctx) ->
    [d1, d2] = @execArgs(ctx)
    if d1? and d2? then d1.sameOrBefore(d2, @precision?.toLowerCase()) else null
 
# 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.DifferenceBetween = class DifferenceBetween extends Expression
  constructor: (json) ->
    super
    @precision = json.precision
 
  exec: (ctx) ->
    args = @execArgs(ctx)
    # Check to make sure args exist and that they have differenceBetween functions so that they can be compared to one another
    if !args[0]? || !args[1]? || typeof args[0].differenceBetween != 'function' || typeof args[1].differenceBetween != 'function'
      return null
    result = args[0].differenceBetween(args[1], @precision?.toLowerCase())
    if result? && result.isPoint() then result.low else result
 
module.exports.DurationBetween = class DurationBetween extends Expression
  constructor: (json) ->
    super
    @precision = json.precision
 
  exec: (ctx) ->
    args = @execArgs(ctx)
    # Check to make sure args exist and that they have durationBetween functions so that they can be compared to one another
    if !args[0]? || !args[1]? || typeof args[0].durationBetween != 'function' || typeof args[1].durationBetween != 'function'
      return null
    result = args[0].durationBetween(args[1], @precision?.toLowerCase())
    if result? && result.isPoint() then result.low else result