Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support the sum of negative numbers #11

Merged
merged 2 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});