All files / src/elm aggregate.coffee

99.21% Statements 125/126
90% Branches 9/10
100% Functions 40/40
99.2% Lines 124/125

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 240127x 1x 1x 1x 1x 1x   1x 44x     3x   41x 41x   26x 26x 26x 133x 26x           1x               1x   866x 866x   1x   12x     6x       1x   45x     9x   9x 9x     1x   202x     14x 1x 13x 2x   11x 11x 11x 11x   1x   195x     13x 1x 12x 2x   10x 10x 9x     1x   42x     6x   6x 1x 5x     1x   140x     10x   10x           5x       1x   16x     4x   4x 4x       4x 4x 4x 4x 19x   4x   8x 8x     1x     100x 100x     19x   19x       19x       19x 19x 19x   19x 95x   19x 19x 19x 19x     1x   81x     9x 1x 8x 2x 6x   1x   25x     5x 1x 4x 1x 3x 3x   1x 12x   12x 3x 9x   7x   19x 9x       1x   25x 25x   1x   25x 25x   1x   25x 25x   1x   4x     4x     1x   4x     4x    
E{ Expression } = require './expression'
{ typeIsArray , allTrue, anyTrue, removeNulls, numerical_sort} = require '../util/util'
{ build } = require './builder'
{ Exception } = require '../datatypes/exception'
{ greaterThan, lessThan } = require '../util/comparison'
Quantity = require './quantity'
 
quantitiesOrArg = (arr) ->
  arr = removeNulls(arr)
  # short curcuit empty arrays and return
  if arr.length == 0
    return arr
 
  allQs = arr.every (x) -> x.isQuantity
  someQs = arr.some (x) -> x.isQuantity
  if allQs
    unit = arr[0].unit
    values = []
    for i in arr
      values.push i.convertUnits(unit)
    return values
  else if someQs
    throw new Exception("Cannot perform aggregate operations on mixed values of Quantities and non Quantities")
  else
    arr
 
quantityOrValue = (value, arr) ->
  # we used the first unit in the list to convert to so that is what
  # we will use as a unit for quantities
  if arr?[0]?.unit
    Quantity.createQuantity(value, arr[0].unit)
  else
    value
 
class AggregateExpression extends Expression
  constructor:(json) ->
    super
    @source = build json.source
 
module.exports.Count = class Count extends AggregateExpression
  constructor:(json) ->
    super
 
  exec: (ctx) ->
    arg = @source.execute(ctx)
    if typeIsArray(arg)
      removeNulls(arg).length
 
module.exports.Sum = class Sum extends AggregateExpression
  constructor:(json) ->
    super
 
  exec: (ctx) ->
    arg = @source.execute(ctx)
    if typeIsArray(arg)
      filtered =  quantitiesOrArg(arg)
      val = if filtered.length == 0 then null else filtered.reduce (x,y) -> x+y
      quantityOrValue(val, arg)
 
module.exports.Min = class Min extends AggregateExpression
  constructor:(json) ->
    super
 
  exec: (ctx) ->
    list = @source.execute(ctx)
    return null unless list?
    listWithoutNulls = removeNulls(list)
    return null unless listWithoutNulls.length > 0
    # We assume the list is an array of all the same type.
    minimum = listWithoutNulls[0]
    for element in listWithoutNulls
      minimum = element if lessThan(element, minimum)
    return minimum
 
module.exports.Max = class Max extends AggregateExpression
  constructor:(json) ->
    super
 
  exec: (ctx) ->
    list = @source.execute(ctx)
    return null unless list?
    listWithoutNulls = removeNulls(list)
    return null unless listWithoutNulls.length > 0
    # We assume the list is an array of all the same type.
    maximum = listWithoutNulls[0]
    for element in listWithoutNulls
      maximum = element if greaterThan(element, maximum)
    maximum
 
module.exports.Avg = class Avg extends  AggregateExpression
  constructor:(json) ->
    super
 
  exec: (ctx) ->
    arg = @source.execute(ctx)
    if typeIsArray(arg)
      filtered = quantitiesOrArg(arg)
      return null if filtered.length == 0
      sum = filtered.reduce (x,y) -> x+y
      quantityOrValue((sum / filtered.length),arg)
 
module.exports.Median = class Median extends AggregateExpression
  constructor:(json) ->
    super
 
  exec: (ctx) ->
    arg = @source.execute(ctx)
    if typeIsArray(arg)
      filtered =  numerical_sort(quantitiesOrArg(arg),"asc")
      if filtered.length == 0
        null
      else if (filtered.length % 2 == 1)
         quantityOrValue(filtered[(filtered.length - 1) / 2],arg)
      else
        v = (filtered[(filtered.length / 2) - 1] +
         filtered[(filtered.length / 2)]) / 2
        quantityOrValue(v,arg)
 
module.exports.Mode = class Mode extends AggregateExpression
  constructor:(json) ->
    super
 
  exec: (ctx) ->
    arg = @source.execute(ctx)
    if typeIsArray(arg)
      filtered = removeNulls(arg)
      mode = @mode(filtered)
      if mode.length == 1 then mode[0] else mode
 
  mode: (arr) ->
    max = 0
    counts = {}
    results = []
    for elem in arr
      cnt = counts[elem] = (counts[elem] ? 0) + 1
      if cnt is max and elem not in results
        results.push elem
      else if cnt > max
        results = [elem]
        max = cnt
    results
 
module.exports.StdDev = class StdDev extends AggregateExpression
 
  constructor:(json) ->
    super
    @type = "standard_deviation"
 
  exec: (ctx) ->
    args = @source.execute(ctx)
    if typeIsArray(args)
      val = quantitiesOrArg(args)
      if val.length > 0 then quantityOrValue(@calculate(val),args)  else null
 
  calculate: (list) ->
    val = @stats(list)
    if val then val[@type]
 
  stats:(list) ->
    sum = list.reduce (x,y) -> x+y
    mean = sum / list.length
    sumOfSquares = 0
 
    for sq in list
      sumOfSquares += Math.pow((sq - mean),2)
 
    std_var = (1/list.length) * sumOfSquares
    pop_var = (1/(list.length-1)) * sumOfSquares
    std_dev = Math.sqrt std_var
    pop_dev = Math.sqrt pop_var
    {standard_variance: std_var, population_variance: pop_var, standard_deviation: std_dev, population_deviation: pop_dev}
 
module.exports.Product = class Product extends AggregateExpression
  constructor:(json) ->
    super
 
  exec: (ctx) ->
    listOfValues = @source.execute(ctx)
    return null if listOfValues is null
    [product, filtered] = productValue(listOfValues)
    return null if product is null
    return quantityOrValue(product, listOfValues)
 
module.exports.GeometricMean = class GeometricMean extends AggregateExpression
  constructor:(json) ->
    super
 
  exec: (ctx) ->
    listOfValues = @source.execute(ctx)
    return null if listOfValues is null
    [product, filtered] = productValue(listOfValues)
    return null if product is null
    geoMean = Math.pow(product, 1.0 / filtered.length)
    return geoMean
 
productValue = (list) ->
  product = 1
  if typeIsArray(list)
    filtered = removeNulls(list)
    return [null, null] if filtered.length == 0
    for item in filtered
      if item.isQuantity
        product = Quantity.doMultiplication(product,item)
      else
        product = product * item
    return [product, filtered]
  else
    [null, null]
 
module.exports.PopulationStdDev = class PopulationStdDev extends StdDev
  constructor:(json) ->
    super
    @type = "population_deviation"
 
module.exports.Variance = class Variance extends  StdDev
  constructor:(json) ->
    super
    @type = "standard_variance"
 
module.exports.PopulationVariance = class PopulationVariance extends  StdDev
  constructor:(json) ->
    super
    @type = "population_variance"
 
module.exports.AllTrue = class AllTrue extends AggregateExpression
  constructor:(json) ->
    super
 
  exec: (ctx) ->
    args =@source.execute(ctx)
    allTrue(args)
 
module.exports.AnyTrue = class AnyTrue extends AggregateExpression
  constructor:(json) ->
    super
 
  exec: (ctx) ->
    args = @source.execute(ctx)
    anyTrue(args)