Skip to content

Commit

Permalink
issue-#57, #87 - Updated 'drop' method to support 'Iterable<T>', and …
Browse files Browse the repository at this point in the history
…updated some docs.
  • Loading branch information
elycruz committed Feb 25, 2024
1 parent 9d91a2c commit 2740ac1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/fjl/src/list/append.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ export const
/**
* Append two, or more, lists, i.e.,
* ```
* expectEqual(append(take(13, alphabetString), drop(13, alphabetString)), alphabetString); // true
* expect(append(take(13, alphabetString), drop(13, alphabetString))).toEqual(alphabetString); // true
*
* // Another example
* const result = append(
* alphabetStr.split(''),
* alphabetStr.split('')
* ),
* expected = repeat(2, alphabetStr).split('');
* expected = take(2, repeat(alphabetStr));
*
* shallowEquals(result, expected) === true // `true`
* ```
Expand Down
28 changes: 24 additions & 4 deletions packages/fjl/src/list/drop.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import {$sliceFrom, sliceFrom} from "./utils/sliceFrom";
import {Slice} from "../types";
import {instanceOfSome} from "../object";

export const

$drop = $sliceFrom,
/**
* Drops `n` items from start of list.
*
* **Note:** Returns `string` type for strings, and `Array` type otherwise.
*/
drop = <T = any>(n: number, xs: Slice<T> | Iterable<T>): typeof xs | T[] => {
if (instanceOfSome(xs, String, Array))
return (xs as (string | T[])).slice(n);

const out = [] as T[];
for (const x of xs) {
if (n-- > 0) continue;
out.push(x as T);
}
return out;
},

/**
* Drops `n` items from start of list to `count` (exclusive).
* Curried version of `drop`.
*
* @curried
*/
drop = sliceFrom;
$drop = <T = any>(n: number) =>
(xs: Slice<T> | Iterable<T>): typeof xs | T[] => drop(n, xs)

;
11 changes: 6 additions & 5 deletions packages/fjl/src/types/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface Nameable {
/**
* Represents strings, arrays, and/or, any structures that contain a `length`, and number indices.
*
* @todo Consider deprecating this for [native] `Iterable<T>` type.
* @todo Consider using `Iterable<T>` type, instead of this type, where it makes sense.
*/
export type NumberIndexable<T = unknown> = ({
length: number;
Expand All @@ -23,10 +23,11 @@ export type NumberIndexable<T = unknown> = ({
*/
{
readonly length: number;
readonly [index: number]: any; // for `String`/`string`, type here
// should actually be `string`, this is too strict, for
// a Sum type, however, so `any` is used instead (works fine)
// interchangeably targeting `string`, and/or `Array` types, from overall type.
readonly [index: number]: any; // Even though string variant type here
// should actually be `string`, this is too strict for
// a Sum type so `any` is used instead - this allows
// `string`, and/or `Array` types, to be used where `NumberIndexable` type is required,
// interchangeably.
});

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/fjl/tests/list/test-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {expectEqual, expectError, vowelsArray, vowelsString} from "../helpers";
import {drop} from "../../src";

describe('#drop', () => {
it('should return a new list/string with dropped items from original until limit', () => {
(<[Parameters<typeof drop>, Slice<any>][]>[
it('should return a new list/string with dropped items from original until length', () => {
(<[Parameters<typeof drop>, Slice][]>[
[[0, ''], ''],
[[0, []], []],
[[1, ''], ''],
Expand All @@ -25,6 +25,7 @@ describe('#drop', () => {
expectEqual(drop(...args), expected);
});
});

it('should throw an error when no parameter is passed in', () => {
(<any[]>[null, undefined, 0, {}]).forEach(xs =>
expectError(() => drop(3, xs))
Expand Down

0 comments on commit 2740ac1

Please sign in to comment.