forked from ramda/ramda
-
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.
Add ascendNatural and descendNatural functions (ramda#3442)
- Loading branch information
Showing
7 changed files
with
116 additions
and
2 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,37 @@ | ||
import curry from './curry.js'; | ||
|
||
|
||
/** | ||
* Makes an ascending comparator function out of a function that returns a value | ||
* that can be compared with natural sorting using localeCompare. | ||
* | ||
* @func | ||
* @memberOf R | ||
* @since v0.30.1 | ||
* @category Function | ||
* @sig Ord b => s -> (a -> b) -> a -> a -> Number | ||
* @param {String|Array} locales A string with a BCP 47 language tag, or an array of such strings. Corresponds to the locales parameter of the Intl.Collator() constructor. | ||
* @param {Function} fn A function of arity one that returns a value that can be compared | ||
* @param {*} a The first item to be compared. | ||
* @param {*} b The second item to be compared. | ||
* @return {Number} `-1` if a occurs before b, `1` if a occurs after b, otherwise `0` | ||
* @see R.ascend | ||
* @example | ||
* | ||
* const unsorted = ['3', '1', '10', 'Ørjan', 'Bob', 'Älva']; | ||
* | ||
* R.sort(R.ascendNatural('en', R.identity), unsorted); | ||
* // => ['1', '3', '10', 'Älva', 'Bob', 'Ørjan'] | ||
* | ||
* R.sort(R.ascendNatural('sv', R.identity), unsorted); | ||
* // => ['1', '3', '10', 'Bob', 'Älva', 'Ørjan'] | ||
* | ||
* R.sort(R.ascend(R.identity), unsorted); | ||
* // => ['1', '10', '3', 'Bob', 'Älva', 'Ørjan'] | ||
*/ | ||
var ascendNatural = curry(function ascendNatural(locales, fn, a, b) { | ||
const aa = fn(a); | ||
const bb = fn(b); | ||
return aa.localeCompare(bb, locales, { numeric: true }); | ||
}); | ||
export default ascendNatural; |
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,37 @@ | ||
import curry from './curry.js'; | ||
|
||
|
||
/** | ||
* Makes a descending comparator function out of a function that returns a value | ||
* that can be compared with natural sorting using localeCompare. | ||
* | ||
* @func | ||
* @memberOf R | ||
* @since v0.30.1 | ||
* @category Function | ||
* @sig Ord b => s -> (a -> b) -> a -> a -> Number | ||
* @param {String|Array} locales A string with a BCP 47 language tag, or an array of such strings. Corresponds to the locales parameter of the Intl.Collator() constructor. | ||
* @param {Function} fn A function of arity one that returns a value that can be compared | ||
* @param {*} a The first item to be compared. | ||
* @param {*} b The second item to be compared. | ||
* @return {Number} `-1` if a occurs after b, `1` if a occurs before b, otherwise `0` | ||
* @see R.descend | ||
* @example | ||
* | ||
* const unsorted = ['3', '1', '10', 'Ørjan', 'Bob', 'Älva']; | ||
* | ||
* R.sort(R.descendNatural('en', R.identity), unsorted); | ||
* // => ['Ørjan', 'Bob', 'Älva', '10', '3', '1'] | ||
* | ||
* R.sort(R.descendNatural('sv', R.identity), unsorted); | ||
* // => ['Ørjan', 'Älva', 'Bob', '10', '3', '1'] | ||
* | ||
* R.sort(R.descend(R.identity), unsorted); | ||
* // => ['Ørjan', 'Älva', 'Bob', '3', '10', '1'] | ||
*/ | ||
var descendNatural = curry(function descendNatural(locales, fn, a, b) { | ||
const aa = fn(a); | ||
const bb = fn(b); | ||
return bb.localeCompare(aa, locales, { numeric: true }); | ||
}); | ||
export default descendNatural; |
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,19 @@ | ||
var R = require('../source/index.js'); | ||
var eq = require('./shared/eq.js'); | ||
|
||
describe('ascendNatural', function() { | ||
it('builds an ascending natural comparator function out of the identity function', function() { | ||
eq(['3', '1', '10', 'Ørjan', 'Bob', 'Älva'].sort(R.ascendNatural('en', R.identity)), | ||
['1', '3', '10', 'Älva', 'Bob', 'Ørjan'] | ||
); | ||
eq(['3', '1', '10', 'Ørjan', 'Bob', 'Älva'].sort(R.ascendNatural('fr', R.identity)), | ||
['1', '3', '10', 'Älva', 'Bob', 'Ørjan'] | ||
); | ||
eq(['3', '1', '10', 'Ørjan', 'Bob', 'Älva'].sort(R.ascendNatural('de', R.identity)), | ||
['1', '3', '10', 'Älva', 'Bob', 'Ørjan'] | ||
); | ||
eq(['3', '1', '10', 'Ørjan', 'Bob', 'Älva'].sort(R.ascendNatural('sv', R.identity)), | ||
['1', '3', '10', 'Bob', 'Älva', 'Ørjan'] | ||
); | ||
}); | ||
}); |
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,19 @@ | ||
var R = require('../source/index.js'); | ||
var eq = require('./shared/eq.js'); | ||
|
||
describe('descendNatural', function() { | ||
it('builds a descending natural comparator function out of the identity function', function() { | ||
eq(['3', '1', '10', 'Ørjan', 'Bob', 'Älva'].sort(R.descendNatural('en', R.identity)), | ||
['Ørjan', 'Bob', 'Älva', '10', '3', '1'] | ||
); | ||
eq(['3', '1', '10', 'Ørjan', 'Bob', 'Älva'].sort(R.descendNatural('fr', R.identity)), | ||
['Ørjan', 'Bob', 'Älva', '10', '3', '1'] | ||
); | ||
eq(['3', '1', '10', 'Ørjan', 'Bob', 'Älva'].sort(R.descendNatural('de', R.identity)), | ||
['Ørjan', 'Bob', 'Älva', '10', '3', '1'] | ||
); | ||
eq(['3', '1', '10', 'Ørjan', 'Bob', 'Älva'].sort(R.descendNatural('sv', R.identity)), | ||
['Ørjan', 'Älva', 'Bob', '10', '3', '1'] | ||
); | ||
}); | ||
}); |