Skip to content
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

Implement dropRepeatsBy (#3041) #3239

Merged
merged 4 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(
(fn, list) => _dispatchable([], _xdropRepeatsWith(eqBy(fn)), dropRepeatsWith(eqBy(fn)))(list)
ivanfilhoz marked this conversation as resolved.
Show resolved Hide resolved
);
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
35 changes: 35 additions & 0 deletions test/dropRepeatsBy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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, n, ...rest }) => ({ i : Math.abs(i), ...rest });

it('removes repeated elements based on predicate', function() {
eq(R.dropRepeatsBy(fn, objs2), objs);
eq(R.dropRepeatsBy(fn, objs), objs);
customcommander marked this conversation as resolved.
Show resolved Hide resolved
});

it('keeps elements from the left', function() {
eq(
R.dropRepeatsBy(fn, [{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);
});

});