arithmetic.js 5.45 KB
Newer Older
jutatip's avatar
jutatip committed
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
import { assert, err, isNil, isNumber, isArray, reduce } from '../../util.js'
import { computeValue } from '../../internal.js'

export const arithmeticOperators = {

  /**
   * Returns the absolute value of a number.
   * https://docs.mongodb.com/manual/reference/operator/aggregation/abs/#exp._S_abs
   * @param obj
   * @param expr
   * @return {Number|null|NaN}
   */
  $abs (obj, expr) {
    let val = computeValue(obj, expr)
    return (val === null || val === undefined) ? null : Math.abs(val)
  },

  /**
   * Computes the sum of an array of numbers.
   *
   * @param obj
   * @param expr
   * @returns {Object}
   */
  $add (obj, expr) {
    let args = computeValue(obj, expr)
    return reduce(args, (acc, num) => acc + num, 0)
  },

  /**
   * Returns the smallest integer greater than or equal to the specified number.
   *
   * @param obj
   * @param expr
   * @returns {number}
   */
  $ceil (obj, expr) {
    let arg = computeValue(obj, expr)
    if (isNaN(arg)) return NaN
    if (isNil(arg)) return null
    assert(isNumber(arg), '$ceil must be a valid expression that resolves to a number.')
    return Math.ceil(arg)
  },

  /**
   * Takes two numbers and divides the first number by the second.
   *
   * @param obj
   * @param expr
   * @returns {number}
   */
  $divide (obj, expr) {
    let args = computeValue(obj, expr)
    return args[0] / args[1]
  },

  /**
   * Raises Euler’s number (i.e. e ) to the specified exponent and returns the result.
   *
   * @param obj
   * @param expr
   * @returns {number}
   */
  $exp (obj, expr) {
    let arg = computeValue(obj, expr)
    if (isNaN(arg)) return NaN
    if (isNil(arg)) return null
    assert(isNumber(arg), '$exp must be a valid expression that resolves to a number.')
    return Math.exp(arg)
  },

  /**
   * Returns the largest integer less than or equal to the specified number.
   *
   * @param obj
   * @param expr
   * @returns {number}
   */
  $floor (obj, expr) {
    let arg = computeValue(obj, expr)
    if (isNaN(arg)) return NaN
    if (isNil(arg)) return null
    assert(isNumber(arg), '$floor must be a valid expression that resolves to a number.')
    return Math.floor(arg)
  },

  /**
   * Calculates the natural logarithm ln (i.e loge) of a number and returns the result as a double.
   *
   * @param obj
   * @param expr
   * @returns {number}
   */
  $ln (obj, expr) {
    let arg = computeValue(obj, expr)
    if (isNaN(arg)) return NaN
    if (isNil(arg)) return null
    assert(isNumber(arg), '$ln must be a valid expression that resolves to a number.')
    return Math.log(arg)
  },

  /**
   * Calculates the log of a number in the specified base and returns the result as a double.
   *
   * @param obj
   * @param expr
   * @returns {number}
   */
  $log (obj, expr) {
    let args = computeValue(obj, expr)
    assert(isArray(args) && args.length === 2, '$log must be a valid expression that resolves to an array of 2 items')
    if (args.some(isNaN)) return NaN
    if (args.some(isNil)) return null
    assert(args.every(isNumber), '$log expression must resolve to array of 2 numbers')
    return Math.log10(args[0]) / Math.log10(args[1])
  },

  /**
   * Calculates the log base 10 of a number and returns the result as a double.
   *
   * @param obj
   * @param expr
   * @returns {number}
   */
  $log10 (obj, expr) {
    let arg = computeValue(obj, expr)
    if (isNaN(arg)) return NaN
    if (isNil(arg)) return null
    assert(isNumber(arg), '$log10 must be a valid expression that resolves to a number.')
    return Math.log10(arg)
  },

  /**
   * Takes two numbers and calculates the modulo of the first number divided by the second.
   *
   * @param obj
   * @param expr
   * @returns {number}
   */
  $mod (obj, expr) {
    let args = computeValue(obj, expr)
    return args[0] % args[1]
  },

  /**
   * Computes the product of an array of numbers.
   *
   * @param obj
   * @param expr
   * @returns {Object}
   */
  $multiply (obj, expr) {
    let args = computeValue(obj, expr)
    return reduce(args, (acc, num) => acc * num, 1)
  },

  /**
   * Raises a number to the specified exponent and returns the result.
   *
   * @param obj
   * @param expr
   * @returns {Object}
   */
  $pow (obj, expr) {
    let args = computeValue(obj, expr)

    assert(isArray(args) && args.length === 2 && args.every(isNumber), '$pow expression must resolve to an array of 2 numbers')
    assert(!(args[0] === 0 && args[1] < 0), '$pow cannot raise 0 to a negative exponent')

    return Math.pow(args[0], args[1])
  },

  /**
   * Calculates the square root of a positive number and returns the result as a double.
   *
   * @param obj
   * @param expr
   * @returns {number}
   */
  $sqrt (obj, expr) {
    let n = computeValue(obj, expr)
    if (isNaN(n)) return NaN
    if (isNil(n)) return null
    assert(isNumber(n) && n > 0, '$sqrt expression must resolve to non-negative number.')
    return Math.sqrt(n)
  },

  /**
   * Takes an array that contains two numbers or two dates and subtracts the second value from the first.
   *
   * @param obj
   * @param expr
   * @returns {number}
   */
  $subtract (obj, expr) {
    let args = computeValue(obj, expr)
    return args[0] - args[1]
  },

  /**
   * Truncates a number to its integer.
   *
   * @param obj
   * @param expr
   * @returns {number}
   */
  $trunc (obj, expr) {
    let n = computeValue(obj, expr)
    if (isNaN(n)) return NaN
    if (isNil(n)) return null
    assert(isNumber(n) && n > 0, '$trunc must be a valid expression that resolves to a non-negative number.')
    return Math.trunc(n)
  }
}