Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): array.slice() #6081

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions examples/tests/sdk_tests/std/array.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,40 @@ test "removeFirst()" {
assert(r2 == false);
assert(mutArr.length == 3);
}

//-----------------------------------------------------------------------------
// slice()

test "slice()" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should test it on preflight too

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let slice1 = arr.slice(2, 5);
assert(slice1 == [3, 4, 5]);

let slice2 = arr.slice(5);
assert(slice2 == [6, 7, 8, 9, 10]);

let slice3 = arr.slice(0, 3);
assert(slice3 == [1, 2, 3]);

let slice4 = arr.slice(0);
assert(slice4 == arr);

let slice5 = arr.slice(0, 0);
assert(slice5 == []);

let mutarr = MutArray<num>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let mutSlice1 = mutarr.slice(2, 5);
assert(mutSlice1 == MutArray<num>[3, 4, 5]);

let mutSlice2 = mutarr.slice(5);
assert(mutSlice2 == MutArray<num>[6, 7, 8, 9, 10]);

let mutSlice3 = mutarr.slice(0, 3);
assert(mutSlice3 == MutArray<num>[1, 2, 3]);

let mutSlice4 = mutarr.slice(0);
assert(mutSlice4 == mutarr);

let mutSlice5 = mutarr.slice(0, 0);
assert(mutSlice5 == MutArray<num>[]);
}
30 changes: 30 additions & 0 deletions libs/wingsdk/src/std/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ export class Array {
searchElement;
throw new Error("Macro");
}

/**
* Returns a shallow copy of a portion of the array.
*
* @macro $self$.slice($args$)
*
* @param start the beginning index of the slice, inclusive.
* @param end the ending index of the slice, exclusive.
* @returns a new array containing the sliced elements.
*/
public slice(start?: number, end?: number): Array {
start;
end;
throw new Error("Macro");
}
}

/**
Expand Down Expand Up @@ -311,4 +326,19 @@ export class MutArray {
value;
throw new Error("Macro");
}

/**
* Returns a shallow copy of a portion of the array.
*
* @macro $self$.slice($args$)
*
* @param start the beginning index of the slice, inclusive.
* @param end the ending index of the slice, exclusive.
* @returns a new array containing the sliced elements.
*/
public slice(start?: number, end?: number): MutArray {
start;
end;
throw new Error("Macro");
}
}
Loading