All files / src/elm arithmetic.coffee

90.48% Statements 76/84
76.47% Branches 26/34
78.18% Functions 43/55
90.36% Lines 75/83

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 261123x 1x 1x 1x 1x   1x   284x     84x                   1x   155x     24x                   1x   116x     93x                   1x   98x     14x                   1x   2x     2x           1x                     1x                     1x   4x     4x           1x   1x   23x     4x               1x   419x     80x               1x   8x 8x     4x       4x     1x   1x     1x           1x                     1x                     1x   1x     1x           1x 1x 1x 1x 1x 1x     25x 25x         1x 1x 1x       1x   1x 1x 1x 1x 1x 1x     30x 30x         2x 2x 2x       1x   1x   121x     11x           1x   121x     11x          
E{ Expression } = require './expression'
{ typeIsArray , allTrue, anyTrue} = require '../util/util'
{ build } = require './builder'
MathUtil = require '../util/math'
Quantity = require('./quantity')
 
module.exports.Add = class Add extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    args = @execArgs(ctx)
    if (not args? || args.some (x) -> not x?)
      null
    else
      args?.reduce (x,y) ->
        if x.isQuantity  or x.isDateTime or x.isDate
          Quantity.doAddition(x,y)
        else
          x + y
 
module.exports.Subtract = class Subtract extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    args = @execArgs(ctx)
    if (not args? || args.some (x) -> not x?)
      null
    else
      args.reduce (x,y) ->
        if x.isQuantity or x.isDateTime or x.isDate
          Quantity.doSubtraction(x,y)
        else
          x - y
 
module.exports.Multiply = class Multiply extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    args = @execArgs(ctx)
    if (not args? || args.some (x) -> not x?)
      null
    else
      args?.reduce (x,y) ->
        if x.isQuantity or y.isQuantity
          Quantity.doMultiplication(x,y)
        else
          x * y
 
module.exports.Divide = class Divide extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    args = @execArgs(ctx)
    if (not args? || args.some (x) -> not x?)
      null
    else
      args?.reduce (x,y) ->
        if x.isQuantity
          Quantity.doDivision(x,y)
        else
          x / y
 
module.exports.TruncatedDivide = class TruncatedDivide extends  Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    args = @execArgs(ctx)
    if (not args? || args.some (x) -> not x?)
      null
    else
      Math.floor( args.reduce (x,y) -> x / y)
 
module.exports.Modulo = class Modulo extends  Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    args = @execArgs(ctx)
    if (not args? || args.some (x) -> not x?)
      null
    else
      args.reduce (x,y) -> x % y
 
module.exports.Ceiling = class Ceiling extends  Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if (not arg?)
      null
    else
      Math.ceil arg
 
module.exports.Floor = class Floor extends  Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if (not arg?)
      null
    else
      Math.floor arg
 
module.exports.Truncate = class Truncate extends Floor
 
module.exports.Abs = class Abs extends  Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if (not arg?)
      null
    else if arg.isQuantity
      Quantity.createQuantity( Math.abs(arg.value), arg.unit)
    else
      Math.abs arg
 
module.exports.Negate = class Negate extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if (not arg?)
      null
    else if arg.isQuantity
      Quantity.createQuantity(arg.value * -1, arg.unit)
    else
      arg * -1
 
module.exports.Round = class Round extends  Expression
  constructor: (json) ->
    super
    @precision = build json.precision
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if (not arg?)
      null
    else
      dec = if @precision? then @precision.execute(ctx) else 0
      Math.round(arg * Math.pow(10, dec)) / Math.pow(10, dec)
 
module.exports.Ln = class Ln extends  Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if (not arg?)
      null
    else
      Math.log arg
 
module.exports.Exp = class Exp extends  Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if (not arg?)
      null
    else
      Math.exp arg
 
module.exports.Log = class Log extends  Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    args = @execArgs(ctx)
    if (not args? || args.some (x) -> not x?)
      null
    else
      args.reduce (x,y) -> Math.log(x)/Math.log(y)
 
module.exports.Power = class Power extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    args = @execArgs(ctx)
    if (not args? || args.some (x) -> not x?)
      null
    else
      args.reduce (x,y) -> Math.pow(x , y)
 
module.exports.MinValue = class MinValue extends Expression
  MIN_VALUES = {}
  MIN_VALUES['{urn:hl7-org:elm-types:r1}Integer'] = MathUtil.MIN_INT_VALUE
  MIN_VALUES['{urn:hl7-org:elm-types:r1}Decimal'] = MathUtil.MIN_FLOAT_VALUE
  MIN_VALUES['{urn:hl7-org:elm-types:r1}DateTime'] = MathUtil.MIN_DATE_VALUE
  MIN_VALUES['{urn:hl7-org:elm-types:r1}Time'] = MathUtil.MIN_TIME_VALUE
 
  constructor: (json) ->
    super
    @valueType = json.valueType
 
  exec: (ctx) ->
    if MIN_VALUES[@valueType]
      if @valueType == '{urn:hl7-org:elm-types:r1}DateTime'
        minDateTime = MIN_VALUES[@valueType].copy()
        minDateTime.timezoneOffset = ctx.getTimezoneOffset()
        return minDateTime
      else
        MIN_VALUES[@valueType]
    else
      throw new Error("Minimum not supported for #{@valueType}")
 
module.exports.MaxValue = class MaxValue extends Expression
  MAX_VALUES = {}
  MAX_VALUES['{urn:hl7-org:elm-types:r1}Integer'] = MathUtil.MAX_INT_VALUE
  MAX_VALUES['{urn:hl7-org:elm-types:r1}Decimal'] = MathUtil.MAX_FLOAT_VALUE
  MAX_VALUES['{urn:hl7-org:elm-types:r1}DateTime'] = MathUtil.MAX_DATE_VALUE
  MAX_VALUES['{urn:hl7-org:elm-types:r1}Time'] = MathUtil.MAX_TIME_VALUE
 
  constructor: (json) ->
    super
    @valueType = json.valueType
 
  exec: (ctx) ->
    if MAX_VALUES[@valueType]?
      if @valueType == '{urn:hl7-org:elm-types:r1}DateTime'
        maxDateTime = MAX_VALUES[@valueType].copy()
        maxDateTime.timezoneOffset = ctx.getTimezoneOffset()
        return maxDateTime
      else
        MAX_VALUES[@valueType]
    else
      throw new Error("Maximum not supported for #{@valueType}")
 
module.exports.Successor = class Successor extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if (not arg?)
      null
    else
      MathUtil.successor arg
 
module.exports.Predecessor = class Predecessor extends  Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if (not arg?)
      null
    else
      MathUtil.predecessor arg