Skip to content

Commit

Permalink
issue-#57, #81 - Update 'drop', and 'dropWhile' to just return 'T[]'.
Browse files Browse the repository at this point in the history
  • Loading branch information
elycruz committed Feb 26, 2024
1 parent 2e0b5ae commit 6be59fb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 46 deletions.
12 changes: 3 additions & 9 deletions packages/fjl/src/list/drop.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import {instanceOfSome} from "../object";

export const

/**
* Drops `n` items from start of iterable and returns a corresponding list (
* string (if iterable is a string), or array).
* Drops `n` items from start of iterable and returns an array containing the non-dropped items.
*/
drop = <T = any>(n: number, xs: Iterable<T>): typeof xs | T[] => {
if (instanceOfSome(xs, String, Array))
return (xs as (string | T[])).slice(n) as typeof xs;

drop = <T = any>(n: number, xs: Iterable<T>): T[] => {
const out = [] as T[];
for (const x of xs) {
if (n-- > 0) continue;
Expand All @@ -24,6 +18,6 @@ export const
* @curried
*/
$drop = <T = any>(n: number) =>
(xs: Iterable<T>): typeof xs | T[] => drop(n, xs)
(xs: Iterable<T>): T[] => drop(n, xs)

;
8 changes: 3 additions & 5 deletions packages/fjl/src/list/dropWhile.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {TernaryPred} from "../types";
import {instanceOf} from "../_platform";

export const

/**
* Drops elements, from start of iterable, fulfilling predicate, and returns
* a list (string if iterable is a string) of the remaining items.
*/
dropWhile = <T>(p: TernaryPred, xs: Iterable<T>): typeof xs | T[] => {
dropWhile = <T>(p: TernaryPred, xs: Iterable<T>): T[] => {
let index = -1,
thresholdReached = false;

Expand All @@ -21,14 +20,13 @@ export const
out.push(x);
}

return instanceOf(xs, String) ? (out.join("") as Iterable<T>) : out;
return out;
},

/**
* Curried version of `dropWhile`.
*/
$dropWhile = <T>(p: TernaryPred) =>
(xs: Iterable<T>): typeof xs | T[] =>
dropWhile(p, xs)
(xs: Iterable<T>): T[] => dropWhile(p, xs)

;
6 changes: 6 additions & 0 deletions packages/fjl/src/list/dropWhileEnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {negateF3} from "../function";

export const

/**
* Drops items off end of list while predicate holds.
*/
dropWhileEnd = <T>(
pred: TernaryPred,
xs: string | T[]
Expand All @@ -16,6 +19,9 @@ export const
return xs.slice(0, splitPoint + 1);
},

/**
* Curried version of `dropWhileEnd`.
*/
$dropWhileEnd = <T>(p: TernaryPred) =>
(xs: string | T[]): typeof xs =>
dropWhileEnd(p, xs)
Expand Down
54 changes: 25 additions & 29 deletions packages/fjl/tests/list/test-drop.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
import {Slice} from "../../src/types/data";
import {expectEqual, expectError, vowelsArray, vowelsString} from "../helpers";
import {drop} from "../../src";
import {vowelsArray} from "../helpers";
import {drop, $drop} from "../../src";

describe('#drop', () => {
it('should return a new list/string with dropped items from original until length', () => {
(<[Parameters<typeof drop>, Slice][]>[
[[0, ''], ''],
[[0, []], []],
[[1, ''], ''],
[[1, []], []],
].concat(
vowelsArray
.map((_, ind) => [
[ind, vowelsArray],
vowelsArray.slice(ind)
]),
vowelsString.split('')
.map((_, ind) => [
[ind, vowelsString],
vowelsString.slice(ind)
])
))
.forEach(([args, expected]) => {
expectEqual(drop(...args), expected);
const {stringify} = JSON;

describe('#drop, #$drop', () => {
(<[Parameters<typeof drop>, any[]][]>[
[[0, []], []],
[[1, []], []],
].concat(
vowelsArray
.map((_, ind) => [
[ind, vowelsArray],
vowelsArray.slice(ind)
])
))
.forEach(([args, expected]) => {
it(`drop(${stringify(args)}) === ${stringify(expected)}`, () => {
expect(drop(...args)).toEqual(expected);
expect($drop(args[0])(args[1])).toEqual(expected);
});
});
});

it('should throw an error when no parameter is passed in', () => {
(<any[]>[null, undefined, 0, {}]).forEach(xs =>
expectError(() => drop(3, xs))
);
(<any[]>[null, undefined, 0, {}]).forEach(xs => {
it(`should throw when: drop(3, ${xs + ''})`, () => {
expect(() => drop(3, xs)).toThrow();
expect(() => $drop(3)(xs)).toThrow();
});
});
});
12 changes: 9 additions & 3 deletions packages/fjl/tests/list/test-dropWhile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {UnaryPred} from "../../src/types";

describe('#dropWhile', () => {
const alnumRegex = /^[a-z]$/i,
alnumPred = (x): boolean => alnumRegex.test(x),
nonAlnumPred = (x): boolean => !alnumPred(x),
getCharCodeLessThan = (lessThanCharCode): UnaryPred<any> => {
alnumPred = (x: any): boolean => alnumRegex.test(x),
nonAlnumPred = (x: any): boolean => !alnumPred(x),
getCharCodeLessThan = (lessThanCharCode: any): UnaryPred<any> => {
const methodName = `charCodeLessThan${lessThanCharCode}`;
return {
[methodName]: (x): boolean => (x + '').charCodeAt(0) < lessThanCharCode
Expand All @@ -29,4 +29,10 @@ describe('#dropWhile', () => {
expect(result).toEqual(expected);
});
});

[null, undefined, 0, {}].forEach(x =>
it(`should throw an error when: \`dropWhile(() => null, ${x + ''})\``, () => {
expect(() => dropWhile(() => null, x as Iterable<any>)).toThrow()
})
);
});

0 comments on commit 6be59fb

Please sign in to comment.