All files / src/elm type.coffee

95.17% Statements 138/145
80% Branches 40/50
95.45% Functions 42/44
95.14% Lines 137/144

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 330193x 1x 1x 1x 1x 1x 1x 1x       1x   5422x 5422x 5422x 5422x           1x   109x     11x   11x                   1x   157x     5x     1x   42x     5x       2x   3x   1x   462x     32x       26x   6x   1x   304x     96x   92x 92x 88x 8x   1x   87x     10x   10x 6x 4x   1x   1667x     124x   123x 122x   1x   1x   39x     9x         8x 3x 5x 4x       2x     3x   1x   1x   32x     9x     1x   211x     17x   16x     16x 3x 13x 13x 13x     1x 12x   1x 10x   1x 8x 10x   7x     4x 4x   2x     10x   1x   1x                                                         1x   16x 16x     4x   1x       1x   9x 9x     3x   1x       1x   16x 16x     4x   1x       1x   9x 9x     3x   1x       1x   9x 9x     3x   1x       1x   36x 36x     6x   1x       1x   16x 16x     4x   1x       1x   6x 6x     2x   1x       1x   9x 9x     3x   1x       1x   23x         1x 1x 1x 1x 1x  
{ Expression, UnimplementedExpression } = require './expression'
{ FunctionRef } = require './reusable'
{ DateTime, Date } = require '../datatypes/datetime'
{ Concept } = require '../datatypes/clinical'
{ parseQuantity } = require './quantity'
{ isValidDecimal, isValidInteger, limitDecimalPrecision } = require('../util/math')
{ normalizeMillisecondsField } = require '../util/util'
{ Ratio } = require './ratio'
 
# TODO: Casting and Conversion needs unit tests!
 
module.exports.As = class As extends Expression
  constructor: (json) ->
    super
    @asType = json.asType
    @asTypeSpecifier = json.asTypeSpecifier
    @strict = json.strict ? false
 
  exec: (ctx) ->
    # TODO: Currently just returns the arg (which works for null, but probably not others)
    @execArgs(ctx)
 
module.exports.ToBoolean = class ToBoolean extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if arg? and typeof arg != 'undefined'
      strArg = arg.toString().toLowerCase()
      if strArg in ["true", "t", "yes", "y", "1"]
        true
      else if strArg in ["false", "f", "no", "n", "0"]
        false
      else
        null
    else
      null
 
module.exports.ToConcept = class ToConcept extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if arg? and typeof arg != 'undefined' then new Concept([arg], arg.display) else null
 
module.exports.ToDate = class ToDate extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if (not arg?)
      return null
    else if arg.isDateTime
      return arg.getDate()
    else
      return Date.parse(arg.toString())
 
module.exports.ToDateTime = class ToDateTime extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if (not arg?)
      return null
    else if arg.isDate
      return arg.getDateTime()
    else
      return DateTime.parse(arg.toString())
 
module.exports.ToDecimal = class ToDecimal extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if arg? and typeof arg != 'undefined'
      decimal = parseFloat(arg.toString())
      decimal = limitDecimalPrecision(decimal)
      return decimal if isValidDecimal(decimal)
    return null
 
module.exports.ToInteger = class ToInteger extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if arg? and typeof arg != 'undefined'
      integer = parseInt(arg.toString())
      return integer if isValidInteger(integer)
    return null
 
module.exports.ToQuantity = class ToQuantity extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if arg? and typeof arg != 'undefined'
      quantity = parseQuantity(arg.toString())
      return quantity
    else
      return null
 
module.exports.ToRatio = class ToRatio extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if arg?
      # Argument will be of form '<quantity>:<quantity>'
      try
        # String will be split into an array. Numerator will be at index 1, Denominator will be at index 4
        splitRatioString = arg.toString().match(/^(\d+(\.\d+)?\s*('.+')?)\s*:\s*(\d+(\.\d+)?\s*('.+')?)$/)
        return null if !splitRatioString?
        numerator = parseQuantity(splitRatioString[1])
        denominator = parseQuantity(splitRatioString[4])
      catch
        # If the input string is not formatted correctly, or cannot be
        # interpreted as a valid Quantity value, the result is null.
        return null
      # The value element of a Quantity must be present.
      return null unless numerator? and denominator?
      return new Ratio({ numerator: numerator, denominator: denominator })
    else
      return null
 
module.exports.ToString = class ToString extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if arg? and typeof arg != 'undefined' then arg.toString() else null
 
module.exports.ToTime = class ToTime extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if arg? and typeof arg != 'undefined'
      timeString = arg.toString()
      # Return null if string doesn't represent a valid ISO-8601 Time
      # Thh:mm:ss.fff(+|-)hh:mm or Thh:mm:ss.fffZ
      matches = /T((\d{2})(\:(\d{2})(\:(\d{2})(\.(\d+))?)?)?)?(Z|(([+-])(\d{2})(\:?(\d{2}))?))?/.exec timeString
      return null unless matches?
      hours = matches[2]
      minutes = matches[4]
      seconds = matches[6]
      # Validate h/m/s if they exist, but allow null
      if hours?
        return null unless hours >= 0 and hours <= 23
        hours = parseInt(hours, 10)
      if minutes?
        return null unless minutes >= 0 and minutes <= 59
        minutes = parseInt(minutes, 10)
      if seconds?
        return null unless seconds >= 0 and seconds <= 59
        seconds = parseInt(seconds, 10)
      milliseconds = matches[8]
      if milliseconds?
        milliseconds = parseInt(normalizeMillisecondsField(milliseconds))
 
      if matches[11]?
        tz = parseInt(matches[12],10) + (if matches[14]? then parseInt(matches[14],10) / 60 else 0)
        timezoneOffset = if matches[11] is '+' then tz else tz * -1
      else if matches[9] == 'Z'
        timezoneOffset = 0
 
      # Time is implemented as Datetime with year 0, month 1, day 1
      return new DateTime(0, 1, 1, hours, minutes, seconds, milliseconds, timezoneOffset)
    else
      return null
 
module.exports.Convert = class Convert extends Expression
  constructor: (json) ->
    super
    @operand = json.operand
    @toType = json.toType
 
  exec: (ctx) ->
    switch @toType
      when "{urn:hl7-org:elm-types:r1}Boolean"
        new ToBoolean({"type": "ToBoolean", "operand": @operand}).execute(ctx)
      when "{urn:hl7-org:elm-types:r1}Concept"
        new ToConcept({"type": "ToConcept", "operand": @operand}).execute(ctx)
      when "{urn:hl7-org:elm-types:r1}Decimal"
        new ToDecimal({"type": "ToDecimal", "operand": @operand}).execute(ctx)
      when "{urn:hl7-org:elm-types:r1}Integer"
        new ToInteger({"type": "ToInteger", "operand": @operand}).execute(ctx)
      when "{urn:hl7-org:elm-types:r1}String"
        new ToString({"type": "ToString", "operand": @operand}).execute(ctx)
      when "{urn:hl7-org:elm-types:r1}Quantity"
        new ToQuantity({"type": "ToQuantity", "operand": @operand}).execute(ctx)
      when "{urn:hl7-org:elm-types:r1}DateTime"
        new ToDateTime({"type": "ToDateTime", "operand": @operand}).execute(ctx)
      when "{urn:hl7-org:elm-types:r1}Date"
        new ToDate({"type": "ToDate", "operand": @operand}).execute(ctx)
      when "{urn:hl7-org:elm-types:r1}Time"
        new ToTime({"type": "ToTime", "operand": @operand}).execute(ctx)
      else
        @execArgs(ctx)
 
module.exports.ConvertsToBoolean = class ConvertsToBoolean extends Expression
  constructor: (json) ->
    super
    @operand = json.operand
 
  exec: (ctx) ->
    operatorValue = @execArgs(ctx)
    if operatorValue is null
      return null
    else
      canConvertToType(ToBoolean, @operand, ctx)
 
module.exports.ConvertsToDate = class ConvertsToDate extends Expression
  constructor: (json) ->
    super
    @operand = json.operand
 
  exec: (ctx) ->
    operatorValue = @execArgs(ctx)
    if operatorValue is null
      return null
    else
      canConvertToType(ToDate, @operand, ctx)
 
module.exports.ConvertsToDateTime = class ConvertsToDateTime extends Expression
  constructor: (json) ->
    super
    @operand = json.operand
 
  exec: (ctx) ->
    operatorValue = @execArgs(ctx)
    if operatorValue is null
      return null
    else
      canConvertToType(ToDateTime, @operand, ctx)
 
module.exports.ConvertsToDecimal = class ConvertsToDecimal extends Expression
  constructor: (json) ->
    super
    @operand = json.operand
 
  exec: (ctx) ->
    operatorValue = @execArgs(ctx)
    if operatorValue is null
      return null
    else
      canConvertToType(ToDecimal, @operand, ctx)
 
module.exports.ConvertsToInteger = class ConvertsToInteger extends Expression
  constructor: (json) ->
    super
    @operand = json.operand
 
  exec: (ctx) ->
    operatorValue = @execArgs(ctx)
    if operatorValue is null
      return null
    else
      canConvertToType(ToInteger, @operand, ctx)
 
module.exports.ConvertsToQuantity = class ConvertsToQuantity extends Expression
  constructor: (json) ->
    super
    @operand = json.operand
 
  exec: (ctx) ->
    operatorValue = @execArgs(ctx)
    if operatorValue is null
      return null
    else
      canConvertToType(ToQuantity, @operand, ctx)
 
module.exports.ConvertsToRatio = class ConvertsToRatio extends Expression
  constructor: (json) ->
    super
    @operand = json.operand
 
  exec: (ctx) ->
    operatorValue = @execArgs(ctx)
    if operatorValue is null
      return null
    else
      canConvertToType(ToRatio, @operand, ctx)
 
module.exports.ConvertsToString = class ConvertsToString extends Expression
  constructor: (json) ->
    super
    @operand = json.operand
 
  exec: (ctx) ->
    operatorValue = @execArgs(ctx)
    if operatorValue is null
      return null
    else
      canConvertToType(ToString, @operand, ctx)
 
module.exports.ConvertsToTime = class ConvertsToTime extends Expression
  constructor: (json) ->
    super
    @operand = json.operand
 
  exec: (ctx) ->
    operatorValue = @execArgs(ctx)
    if operatorValue is null
      return null
    else
      canConvertToType(ToTime, @operand, ctx)
 
canConvertToType = (toFunction, operand, ctx) ->
  try
    value = new toFunction({"type": toFunction.name, "operand": operand}).execute(ctx)
    if value? then true else false
  catch
    false
 
module.exports.Is = class Is extends UnimplementedExpression
module.exports.IntervalTypeSpecifier = class IntervalTypeSpecifier extends UnimplementedExpression
module.exports.ListTypeSpecifier = class ListTypeSpecifier extends UnimplementedExpression
module.exports.NamedTypeSpecifier = class NamedTypeSpecifier extends UnimplementedExpression
module.exports.TupleTypeSpecifier = class TupleTypeSpecifier extends UnimplementedExpression