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): adding toArray() to Set and MutSet #3160

Merged
merged 5 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 18 additions & 0 deletions docs/docs/04-standard-library/02-std/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,7 @@ Mutable Set.
| <code><a href="#@winglang/sdk.std.MutSet.copy">copy</a></code> | Create an immutable shallow copy of this set. |
| <code><a href="#@winglang/sdk.std.MutSet.delete">delete</a></code> | Removes a specified value from a set, if it is in the set. |
| <code><a href="#@winglang/sdk.std.MutSet.has">has</a></code> | Returns a boolean indicating whether an element with the specified value exists in the set. |
| <code><a href="#@winglang/sdk.std.MutSet.toArray">toArray</a></code> | Create an immutable array shallow copy of this set. |

---

Expand Down Expand Up @@ -1934,6 +1935,14 @@ The value to test for presence in the Set object.

---

##### `toArray` <a name="toArray" id="@winglang/sdk.std.MutSet.toArray"></a>

```wing
toArray(): Array
```

Create an immutable array shallow copy of this set.


#### Properties <a name="Properties" id="Properties"></a>

Expand Down Expand Up @@ -2026,6 +2035,7 @@ Immutable Set.
| --- | --- |
| <code><a href="#@winglang/sdk.std.Set.copyMut">copyMut</a></code> | Create a mutable shallow copy of this set. |
| <code><a href="#@winglang/sdk.std.Set.has">has</a></code> | Returns a boolean indicating whether an element with the specified value exists in the set. |
| <code><a href="#@winglang/sdk.std.Set.toArray">toArray</a></code> | Create an immutable array shallow copy of this set. |

---

Expand Down Expand Up @@ -2053,6 +2063,14 @@ The value to test for presence in the Set object.

---

##### `toArray` <a name="toArray" id="@winglang/sdk.std.Set.toArray"></a>

```wing
toArray(): Array
```

Create an immutable array shallow copy of this set.


#### Properties <a name="Properties" id="Properties"></a>

Expand Down
17 changes: 16 additions & 1 deletion examples/tests/sdk_tests/std/set.w
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
//-----------------------------------------------------------------------------
// TODO: https://github.com/winglang/wing/issues/2785
// TODO: https://github.com/winglang/wing/issues/2785

//-----------------------------------------------------------------------------
// toArray()
let mySet = Set<num> {1, 2, 3};
let myArrayFromSet = mySet.toArray();
assert(myArrayFromSet.at(0) == 1);
assert(myArrayFromSet.at(1) == 2);
assert(myArrayFromSet.at(2) == 3);

let myMutSet = MutSet<str> {"a", "b", "c"};
let myArrayFromMutSet = myMutSet.toArray();
assert(myArrayFromMutSet.at(0) == "a");
assert(myArrayFromMutSet.at(1) == "b");
assert(myArrayFromMutSet.at(2) == "c");
marciocadev marked this conversation as resolved.
Show resolved Hide resolved
//-----------------------------------------------------------------------------
23 changes: 23 additions & 0 deletions libs/wingsdk/src/std/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// They should not be consumed directly by users.
// TODO: These should be interfaces, currently Wing does not support interface JSII imports

import { Array } from "./array";
import { T1 } from "./generics";
import { Code, InflightClient } from "../core";

Expand Down Expand Up @@ -48,6 +49,17 @@ export class Set {
public copyMut(): MutSet {
throw new Error("Macro");
}

/**
* Create an immutable array shallow copy of this set
*
* @macro Object.freeze([...($self$)])
*
* @returns an ImmutableArray with the same values as this set
*/
public toArray(): Array {
throw new Error("Macro");
}
}

/**
Expand Down Expand Up @@ -120,4 +132,15 @@ export class MutSet {
value;
throw new Error("Abstract");
}

/**
* Create an immutable array shallow copy of this set
*
* @macro Object.freeze([...($self$)])
*
* @returns an ImmutableArray with the same values as this set
*/
public toArray(): Array {
throw new Error("Macro");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ const $AppBase = $stdlib.core.App.for(process.env.WING_TARGET);
class $Root extends $stdlib.std.Resource {
constructor(scope, id) {
super(scope, id);
const mySet = Object.freeze(new Set([1, 2, 3]));
const myArrayFromSet = Object.freeze([...(mySet)]);
{((cond) => {if (!cond) throw new Error("assertion failed: myArrayFromSet.at(0) == 1")})(((myArrayFromSet.at(0)) === 1))};
{((cond) => {if (!cond) throw new Error("assertion failed: myArrayFromSet.at(1) == 2")})(((myArrayFromSet.at(1)) === 2))};
{((cond) => {if (!cond) throw new Error("assertion failed: myArrayFromSet.at(2) == 3")})(((myArrayFromSet.at(2)) === 3))};
const myMutSet = new Set(["a", "b", "c"]);
const myArrayFromMutSet = Object.freeze([...(myMutSet)]);
{((cond) => {if (!cond) throw new Error("assertion failed: myArrayFromMutSet.at(0) == \"a\"")})(((myArrayFromMutSet.at(0)) === "a"))};
{((cond) => {if (!cond) throw new Error("assertion failed: myArrayFromMutSet.at(1) == \"b\"")})(((myArrayFromMutSet.at(1)) === "b"))};
{((cond) => {if (!cond) throw new Error("assertion failed: myArrayFromMutSet.at(2) == \"c\"")})(((myArrayFromMutSet.at(2)) === "c"))};
}
}
class $App extends $AppBase {
Expand Down
Loading