All files / src/elm literal.coffee

100% Statements 23/23
71.43% Branches 5/7
93.75% Functions 15/16
100% Lines 22/22

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 7747x   1x 1x 41570x               41570x 41570x 41570x             1x   210x 210x       1x             1x   32848x 32848x       1x             1x   1807x 1807x       1x             1x   6705x       1x              
E{ Expression } = require './expression'
 
module.exports.Literal = class Literal extends Expression
  @from: (json) ->
    switch(json.valueType)
      when "{urn:hl7-org:elm-types:r1}Boolean" then new BooleanLiteral(json)
      when "{urn:hl7-org:elm-types:r1}Integer" then new IntegerLiteral(json)
      when "{urn:hl7-org:elm-types:r1}Decimal" then new DecimalLiteral(json)
      when "{urn:hl7-org:elm-types:r1}String" then new StringLiteral(json)
      else new Literal(json)
 
  constructor: (json) ->
    super
    @valueType = json.valueType
    @value = json.value
 
  exec: (ctx) ->
    @value
 
# The following are not defined in ELM, but helpful for execution
 
module.exports.BooleanLiteral = class BooleanLiteral extends Literal
  constructor: (json) ->
    super
    @value = @value is 'true'
 
  # 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,
    isBooleanLiteral:
      get: -> true
 
  exec: (ctx) ->
    @value
 
module.exports.IntegerLiteral = class IntegerLiteral extends Literal
  constructor: (json) ->
    super
    @value = parseInt(@value, 10)
 
  # 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,
    isIntegerLiteral:
      get: -> true
 
  exec: (ctx) ->
    @value
 
module.exports.DecimalLiteral = class DecimalLiteral extends Literal
  constructor: (json) ->
    super
    @value = parseFloat(@value)
 
  # 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,
    isDecimalLiteral:
      get: -> true
 
  exec: (ctx) ->
    @value
 
module.exports.StringLiteral = class StringLiteral extends Literal
  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,
    isStringLiteral:
      get: -> true
 
  exec: (ctx) ->
    # TODO: Remove these replacements when CQL-to-ELM fixes bug: https://github.com/cqframework/clinical_quality_language/issues/82
    @value.replace(/\\'/g, "'").replace(/\\"/g, "\"")