All files / src/elm list.coffee

96.25% Statements 77/80
95.83% Branches 23/24
100% Functions 32/32
96% Lines 72/75

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 17087x 1x 1x 1x 1x   1x   6953x 6953x       1x         1328x   1x   49x     7x     5x   3x                 1x       1x 9x 9x     1x 10x 10x     1x     1x   1x   1531x     32x   32x     1x   507x     25x           1x   90x 90x 90x     10x 10x 2x 8x 5x         1x 167x 61x     1x       1x       1x   1x   4x     2x           1x   25x     5x       1x 33x 33x 202x 177x       33x 33x 33x 164x   5x 5x       1x   1x   69x 69x     24x     1x   49x 49x     7x        
E{ Expression, UnimplementedExpression } = require './expression'
{ ValueSet } = require '../datatypes/datatypes'
{ build } = require './builder'
{ typeIsArray } = require '../util/util'
{ equals } = require '../util/comparison'
 
module.exports.List = class List extends Expression
  constructor: (json) ->
    super
    @elements = (build json.element) ? []
 
  # 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,
    isList:
      get: -> true
 
  exec: (ctx) ->
    (item.execute(ctx) for item in @elements)
 
module.exports.Exists = class Exists extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    list = @execArgs(ctx)
    # if list exists and has non empty length we need to make sure it isnt just full of nulls
    if list?.length > 0
      for item in list
        # return true if we found an item that isnt null.
        return true if item != null
    false
 
 
# Equal is completely handled by overloaded#Equal
 
# NotEqual is completely handled by overloaded#Equal
 
# Delegated to by overloaded#Union
module.exports.doUnion = (a, b) ->
  doDistinct(a.concat b)
 
# Delegated to by overloaded#Except
module.exports.doExcept = (a, b) ->
  setList = doDistinct(a)
  (itm for itm in setList when not doContains(b, itm))
 
# Delegated to by overloaded#Intersect
module.exports.doIntersect = (a, b) ->
  setList = doDistinct(a)
  (itm for itm in setList when doContains(b, itm))
 
# ELM-only, not a product of CQL
module.exports.Times = class Times extends UnimplementedExpression
 
# ELM-only, not a product of CQL
module.exports.Filter = class Filter extends UnimplementedExpression
 
module.exports.SingletonFrom = class SingletonFrom extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs ctx
    if arg? and arg.length > 1 then throw new Error 'IllegalArgument: \'SingletonFrom\' requires a 0 or 1 arg array'
    else if arg? and arg.length is 1 then return arg[0]
    else return null
 
module.exports.ToList = class ToList extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs ctx
    if arg?
      [arg]
    else
      []
 
module.exports.IndexOf = class IndexOf extends Expression
  constructor: (json) ->
    super
    @source = build json.source
    @element = build json.element
 
  exec: (ctx) ->
    src = @source.exec ctx
    el = @element.exec ctx
    if not src? or not el? then return null
    (index = i; break) for itm, i in src when equals itm, el
    if index? then return index else return -1
 
# Indexer is completely handled by overloaded#Indexer
 
# Delegated to by overloaded#Contains and overloaded#In
module.exports.doContains = doContains = (container, item) ->
  return true for element in container when equals element, item
  return false
 
# Delegated to by overloaded#Includes and overloaded@IncludedIn
module.exports.doIncludes = doIncludes = (list, sublist) ->
  sublist.every (x) -> doContains(list, x)
 
# Delegated to by overloaded#ProperIncludes and overloaded@ProperIncludedIn
module.exports.doProperIncludes = (list, sublist) ->
  list.length > sublist.length and doIncludes(list, sublist)
 
# ELM-only, not a product of CQL
module.exports.ForEach = class ForEach extends UnimplementedExpression
 
module.exports.Flatten = class Flatten extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    arg = @execArgs(ctx)
    if typeIsArray(arg) and (arg.every (x) -> typeIsArray x)
      arg.reduce ((x, y) -> x.concat(y)), []
    else
      arg
 
module.exports.Distinct = class Distinct extends Expression
  constructor: (json) ->
    super
 
  exec: (ctx) ->
    result = @execArgs ctx
    if not result? then return null
    doDistinct(result)
 
doDistinct = (list) ->
  seen = []
  list.filter (item) ->
    isNew = seen.every (seenItem) -> !equals(item, seenItem)
    seen.push item if isNew
    isNew
 
  # Remove duplicate null elements
  firstNullFound = false
  setList = []
  for item in seen
    setList.push item if item != null
    if item == null && !firstNullFound
      setList.push item
      firstNullFound = true
  setList
 
# ELM-only, not a product of CQL
module.exports.Current = class Current extends UnimplementedExpression
 
module.exports.First = class First extends Expression
  constructor: (json) ->
    super
    @source = build json.source
 
  exec: (ctx) ->
    src = @source.exec ctx
    if src? and typeIsArray(src) and src.length > 0 then src[0] else null
 
module.exports.Last = class Last extends Expression
  constructor: (json) ->
    super
    @source = build json.source
 
  exec: (ctx) ->
    src = @source.exec ctx
    if src? and typeIsArray(src) and src.length > 0 then src[src.length-1] else null
 
# Length is completely handled by overloaded#Length