Skip to main content
Module

x/fae/groupWith.ts

A functional module for Deno inspired from Ramda.
Latest
File
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import type { PH, Predicate2 } from './utils/types.ts';import curryN from './utils/curry_n.ts';import { slice } from './slice.ts';
// @typestype GroupWith_2<L extends T[] | string, T> = (functor: L) => L[];
type GroupWith_1<L extends T[] | string, T> = (predicate: Predicate2<T>) => L[];
type GroupWith = & (<L extends T[] | string, T>(predicate: Predicate2<T>) => GroupWith_2<L, T>) & (<L extends T[] | string, T>( predicate: PH, functor: L, ) => GroupWith_1<L, T>) & (<L extends T[] | string, T>(predicate: Predicate2<T>, functor: L) => L[]);
function _groupWith<L extends T[] | string, T>( predicate: Predicate2<T | string>, functor: L,) { const result: T[][] | string[] = []; const len = functor.length; let i = 0; while (i < len) { let j = i + 1; while (j < len && predicate(functor[j - 1], functor[j])) j++; result.push(slice(i, j, functor) as T[] & string); i = j; } return result;}
export const groupWith: GroupWith = curryN(2, _groupWith);