Skip to content

Commit

Permalink
Implement dropRepeatsBy (ramda#3041) (ramda#3239)
Browse files Browse the repository at this point in the history
* 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
ivanfilhoz authored Feb 23, 2022
1 parent 3175f72 commit c7aef9a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
30 changes: 30 additions & 0 deletions source/dropRepeatsBy.js
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;
1 change: 1 addition & 0 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export { default as drop } from './drop.js';
export { default as dropLast } from './dropLast.js';
export { default as dropLastWhile } from './dropLastWhile.js';
export { default as dropRepeats } from './dropRepeats.js';
export { default as dropRepeatsBy } from './dropRepeatsBy.js';
export { default as dropRepeatsWith } from './dropRepeatsWith.js';
export { default as dropWhile } from './dropWhile.js';
export { default as either } from './either.js';
Expand Down
38 changes: 38 additions & 0 deletions test/dropRepeatsBy.js
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);
});

});
2 changes: 1 addition & 1 deletion test/dropRepeatsWith.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('dropRepeatsWith', function() {
{i: 1}, {i: 1}, {i: 1}, {i: 2}, {i: 3},
{i: 3}, {i: 4}, {i: 4}, {i: 5}, {i: 3}
];
var eqI = R.eqProps('i');
var eqI = (a, b) => a.i === b.i;

it('removes repeated elements based on predicate', function() {
eq(R.dropRepeatsWith(eqI, objs2), objs);
Expand Down

0 comments on commit c7aef9a

Please sign in to comment.