Skip to content

Commit

Permalink
Fix LARGE, ignore string inputs (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolashefti committed Mar 4, 2024
1 parent 813978b commit 6a093de
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/statistical.js
Original file line number Diff line number Diff line change
Expand Up @@ -1478,13 +1478,19 @@ export function KURT() {
* @returns
*/
export function LARGE(array, k) {
array = utils.parseNumberArray(utils.flatten(array))
k = utils.parseNumber(k)
const someError = utils.anyError.apply(undefined, array)

if (utils.anyIsError(array, k)) {
return array
if (someError) {
return someError
}

if (utils.anyIsError(k)) {
return k
}

array = utils.numbers(utils.flatten(array))
k = utils.parseNumber(k)

if (k < 0 || array.length < k) {
return error.value
}
Expand Down
24 changes: 18 additions & 6 deletions test/statistical.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,12 +570,24 @@ describe('Statistical', () => {
expect(statistical.KURT([3, 4, 5, 2, 'invalid', 4, 5, 6, 4, 7])).to.equal(error.value)
})

it('LARGE', () => {
expect(statistical.LARGE([1, 3, 2, 5, 4], 1)).to.equal(5)
expect(statistical.LARGE([1, 3, 2, 5, 4], 3)).to.equal(3)
expect(statistical.LARGE([3, 5, 3], -3)).to.equal(error.value)
expect(statistical.LARGE([3, 5, 3], 4)).to.equal(error.value)
expect(statistical.LARGE([3, 5, 3, 'invalid', 4], 3)).to.equal(error.value)
describe('LARGE', () => {
it('should return the k-th largest value in a data set', () => {
expect(statistical.LARGE([1, 3, 2, 5, 4], 1)).to.equal(5)
expect(statistical.LARGE([1, 3, 2, 5, 4], 3)).to.equal(3)
})

it('should throw an error in case of malformed parameters', () => {
expect(statistical.LARGE([3, 5, 3], -3)).to.equal(error.value)
expect(statistical.LARGE([3, 5, 3], 4)).to.equal(error.value)
expect(statistical.LARGE([1, 3, error.div0, 5, 4], 3)).to.equal(error.div0)
})

it('should ignore invalid values', () => {
expect(statistical.LARGE([1, 3, undefined, 5, 4], 1)).to.equal(5)
expect(statistical.LARGE([1, 3, null, 5, 4], 1)).to.equal(5)
expect(statistical.LARGE([1, 3, true, 5, 4], 1)).to.equal(5)
expect(statistical.LARGE([1, 3, 'string', 5, 4], 1)).to.equal(5)
})
})

it('LINEST', () => {
Expand Down
7 changes: 7 additions & 0 deletions test/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,11 @@ describe('Utils => Common', () => {
expect(utils.anyIsString(1, 'text')).to.be.true
})
})

describe('numbers', () => {
it('should filter out number inputs', () => {
expect(utils.numbers([1, 'string', 2])).to.deep.equal([1, 2])
expect(utils.numbers([1, error.div0, 2])).to.deep.equal([1, 2])
})
})
})

0 comments on commit 6a093de

Please sign in to comment.