From 133440f6d3e8e52822af9510c674b1b140da841a Mon Sep 17 00:00:00 2001 From: Jose Luis Pillado Date: Fri, 14 Jun 2019 18:18:54 +0200 Subject: [PATCH 1/3] :sparkles: Create sumMultipleFinite method --- src/index.ts | 7 ++- src/sumMultipleFinite.ts | 65 ++++++++++++++++++++++++++ test/exportedMethods.ts | 3 +- test/unit/sumMultipleFinite.spec.ts | 71 +++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 src/sumMultipleFinite.ts create mode 100644 test/unit/sumMultipleFinite.spec.ts diff --git a/src/index.ts b/src/index.ts index 187829c..35f317b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ import mergeForEach from './mergeForEach' import mergeJoinWith from './mergeJoinWith' import mGet from './mGet' import shortcuttedReduce from './shortcuttedReduce' +import sumMultipleFinite from './sumMultipleFinite' import { LoDashStatic } from 'lodash' /** @@ -24,7 +25,8 @@ export default function (_: LoDashStatic) { mergeForEach: mergeForEach, mergeJoinWith: mergeJoinWith, mGet: mGet, - shortcuttedReduce: shortcuttedReduce + shortcuttedReduce: shortcuttedReduce, + sumMultipleFinite: sumMultipleFinite }) return _ @@ -39,5 +41,6 @@ export { mergeForEach, mergeJoinWith, mGet, - shortcuttedReduce + shortcuttedReduce, + sumMultipleFinite } diff --git a/src/sumMultipleFinite.ts b/src/sumMultipleFinite.ts new file mode 100644 index 0000000..862ea91 --- /dev/null +++ b/src/sumMultipleFinite.ts @@ -0,0 +1,65 @@ +import * as _ from 'lodash' + +import mGet from './mGet' + +export default sumMultipleFinite + +/** + * Returns the `sum` of multiple values from an object, adding just the `finite` ones. + * + * @param object Object to be queried. + * @param arrayOfKeys Array with the paths of the properties to get. + * @return Number with the sum of the values for the given key paths. + */ +export function sumMultipleFinite < + Collection extends object, + CollectionKey extends keyof Collection +> ( + object: Collection, + arrayOfKeys: CollectionKey[] +): (Number) { + return _.sum(_.filter(mGet(object, arrayOfKeys), _.isFinite)) +} + +declare module 'lodash' { + interface LoDashStatic { + /** + * Returns the `sum` of multiple values from an object, adding just the `finite` ones. + * + * @param object Object to be queried. + * @param arrayOfKeys Array with the paths of the properties to get. + * @return Number with the sum of the values for the given key paths. + */ + sumMultipleFinite< + Collection extends object, + CollectionKey extends keyof Collection + > ( + object: Collection, + arrayOfKeys: CollectionKey[] + ): (Number) + } + + interface LoDashImplicitWrapper { + /** + * @see _.sumMultipleFinite + */ + sumMultipleFinite< + CollectionKey extends keyof TValue + >( + this: LoDashImplicitWrapper, + arrayOfKeys: CollectionKey[] + ): LoDashImplicitWrapper<(Number)> + } + + interface LoDashExplicitWrapper { + /** + * @see _.sumMultipleFinite + */ + sumMultipleFinite< + CollectionKey extends keyof TValue + >( + this: LoDashExplicitWrapper, + arrayOfKeys: CollectionKey[] + ): LoDashExplicitWrapper<(Number)> + } +} diff --git a/test/exportedMethods.ts b/test/exportedMethods.ts index a7e6359..1957f08 100644 --- a/test/exportedMethods.ts +++ b/test/exportedMethods.ts @@ -7,5 +7,6 @@ export default [ 'mergeForEach', 'mergeJoinWith', 'mGet', - 'shortcuttedReduce' + 'shortcuttedReduce', + 'sumMultipleFinite' ] diff --git a/test/unit/sumMultipleFinite.spec.ts b/test/unit/sumMultipleFinite.spec.ts new file mode 100644 index 0000000..3d851f4 --- /dev/null +++ b/test/unit/sumMultipleFinite.spec.ts @@ -0,0 +1,71 @@ +import sumMultipleFinite from '../../src/sumMultipleFinite' +import { expect } from 'chai' + +describe('sumMultipleFinite', function () { + it('should sum correctly one value', function () { + const object = { + a: 1 + } + const result = sumMultipleFinite(object, ['a']) + expect(result).to.equal(1) + }) + + it('should sum correctly all existing values', function () { + const object = { + a: 1, + b: 2, + c: 5 + } + + const result = sumMultipleFinite(object, ['b', 'a']) + expect(result).to.equal(3) + }) + + it('should sum correctly with negative values', function () { + const object = { + a: 1, + b: 2, + c: -5 + } + + const result = sumMultipleFinite(object, ['b', 'a', 'c']) + expect(result).to.equal(-2) + }) + + it('should ignore not finite values', function () { + const object = { + a: 1, + b: NaN, + c: 5, + d: null, + e: undefined, + f: '', + g: '44' + } + + const result = sumMultipleFinite(object, ['a', 'b', 'c', 'd', 'e', 'f', 'g']) + expect(result).to.equal(6) + }) + + it('should return 0 when all values are not finite', function () { + const object = { + b: NaN, + d: null, + e: undefined + } + + const result = sumMultipleFinite(object, ['b', 'd', 'e']) + expect(result).to.equal(0) + }) + + it('should return 0 when no keys are provided', function () { + const object = { + b: NaN, + d: null, + e: undefined + } + + const result = sumMultipleFinite(object, []) + expect(result).to.equal(0) + }) +}) From d0dd483c79dfd20678c28fc5a129f7cf3b8f947c Mon Sep 17 00:00:00 2001 From: Jose Luis Pillado Date: Mon, 17 Jun 2019 11:07:54 +0200 Subject: [PATCH 2/3] :ok_hand: Update test description --- src/sumMultipleFinite.ts | 2 +- test/unit/sumMultipleFinite.spec.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sumMultipleFinite.ts b/src/sumMultipleFinite.ts index 862ea91..3ac9e01 100644 --- a/src/sumMultipleFinite.ts +++ b/src/sumMultipleFinite.ts @@ -36,7 +36,7 @@ declare module 'lodash' { > ( object: Collection, arrayOfKeys: CollectionKey[] - ): (Number) + ): Number } interface LoDashImplicitWrapper { diff --git a/test/unit/sumMultipleFinite.spec.ts b/test/unit/sumMultipleFinite.spec.ts index 3d851f4..e9b5906 100644 --- a/test/unit/sumMultipleFinite.spec.ts +++ b/test/unit/sumMultipleFinite.spec.ts @@ -10,7 +10,7 @@ describe('sumMultipleFinite', function () { expect(result).to.equal(1) }) - it('should sum correctly all existing values', function () { + it('should sum only requested values when all values are finite', function () { const object = { a: 1, b: 2, From ebb5883b649148d8ac7ee16d1cefe389384d7b16 Mon Sep 17 00:00:00 2001 From: Jose Luis Pillado Date: Mon, 17 Jun 2019 11:16:20 +0200 Subject: [PATCH 3/3] 1.4.0 --- docs/constants.ts.html | 2 +- docs/fromPairsMap.ts.html | 2 +- docs/fromPairsMapNonNil.ts.html | 2 +- docs/getTruthyKeys.ts.html | 2 +- docs/global.html | 159 ++++++++++++++++++++++++++++- docs/hasTruthyValues.ts.html | 2 +- docs/index.html | 4 +- docs/index.ts.html | 9 +- docs/mGet.ts.html | 2 +- docs/mapNonNil.ts.html | 2 +- docs/mergeForEach.ts.html | 2 +- docs/mergeJoinWith.ts.html | 2 +- docs/shortcuttedReduce.ts.html | 2 +- docs/sumMultipleFinite.ts.html | 176 ++++++++++++++++++++++++++++++++ package.json | 2 +- 15 files changed, 353 insertions(+), 17 deletions(-) create mode 100644 docs/sumMultipleFinite.ts.html diff --git a/docs/constants.ts.html b/docs/constants.ts.html index 2ed7bda..c9f2d6c 100644 --- a/docs/constants.ts.html +++ b/docs/constants.ts.html @@ -72,7 +72,7 @@

Geoblink Lodash Mixins

- +
diff --git a/docs/fromPairsMap.ts.html b/docs/fromPairsMap.ts.html index a58944f..a0e8dd1 100644 --- a/docs/fromPairsMap.ts.html +++ b/docs/fromPairsMap.ts.html @@ -72,7 +72,7 @@

Geoblink Lodash Mixins

- +
diff --git a/docs/fromPairsMapNonNil.ts.html b/docs/fromPairsMapNonNil.ts.html index 5ac8e91..9a81284 100644 --- a/docs/fromPairsMapNonNil.ts.html +++ b/docs/fromPairsMapNonNil.ts.html @@ -72,7 +72,7 @@

Geoblink Lodash Mixins

- +
diff --git a/docs/getTruthyKeys.ts.html b/docs/getTruthyKeys.ts.html index 2ac8d73..01cfe6f 100644 --- a/docs/getTruthyKeys.ts.html +++ b/docs/getTruthyKeys.ts.html @@ -72,7 +72,7 @@

Geoblink Lodash Mixins

- +
diff --git a/docs/global.html b/docs/global.html index 6b966ab..e7a6c68 100644 --- a/docs/global.html +++ b/docs/global.html @@ -70,7 +70,7 @@

Geoblink Lodash Mixins

- +
@@ -2045,6 +2045,163 @@
Parameters:
+ + + + + + + + + + + +

+ sumMultipleFinite(object, arrayOfKeys) +

+
+ + + + + +
+ Returns the `sum` of multiple values from an object, adding just the `finite` ones. +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
object + + + Object to be queried. + +
arrayOfKeys + + + Array with the paths of the properties to get. + +
+ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + diff --git a/docs/hasTruthyValues.ts.html b/docs/hasTruthyValues.ts.html index 071ec5f..7b76235 100644 --- a/docs/hasTruthyValues.ts.html +++ b/docs/hasTruthyValues.ts.html @@ -72,7 +72,7 @@

Geoblink Lodash Mixins

- +
diff --git a/docs/index.html b/docs/index.html index b8a7916..1f797ad 100644 --- a/docs/index.html +++ b/docs/index.html @@ -70,7 +70,7 @@

Geoblink Lodash Mixins

- +
@@ -93,7 +93,7 @@