From 9ab73d965b3a0d7859bb6e33490d4cb2d94c8f28 Mon Sep 17 00:00:00 2001 From: ignacio 'nacho' velazco Date: Fri, 19 Jun 2020 15:49:55 -0300 Subject: [PATCH] fix: support the sum of negative numbers (#11) BREAKING CHANGE: sum negative numbers * fix: support the sum of negative numbers * test: add test for mixed amounts --- src/sum-prop.js | 25 +++---------------------- src/sum-prop.test.js | 27 ++++++++++++++------------- 2 files changed, 17 insertions(+), 35 deletions(-) diff --git a/src/sum-prop.js b/src/sum-prop.js index 2988bce..7317974 100644 --- a/src/sum-prop.js +++ b/src/sum-prop.js @@ -1,23 +1,10 @@ 'use strict'; -const { - compose, - applyTo, - propOr, - reduce, - pair, - useWith, - identity, - sum, - when, - lt, - always, - gt -} = require('ramda'); +const { compose, applyTo, propOr, reduce, pair, useWith, identity, sum } = require('ramda'); const curryN = require('./curry-n'); const castArray = require('./cast-array'); const { rejectNilOrEmpty } = require('./reject-nil'); -const { MAX_MATH_DELTA, MIN_MATH_DELTA } = require('./utils/_math-constants'); +const round = require('./round'); /** * Sums the values at `propName` present in each element of an array. @@ -37,13 +24,7 @@ function sumProp(precision, propName, values) { return applyTo( values, compose( - // Round to two decimal places - total => - Number.isFinite(total) ? +(Math.round(total + `e+${precision}`) + `e-${precision}`) : total, - // Coerce values higher than `Number.MAX_SAFE_INTEGER` to `Infinity` - when(lt(MAX_MATH_DELTA), always(Infinity)), - // Coerce values lower than `0.0000001` to `0` - when(gt(MIN_MATH_DELTA), always(0)), + round(precision), reduce(useWith(compose(sum, pair), [identity, compose(Number, propOr(0, propName))]), 0), rejectNilOrEmpty, castArray diff --git a/src/sum-prop.test.js b/src/sum-prop.test.js index bf3663d..64697bd 100644 --- a/src/sum-prop.test.js +++ b/src/sum-prop.test.js @@ -32,19 +32,20 @@ test('should ignore `undefined` elements in the array', () => { expect(sumProp(2, 'amount', [{ amount: 30 }, undefined, { amount: 12 }, undefined])).toBe(42); }); -test('should underflow total value to zero if sum is lower than `1e-7`', () => { - expect( - sumProp(1, 'amount', [ - { amount: 85 }, - { amount: -294.21 }, - { amount: 294.21 }, - { amount: -219.5 }, - { amount: -85 }, - { amount: -219.5 } - ]) - ).toBe(0); -}); - test('should overflow result to `Infinity` if sum goes above `MAX_SAFE_INTEGER`', () => { expect(sumProp(1, 'amount', [{ amount: Number.MAX_SAFE_INTEGER }, { amount: 1 }])).toBe(Infinity); }); + +test('should underflow total value to zero if sum is betweem `1e-7` and `-1e-7`', () => { + expect(sumProp(2, 'foo', [{ foo: 0.00000000001 }, { foo: 0.00000000002 }])).toBe(0); + + expect(sumProp(2, 'foo', [{ foo: -0.00000000001 }, { foo: -0.00000000002 }])).toBe(0); +}); + +test('should sum all negative numeric values present at the given property in the array', () => { + expect(sumProp(2, 'amount', [{ amount: -10 }, { amount: -20 }, { amount: -12 }])).toBe(-42); +}); + +test('should sum all numeric values ( negatives and positives ) present at the given property in the array', () => { + expect(sumProp(2, 'amount', [{ amount: -10 }, { amount: 20 }, { amount: -12 }])).toBe(-2); +});