-
Notifications
You must be signed in to change notification settings - Fork 7
/
dropRepeatsWith.ts
45 lines (35 loc) · 1.25 KB
/
dropRepeatsWith.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import curryN from './utils/curry_n.ts';
import type { PH, Predicate2 } from './utils/types.ts';
import { dispatch } from './utils/dispatch.ts';
import DropRepeatsWithTransformer from './utils/Transformers/dropRepeatsWith.ts';
// @types
type DropRepeatsWith_2<T> = (list: T[]) => T[];
type DropRepeatsWith_1<T> = (predicate: Predicate2<T>) => T[];
type DropRepeatsWith =
& (<T>(predicate: Predicate2<T>) => DropRepeatsWith_2<T>)
& (<T>(predicate: PH, list: T[]) => DropRepeatsWith_1<T>)
& (<T>(predicate: Predicate2<T>, list: T[]) => T[]);
function _dropRepeatsWith<T>(predicate: Predicate2<T>, list: T[]) {
const result = [];
const len = list.length;
if (len === 0) return [];
result[0] = list[0];
let last = result[0];
for (let i = 1; i < len; i++) {
if (!predicate(last, list[i])) result.push(list[i]);
last = list[i];
}
return result;
}
const dispatched = dispatch(
DropRepeatsWithTransformer,
_dropRepeatsWith,
);
/**
* Returns a new list without consecutively repeating elements.
* Equality is decided by `predicate`
*
* Acts as a transducer if a transformer is given in `list` position.
*/
export const dropRepeatsWith: DropRepeatsWith = curryN(2, dispatched);