From 80ae85faf39656324951c5a42af22ed3459fb192 Mon Sep 17 00:00:00 2001 From: duart38 Date: Tue, 16 Mar 2021 12:59:52 +0100 Subject: [PATCH] add remove, getSection --- builder/ARRAY.ts | 22 ++++++++++++++++++++++ tests/ARRAY_test.ts | 25 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/builder/ARRAY.ts b/builder/ARRAY.ts index 80c1950..3382ea8 100644 --- a/builder/ARRAY.ts +++ b/builder/ARRAY.ts @@ -73,6 +73,28 @@ export class ARR implements WritableBuilder, ARR>, 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 diff --git a/tests/ARRAY_test.ts b/tests/ARRAY_test.ts index 378b4a0..27fd3a4 100644 --- a/tests/ARRAY_test.ts +++ b/tests/ARRAY_test.ts @@ -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"; @@ -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"); +}); \ No newline at end of file