-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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)> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |