Skip to content

Commit

Permalink
fix: support the sum of negative numbers (#11)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: sum negative numbers

* fix: support the sum of negative numbers 

* test: add test for mixed amounts
  • Loading branch information
ivelazco authored Jun 19, 2020
1 parent 713a236 commit 9ab73d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
25 changes: 3 additions & 22 deletions src/sum-prop.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
27 changes: 14 additions & 13 deletions src/sum-prop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 9ab73d9

Please sign in to comment.