-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from ZIMkaRU/feature/add-min-max-utils
Add max and min utils
- Loading branch information
Showing
10 changed files
with
138 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
# 1.8.0 | ||
- feat: max | ||
- feat: min | ||
|
||
# 1.7.0 | ||
- feat: freezeDeep | ||
|
||
|
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
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
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,12 @@ | ||
'use strict' | ||
|
||
const determineExtremumValue = require('./util/determineExtremumValue') | ||
|
||
const max = (array) => { | ||
return determineExtremumValue( | ||
array, | ||
(value, other) => value > other | ||
) | ||
} | ||
|
||
module.exports = max |
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,12 @@ | ||
'use strict' | ||
|
||
const determineExtremumValue = require('./util/determineExtremumValue') | ||
|
||
const min = (array) => { | ||
return determineExtremumValue( | ||
array, | ||
(value, other) => value < other | ||
) | ||
} | ||
|
||
module.exports = min |
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,29 @@ | ||
'use strict' | ||
|
||
const isNil = require('../isNil') | ||
|
||
const determineExtremumValue = (array, comparator) => { | ||
if ( | ||
!Array.isArray(array) || | ||
array.length === 0 | ||
) { | ||
return | ||
} | ||
|
||
return array.reduce((prev, curr) => { | ||
if (isNil(curr)) { | ||
return prev | ||
} | ||
if ( | ||
prev === undefined && | ||
!Number.isNaN(curr) && | ||
typeof curr !== 'symbol' | ||
) { | ||
return curr | ||
} | ||
|
||
return comparator(curr, prev) ? curr : prev | ||
}, undefined) | ||
} | ||
|
||
module.exports = determineExtremumValue |
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,36 @@ | ||
'use strict' | ||
|
||
/* eslint-env mocha */ | ||
|
||
const assert = require('assert') | ||
const max = require('../src/max') | ||
|
||
describe('max', () => { | ||
it('should return the largest value from a collection', () => { | ||
const actual = max([4, null, 2, 8, 6]) | ||
|
||
assert.strictEqual(actual, 8) | ||
}) | ||
|
||
it('should return `undefined` without arguments', () => { | ||
const actual = max() | ||
|
||
assert.strictEqual(actual, undefined) | ||
}) | ||
|
||
it('should return `undefined` for empty collections', () => { | ||
const sources = [null, undefined, false, 0, NaN, '', Symbol('a'), []] | ||
|
||
for (const source of sources) { | ||
const actual = max(source) | ||
|
||
assert.strictEqual(actual, undefined) | ||
} | ||
}) | ||
|
||
it('should return the largest value from a string collection', () => { | ||
const actual = max(['cd', null, 'ab', 'gh', 'ef']) | ||
|
||
assert.strictEqual(actual, 'gh') | ||
}) | ||
}) |
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,36 @@ | ||
'use strict' | ||
|
||
/* eslint-env mocha */ | ||
|
||
const assert = require('assert') | ||
const min = require('../src/min') | ||
|
||
describe('min', () => { | ||
it('should return the smallest value from a collection', () => { | ||
const actual = min([4, null, 2, 8, 6]) | ||
|
||
assert.strictEqual(actual, 2) | ||
}) | ||
|
||
it('should return `undefined` without arguments', () => { | ||
const actual = min() | ||
|
||
assert.strictEqual(actual, undefined) | ||
}) | ||
|
||
it('should return `undefined` for empty collections', () => { | ||
const sources = [null, undefined, false, 0, NaN, '', Symbol('a'), []] | ||
|
||
for (const source of sources) { | ||
const actual = min(source) | ||
|
||
assert.strictEqual(actual, undefined) | ||
} | ||
}) | ||
|
||
it('should return the smallest value from a string collection', () => { | ||
const actual = min(['cd', null, 'ab', 'gh', 'ef']) | ||
|
||
assert.strictEqual(actual, 'ab') | ||
}) | ||
}) |