Skip to content

Commit

Permalink
add remove, getSection
Browse files Browse the repository at this point in the history
  • Loading branch information
duart38 committed Mar 16, 2021
1 parent 30f0ebf commit 80ae85f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
22 changes: 22 additions & 0 deletions builder/ARRAY.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ export class ARR<I> implements WritableBuilder<Array<I>, ARR<I>>, Logical {
return this.value.entries();
}

/**
* Attempts to remove an element in the array if found.
* @param element
*/
public remove(element: I): this {
const t = this.value.findIndex((x)=>x === element)
if(t != -1) this.value.splice(t, 1);
return this;
}

/**
* Modifies the array to be a section of itself.
* For both start and end, a negative index can be used to indicate an offset from the end of the array.
* For example, -2 refers to the second to last element of the array.
* @param start
* @param end
*/
public getSection(start: number, end: number): this {
this.value = this.value.slice(start, end);
return this;
}

/**
* join the array together into a string
* @param separator
Expand Down
25 changes: 25 additions & 0 deletions tests/ARRAY_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
assertArrayIncludes,
assertStringIncludes,
fail,
assertEquals,
assertNotEquals
} from "https://deno.land/std@0.90.0/testing/asserts.ts";
import { ARRAY } from "../mod.ts";

Expand Down Expand Up @@ -125,3 +127,26 @@ Deno.test("Test array entries", () => {
// merely runs the internal entries. no need for testing
ARRAY(["hello"]).entries();
});
Deno.test("Test array remove", () => {
let called = false;
ARRAY(["hello", "world"]).remove("world").do((x)=>{
assertArrayIncludes(x, ["hello"]);
called = true;
});

ARRAY(["hello", "world"]).remove("x").do((x)=>{
assertArrayIncludes(x, ["hello", "world"]);
called = true;
});
if(!called) fail("clause was never called");
});
Deno.test("Test array getSection", () => {
let called = false;
ARRAY([1,2,3,4,5,6,7,8,9,10]).getSection(0,3).do((x)=>{
assertEquals(x.length, 3);
assertEquals(x[0], 1);
assertNotEquals(x, [1,2,3,4,5,6,7,8,9,10]);
called = true;
})
if(!called) fail("clause was never called");
});

0 comments on commit 80ae85f

Please sign in to comment.