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.
Implement dropRepeatsBy (ramda#3041) (ramda#3239)
* Implement dropRepeatsBy (ramda#3041) * Improve tests for dropRepeatsBy * Remove arrow function from dropRepeatsBy for compatibility purposes * Improve test isolation for dropRepeatsBy and dropRepeatsWith
- Loading branch information
1 parent
3175f72
commit c7aef9a
Showing
4 changed files
with
70 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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import _curry2 from './internal/_curry2.js'; | ||
import _dispatchable from './internal/_dispatchable.js'; | ||
import _xdropRepeatsWith from './internal/_xdropRepeatsWith.js'; | ||
import dropRepeatsWith from './dropRepeatsWith.js'; | ||
import eqBy from './eqBy.js'; | ||
|
||
|
||
/** | ||
* Returns a new list without any consecutively repeating elements, | ||
* based upon the value returned by applying the supplied function to | ||
* each list element. [`R.equals`](#equals) is used to determine equality. | ||
* | ||
* Acts as a transducer if a transformer is given in list position. | ||
* | ||
* @func | ||
* @memberOf R | ||
* @category List | ||
* @sig (a -> b) -> [a] -> [a] | ||
* @param {Function} fn A function used to produce a value to use during comparisons. | ||
* @param {Array} list The array to consider. | ||
* @return {Array} `list` without repeating elements. | ||
* @see R.transduce | ||
* @example | ||
* | ||
* R.dropRepeatsBy(Math.abs, [1, -1, -1, 2, 3, -4, 4, 2, 2]); //=> [1, 2, 3, -4, 2] | ||
*/ | ||
var dropRepeatsBy = _curry2(function(fn, list) { | ||
return _dispatchable([], _xdropRepeatsWith(eqBy(fn)), dropRepeatsWith(eqBy(fn)))(list); | ||
}); | ||
export default dropRepeatsBy; |
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,38 @@ | ||
var R = require('../source/index.js'); | ||
var eq = require('./shared/eq.js'); | ||
|
||
|
||
describe('dropRepeatsBy', function() { | ||
var objs = [ | ||
{i: 1}, {i: 2}, {i: 3}, {i: -4}, {i: 5}, {i: 3} | ||
]; | ||
var objs2 = [ | ||
{i: 1}, {i: -1}, {i: 1}, {i: 2}, {i: 3}, | ||
{i: 3}, {i: -4}, {i: 4}, {i: 5}, {i: 3} | ||
]; | ||
var fn = ({ i }) => ({ i: Math.abs(i) }); | ||
|
||
it('removes repeated elements based on predicate', function() { | ||
eq(R.dropRepeatsBy(fn, objs2), objs); | ||
eq(R.dropRepeatsBy(fn, objs), objs); | ||
}); | ||
|
||
it('keeps elements from the left', function() { | ||
eq( | ||
R.dropRepeatsBy( | ||
({ n, ...rest }) => ({ ...rest }), | ||
[{i: 1, n: 1}, {i: 1, n: 2}, {i: 1, n: 3}, {i: 4, n: 1}, {i: 4, n: 2}] | ||
), | ||
[{i: 1, n: 1}, {i: 4, n: 1}] | ||
); | ||
}); | ||
|
||
it('returns an empty array for an empty array', function() { | ||
eq(R.dropRepeatsBy(fn, []), []); | ||
}); | ||
|
||
it('can act as a transducer', function() { | ||
eq(R.into([], R.dropRepeatsBy(fn), objs2), objs); | ||
}); | ||
|
||
}); |
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