-
Notifications
You must be signed in to change notification settings - Fork 14
Add max and min utils #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fdd56ea
Add helper to determine extremum value
ZIMkaRU 0ead97e
Add min util
ZIMkaRU 349ede6
Add test coverage for min util
ZIMkaRU 75ae8cd
Add max util
ZIMkaRU 5ef0d0b
Add test coverage for max util
ZIMkaRU 9bee177
Update readme
ZIMkaRU 6844b43
Bump version
ZIMkaRU dc68392
Update changelog
ZIMkaRU 1b03df8
Fix max util name
ZIMkaRU 135805e
Add extremum value test case for nil
ZIMkaRU 701ecc6
Add extremum value test case for string
ZIMkaRU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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') | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems, this condition is not covered by tests