diff --git a/docs/api/04-standard-library/std/array.md b/docs/api/04-standard-library/std/array.md index bfb6a1a2375..70a0071537f 100644 --- a/docs/api/04-standard-library/std/array.md +++ b/docs/api/04-standard-library/std/array.md @@ -204,6 +204,7 @@ Mutable Array. | **Name** | **Description** | | --- | --- | | at | Get the value at the given index. | +| clear | Removes all elements. | | concat | Merge arr to the end of this array. | | contains | Checks if this array includes searchElement. | | copy | Create an immutable shallow copy of this array. | @@ -236,6 +237,14 @@ index of the value to get. --- +##### `clear` + +```wing +clear(): void +``` + +Removes all elements. + ##### `concat` ```wing diff --git a/packages/@winglang/sdk/src/std/array.ts b/packages/@winglang/sdk/src/std/array.ts index 2255e1e2770..f31eca4c3f2 100644 --- a/packages/@winglang/sdk/src/std/array.ts +++ b/packages/@winglang/sdk/src/std/array.ts @@ -165,6 +165,16 @@ export class MutArray { throw new Error("Abstract"); } + /** + * Removes all elements + * + * @macro $self$.length = 0; + * + */ + public clear(): void { + throw new Error("Macro"); + } + /** * Get the value at the given index * @macro ((arr, index) => { if (index < 0 || index >= arr.length) throw new Error("Index out of bounds"); return arr[index]; })($self$, $args$) diff --git a/tests/sdk_tests/std/array.test.w b/tests/sdk_tests/std/array.test.w index abaef168609..f3aa9020c49 100644 --- a/tests/sdk_tests/std/array.test.w +++ b/tests/sdk_tests/std/array.test.w @@ -19,6 +19,39 @@ test "length" { assert(MutArray["hello"].length == 1); } +//----------------------------------------------------------------------------- +// clear() + +test "clear()" { + let arr = MutArray["hello", "world", "wingcloud"]; + assert(arr.length == 3); + + arr.clear(); + + assert(arr.length == 0); + + // Verify that accessing elements throws an error + let assertThrows = (expected: str, block: (): void) => { + let var error = false; + try { + block(); + } catch actual { + assert(actual == expected); + error = true; + } + assert(error); + }; + + assertThrows("Index out of bounds", () => { + arr.at(0); + }); + + // Verify that we can add new elements after clearing + arr.push("new element"); + assert(arr.length == 1); + assert(arr.at(0) == "new element"); +} + //----------------------------------------------------------------------------- // at() diff --git a/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/array.test.w_test_sim.md b/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/array.test.w_test_sim.md index bcc7e2198a9..403bf2cf1bf 100644 --- a/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/array.test.w_test_sim.md +++ b/tools/hangar/__snapshots__/test_corpus/sdk_tests/std/array.test.w_test_sim.md @@ -3,6 +3,7 @@ ## stdout.log ```log pass ─ array.test.wsim » root/Default/test:at() +pass ─ array.test.wsim » root/Default/test:clear() pass ─ array.test.wsim » root/Default/test:concatArray() pass ─ array.test.wsim » root/Default/test:concatMutArray() pass ─ array.test.wsim » root/Default/test:contains() @@ -21,7 +22,7 @@ pass ─ array.test.wsim » root/Default/test:removeFirst() pass ─ array.test.wsim » root/Default/test:set() pass ─ array.test.wsim » root/Default/test:slice() -Tests 18 passed (18) +Tests 19 passed (19) Snapshots 1 skipped Test Files 1 passed (1) Duration