Skip to content

Commit

Permalink
add ARRAY filter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
duart38 committed Mar 17, 2021
1 parent 3b95038 commit d3d51ee
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
19 changes: 19 additions & 0 deletions builder/ARRAY.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ export class ARR<I> extends LogicalStack<ARR<I>>
return this;
}

/**
* Performs a filter on the inner value regardless if stack is true or false.
* @param predicate
* @returns
*/
public filter(predicate: (val: I, index?: number, arr?:I[])=>boolean): this {
this.value = this.value.filter(predicate);
return this;
}
/**
* Performs a filter if the previous chain items result in a "true".
* @param predicate
* @returns
*/
public thenFilter(predicate: (val: I, index?: number, arr?:I[])=>boolean): this {
if(this.lastStackMatched) this.filter(predicate);
return this;
}

/**
* Checks if the current array length is of size 'n'
* @param n
Expand Down
24 changes: 24 additions & 0 deletions tests/ARRAY_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ import {
} from "https://deno.land/std@0.90.0/testing/asserts.ts";
import { ARRAY } from "../mod.ts";


Deno.test("Test array filter", () => {
let called = false;
ARRAY([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).filter((x)=>x == 1).do((arr)=>{
assertEquals(arr[0], 1);
called = true;
})
if (!called) fail("clause was never called");
});
Deno.test("Test array thenFilter", () => {
let called = false;
ARRAY([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
.isOfSize(0)
.thenFilter((x)=>{fail("incorrect clause"); return false})
.or()
.isOfSize(10)
.thenFilter((x)=>x == 1).do((arr)=>{
assertEquals(arr[0], 1);
assertEquals(arr.length, 1);
called = true;
})
if (!called) fail("clause was never called");
});

Deno.test("Test array append()", () => {
let called = false;
ARRAY(["hello"]).append("world").do((cv) => {
Expand Down

0 comments on commit d3d51ee

Please sign in to comment.