Skip to content

Commit

Permalink
Merge branch 'main' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
djcsdy committed Mar 9, 2021
2 parents 51ffc0f + 0de8eb2 commit 8785338
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 127 deletions.
118 changes: 117 additions & 1 deletion index.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import test from "ava";
import {
contains,
dropWhile,
empty,
exclude,
excludeFirst,
excludeNull,
filter,
filterFirst,
find,
findIndex,
fold,
fold1,
foldMap,
forEach,
groupByIdentity,
head,
initial,
isArray,
isArrayLike,
last,
map,
maximum,
partition,
partitionWhile,
remove,
removeFirst
removeFirst,
slice,
tail,
takeWhile
} from "./index";

test("isArray", t => {
Expand All @@ -29,6 +43,83 @@ test("isArrayLike", t => {
t.false(isArrayLike(3));
});

test("head", t => {
t.is(head([1, 2, 3]), 1);
t.is(head([]), null);
});

test("tail", t => {
t.deepEqual(tail([1, 2, 3, 4]), [2, 3, 4]);
t.deepEqual(tail([]), []);
});

test("initial", t => {
t.deepEqual(initial([1, 2, 3, 4]), [1, 2, 3]);
t.deepEqual(initial([]), []);
});

test("last", t => {
t.is(last([]), null);
t.is(last([1, 2, 3]), 3);
});

test("empty", t => {
t.true(empty([]));
t.false(empty([1, 2, 3]));
});

test("slice", t => {
t.deepEqual(slice([1, 2, 3, 4], 1), [2, 3, 4]);
t.deepEqual(slice([1, 2, 3, 4, 5], 1, 4), [2, 3, 4]);
t.deepEqual(slice([1, 2, 3], 2), [3]);
t.deepEqual(slice([1, 2, 3], 0, 2), [1, 2]);
t.deepEqual(slice([], 3, 5), []);
});

test("takeWhile", t => {
t.deepEqual(
takeWhile([1, 2, 3, 4, 3, 2, 1], e => e < 4),
[1, 2, 3]
);
t.deepEqual(
takeWhile([1, 2, 3], (_, i) => i < 2),
[1, 2]
);
});

test("dropWhile", t => {
t.deepEqual(
dropWhile([1, 2, 3, 4, 3, 2, 1], e => e < 4),
[4, 3, 2, 1]
);
t.deepEqual(
dropWhile([1, 2, 3], (_, i) => i < 2),
[3]
);
});

test("map", t => {
t.deepEqual(
map([1, 2, 3], e => e + 1),
[2, 3, 4]
);
t.deepEqual(
map([1, 2, 3], (e, i) => (i === 1 ? e * 10 : e)),
[1, 20, 3]
);
});

test("filter", t => {
t.deepEqual(
filter([1, 2, 3], e => e % 2 === 1),
[1, 3]
);
t.deepEqual(
filter([1, 3, 2, 4, 5], (_, i) => i % 2 === 0),
[1, 2, 5]
);
});

test("filterFirst", t => {
t.deepEqual(
filterFirst([1, 2, 3, 4, 3, 2, 1], n => n < 3),
Expand Down Expand Up @@ -62,6 +153,20 @@ test("removeFirst", t => {
t.deepEqual(removeFirst([1, 2, 3, 4, 3, 2, 1], 3), [1, 2, 4, 3, 2, 1]);
});

test("fold", t => {
t.is(
fold([1, 2, 3], (a, e, i) => a + e * i, 0),
8
);
});

test("fold1", t => {
t.is(
fold1([1, 2, 3], (a, e, i) => a + e * i),
9
);
});

test("foldMap", t => {
t.is(
foldMap(["2", "3", "4"], (a, b) => a + b, parseFloat, 2),
Expand All @@ -82,6 +187,11 @@ test("foldMap", t => {
);
});

test("contains", t => {
t.true(contains([1, 2, 3], 1));
t.false(contains([1, 2, 3], 0));
});

test("findIndex", t => {
t.is(
findIndex([1, 2, 3, 4, 3, 2, 1], n => n >= 3),
Expand All @@ -96,6 +206,12 @@ test("find", t => {
);
});

test("maximum", t => {
t.is(maximum([1, 2, 3]), 3);
t.is(maximum([1, 2, 3, 4, 3, 2, 1]), 4);
t.is(maximum([]), null);
});

type Result<T> = Success<T> | Error;

interface Success<T> {
Expand Down
24 changes: 17 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {isNotNull, isNull} from "@softwareventures/nullable";
import {Comparator, compare as defaultCompare, Comparison} from "@softwareventures/ordered";

// eslint-disable-next-line @typescript-eslint/unbound-method
Expand Down Expand Up @@ -74,7 +75,7 @@ export function tail<T>(array: ArrayLike<T>): T[] {
}

export function initial<T>(array: ArrayLike<T>): T[] {
return array.length === 0 ? [] : nativeSlice.call(array, array.length - 1);
return array.length === 0 ? [] : nativeSlice.call(array, 0, array.length - 1);
}

export function last<T>(array: ArrayLike<T>): T | null {
Expand Down Expand Up @@ -256,11 +257,7 @@ export function excludeFn<T>(
}

export function excludeNull<T>(array: ArrayLike<T | null | undefined>): T[] {
return filter(array, notNull);
}

function notNull<T>(value: T | null | undefined): value is T {
return value != null;
return filter(array, isNotNull);
}

export function excludeFirst<T>(
Expand Down Expand Up @@ -307,6 +304,19 @@ export function foldFn<T, U>(
return array => (nativeReduce as (...args: any[]) => any).call(array, f, initial);
}

export function fold1<T>(
array: ArrayLike<T>,
f: (accumulator: T, element: T, index: number) => T
): T {
return (nativeReduce as (...args: any[]) => any).call(array, f);
}

export function fold1Fn<T>(
f: (accumulator: T, element: T, index: number) => T
): (array: ArrayLike<T>) => T {
return array => fold1(array, f);
}

export function foldRight<T, U>(
array: ArrayLike<T>,
f: (accumulator: U, element: T, index: number) => U,
Expand Down Expand Up @@ -560,7 +570,7 @@ export function concatMapFn<T, U>(
}

export function noneNull<T>(array: ArrayLike<T | null>): ArrayLike<T> | null {
return any(array, e => e == null) ? null : (array as ArrayLike<T>);
return any(array, isNull) ? null : (array as ArrayLike<T>);
}

export function scan<T, U>(
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@
},
"sideEffects": false,
"devDependencies": {
"@softwareventures/eslint-config": "3.4.2",
"@softwareventures/eslint-config": "3.5.0",
"@softwareventures/prettier-config": "1.0.2",
"@softwareventures/semantic-release-config": "1.1.1",
"@softwareventures/tsconfig": "5.0.1",
"ava": "3.15.0",
"cz-conventional-changelog": "3.3.0",
"eslint": "7.21.0",
"prettier": "2.2.1",
"semantic-release": "17.4.0",
"semantic-release": "17.4.1",
"ts-node": "9.1.1",
"typescript": "4.2.2"
"typescript": "4.2.3"
},
"dependencies": {
"@softwareventures/nullable": "^1.0.0",
"@softwareventures/ordered": "^0.2.0",
"tslib": "^2.0.0"
},
Expand Down
Loading

0 comments on commit 8785338

Please sign in to comment.