Skip to content

Commit

Permalink
Merge 133440f into e670493
Browse files Browse the repository at this point in the history
  • Loading branch information
fofi committed Jun 14, 2019
2 parents e670493 + 133440f commit d673c20
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/**
Expand All @@ -24,7 +25,8 @@ export default function (_: LoDashStatic) {
mergeForEach: mergeForEach,
mergeJoinWith: mergeJoinWith,
mGet: mGet,
shortcuttedReduce: shortcuttedReduce
shortcuttedReduce: shortcuttedReduce,
sumMultipleFinite: sumMultipleFinite
})

return _
Expand All @@ -39,5 +41,6 @@ export {
mergeForEach,
mergeJoinWith,
mGet,
shortcuttedReduce
shortcuttedReduce,
sumMultipleFinite
}
65 changes: 65 additions & 0 deletions src/sumMultipleFinite.ts
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)>
}
}
3 changes: 2 additions & 1 deletion test/exportedMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export default [
'mergeForEach',
'mergeJoinWith',
'mGet',
'shortcuttedReduce'
'shortcuttedReduce',
'sumMultipleFinite'
]
71 changes: 71 additions & 0 deletions test/unit/sumMultipleFinite.spec.ts
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)
})
})

0 comments on commit d673c20

Please sign in to comment.