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<TValue> {
+ /**
+ * @see _.sumMultipleFinite
+ */
+ sumMultipleFinite<
+ CollectionKey extends keyof TValue
+ >(
+ this: LoDashImplicitWrapper<TValue | null | undefined>,
+ arrayOfKeys: CollectionKey[]
+ ): LoDashImplicitWrapper<(Number)>
+ }
+
+ interface LoDashExplicitWrapper<TValue> {
+ /**
+ * @see _.sumMultipleFinite
+ */
+ sumMultipleFinite<
+ CollectionKey extends keyof TValue
+ >(
+ this: LoDashExplicitWrapper<TValue | null | undefined>,
+ arrayOfKeys: CollectionKey[]
+ ): LoDashExplicitWrapper<(Number)>
+ }
+}
+
+ Geoblink Lodash Mixins
-Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
- sumMultipleFinite
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
-Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
- sumMultipleFinite
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
-Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
- sumMultipleFinite
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
-Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
- sumMultipleFinite
@@ -2045,6 +2045,163 @@
+
+
+
+
+
+
+
+
Parameters:
+ + + + + + + + + ++ sumMultipleFinite(object, arrayOfKeys) +
+ + + + + + +
+ Returns the `sum` of multiple values from an object, adding just the `finite` ones.
+
+
+
+
+
+
+
+
+ Parameters:
+ + +Name | + + +Type | + + + + + +Description | +
---|---|---|
object |
+
+
+ + + | + + + + + ++ Object to be queried. + + | +
arrayOfKeys |
+
+
+ + + | + + + + + ++ Array with the paths of the properties to get. + + | +
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
- Source: +
-
+
-
+
- + sumMultipleFinite.ts, line 26 + +
+
+
+
+
+
+
+
+
Geoblink Lodash Mixins
-Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
- sumMultipleFinite
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
-Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
- sumMultipleFinite
@@ -93,7 +93,7 @@
Global
- SORTI
-
+
@geoblink/lodash-mixins
A collection of functionalities to extend lodash library.
Installation
Using NPM:
npm i --save @geoblink/lodash-mixins
Or yarn:
diff --git a/docs/index.ts.html b/docs/index.ts.html index 1b6d663..3fa1156 100644 --- a/docs/index.ts.html +++ b/docs/index.ts.html @@ -72,7 +72,7 @@Geoblink Lodash Mixins
-Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
- sumMultipleFinite
@@ -96,6 +96,7 @@import mergeJoinWith from './mergeJoinWith' import mGet from './mGet' import shortcuttedReduce from './shortcuttedReduce' +import sumMultipleFinite from './sumMultipleFinite' import { LoDashStatic } from 'lodash' /** @@ -113,7 +114,8 @@
mergeForEach: mergeForEach, mergeJoinWith: mergeJoinWith, mGet: mGet, - shortcuttedReduce: shortcuttedReduce + shortcuttedReduce: shortcuttedReduce, + sumMultipleFinite: sumMultipleFinite }) return _ @@ -128,7 +130,8 @@
mergeForEach, mergeJoinWith, mGet, - shortcuttedReduce + shortcuttedReduce, + sumMultipleFinite } diff --git a/docs/mGet.ts.html b/docs/mGet.ts.html index 9f43e42..c5d5d07 100644 --- a/docs/mGet.ts.html +++ b/docs/mGet.ts.html @@ -72,7 +72,7 @@
Geoblink Lodash Mixins
-Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
- sumMultipleFinite
diff --git a/docs/mapNonNil.ts.html b/docs/mapNonNil.ts.html index f117588..4725e9e 100644 --- a/docs/mapNonNil.ts.html +++ b/docs/mapNonNil.ts.html @@ -72,7 +72,7 @@Geoblink Lodash Mixins
-Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
- sumMultipleFinite
diff --git a/docs/mergeForEach.ts.html b/docs/mergeForEach.ts.html index 58b6add..450a463 100644 --- a/docs/mergeForEach.ts.html +++ b/docs/mergeForEach.ts.html @@ -72,7 +72,7 @@Geoblink Lodash Mixins
-Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
- sumMultipleFinite
diff --git a/docs/mergeJoinWith.ts.html b/docs/mergeJoinWith.ts.html index e7b028b..c91fff3 100644 --- a/docs/mergeJoinWith.ts.html +++ b/docs/mergeJoinWith.ts.html @@ -72,7 +72,7 @@Geoblink Lodash Mixins
-Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
- sumMultipleFinite
diff --git a/docs/shortcuttedReduce.ts.html b/docs/shortcuttedReduce.ts.html index ec28429..8e59be8 100644 --- a/docs/shortcuttedReduce.ts.html +++ b/docs/shortcuttedReduce.ts.html @@ -72,7 +72,7 @@Geoblink Lodash Mixins
-Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
Global
- SORTING_ORDER
- fromPairsMap
- fromPairsMapNonNil
- getTruthyKeys
- hasTruthyValues
- mGet
- mapNonNil
- mergeForEach
- mergeJoinWith
- shortcuttedReduce
- sumMultipleFinite
diff --git a/docs/sumMultipleFinite.ts.html b/docs/sumMultipleFinite.ts.html new file mode 100644 index 0000000..0d553f6 --- /dev/null +++ b/docs/sumMultipleFinite.ts.html @@ -0,0 +1,176 @@ + + + + + + + + + ++ sumMultipleFinite.ts - Documentation + + + + + + + + + + + + + + + + + + + + + + + + + + + +++ + + +-
+
- + + + + + + + + + + +
+ ++ ++ sumMultipleFinite.ts +
+ + + + + ++ + + + + ++ +
+ + + + + + + + + + diff --git a/package.json b/package.json index 390841e..e9e9488 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@geoblink/lodash-mixins", - "version": "1.3.0", + "version": "1.4.0", "description": "A collection of useful functions built on top of Lodash", "main": "dist/index.js", "files": [ 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..3ac9e01 --- /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..e9b5906 --- /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 only requested values when all values are finite', 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) + }) +})