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): add MutArray.popAt() and MutArray.removeFirst() to std library #3925

Merged
merged 6 commits into from
Aug 30, 2023
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
34 changes: 34 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 @@ -1099,7 +1099,9 @@ Mutable Array.
| <code><a href="#@winglang/sdk.std.MutArray.join">join</a></code> | Returns a new string containing the concatenated values in this array, separated by commas or a specified separator string. |
| <code><a href="#@winglang/sdk.std.MutArray.lastIndexOf">lastIndexOf</a></code> | Returns the index of the last occurrence of searchElement found. |
| <code><a href="#@winglang/sdk.std.MutArray.pop">pop</a></code> | Remove value from end of array. |
| <code><a href="#@winglang/sdk.std.MutArray.popAt">popAt</a></code> | Removes value from the given index of an array. |
| <code><a href="#@winglang/sdk.std.MutArray.push">push</a></code> | Add value to end of array. |
| <code><a href="#@winglang/sdk.std.MutArray.removeFirst">removeFirst</a></code> | Removes first occurence of a given value in an array. |
| <code><a href="#@winglang/sdk.std.MutArray.set">set</a></code> | Sets a new value at the given index of an array. |

---
Expand Down Expand Up @@ -1241,6 +1243,22 @@ pop(): <T>

Remove value from end of array.

##### `popAt` <a name="popAt" id="@winglang/sdk.std.MutArray.popAt"></a>

```wing
popAt(index: num): <T>
```

Removes value from the given index of an array.

###### `index`<sup>Required</sup> <a name="index" id="@winglang/sdk.std.MutArray.popAt.parameter.index"></a>

- *Type:* num

the index to remove the value at.

---

##### `push` <a name="push" id="@winglang/sdk.std.MutArray.push"></a>

```wing
Expand All @@ -1257,6 +1275,22 @@ value to add.

---

##### `removeFirst` <a name="removeFirst" id="@winglang/sdk.std.MutArray.removeFirst"></a>

```wing
removeFirst(value: <T>): bool
```

Removes first occurence of a given value in an array.

###### `value`<sup>Required</sup> <a name="value" id="@winglang/sdk.std.MutArray.removeFirst.parameter.value"></a>

- *Type:* <a href="#@winglang/sdk.std.T1">&lt;T&gt;</a>

the value to remove.

---

##### `set` <a name="set" id="@winglang/sdk.std.MutArray.set"></a>

```wing
Expand Down
51 changes: 51 additions & 0 deletions examples/tests/sdk_tests/std/array.w
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,39 @@ test "pushAndPop()" {
assert(a.at(0) == "hello");
}

//-----------------------------------------------------------------------------
// popAt()

test "popAt()" {
let assertThrows = (expected: str, block: (): void) => {
let var error = false;
try {
block();
} catch actual {
assert(actual == expected);
error = true;
}
assert(error);
};

let INDEX_OUT_OF_BOUNDS_ERROR = "Index out of bounds";
let mutArr = MutArray<str>["hello", "world"];

let item = mutArr.popAt(0);

assert(item == "hello");
assert(mutArr.length == 1);
assert(mutArr.at(0) == "world");

assertThrows(INDEX_OUT_OF_BOUNDS_ERROR, () => {
mutArr.popAt(-3);
});

assertThrows(INDEX_OUT_OF_BOUNDS_ERROR, () => {
mutArr.popAt(3);
});
}

//-----------------------------------------------------------------------------
// concat()
let array = Array<str>["hello"];
Expand Down Expand Up @@ -307,4 +340,22 @@ test "insert()" {

assert(mutArr.length == 5);
assert(mutArr.at(4) == 25);
}

//-----------------------------------------------------------------------------
// removeFirst()

test "removeFirst()" {
let mutArr = MutArray<num>[3, 6, 9, 3];

let r1 = mutArr.removeFirst(3);

assert(r1 == true);
assert(mutArr.length == 3);
assert(mutArr == MutArray<num> [6, 9, 3]);

let r2 = mutArr.removeFirst(-42);

assert(r2 == false);
assert(mutArr.length == 3);
}
2 changes: 1 addition & 1 deletion libs/wingc/src/jsify/snapshots/json_object.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = function({ $jsonObj1, $std_Json }) {
return $obj;
}
async handle() {
{console.log(((args) => { return JSON.stringify(args[0], null, args[1]) })([$jsonObj1]))};
{console.log(((args) => { return JSON.stringify(args[0], null, args[1]?.indent) })([$jsonObj1]))};
}
}
return $Closure1;
Expand Down
4 changes: 2 additions & 2 deletions libs/wingc/src/lsp/snapshots/completions/json_statics.snap
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ source: libs/wingc/src/lsp/completions.rs
command: editor.action.triggerParameterHints
- label: stringify
kind: 2
detail: "(json: any, indent: num?): str"
detail: "(json: any, options: JsonStringifyOptions?): str"
documentation:
kind: markdown
value: "```wing\nstatic stringify: (json: any, indent: num?): str\n```\n---\nFormats Json as string.\n\n\n### Returns\nstring representation of the Json\n\n### Remarks\n(JSON.stringify($args$))"
value: "```wing\nstatic stringify: (json: any, options: JsonStringifyOptions?): str\n```\n---\nFormats Json as string.\n\n\n### Returns\nstring representation of the Json"
sortText: ff|stringify
insertText: stringify($0)
insertTextFormat: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ source: libs/wingc/src/lsp/completions.rs
command: editor.action.triggerParameterHints
- label: stringify
kind: 2
detail: "(json: any, indent: num?): str"
detail: "(json: any, options: JsonStringifyOptions?): str"
documentation:
kind: markdown
value: "```wing\nstatic stringify: (json: any, indent: num?): str\n```\n---\nFormats Json as string.\n\n\n### Returns\nstring representation of the Json\n\n### Remarks\n(JSON.stringify($args$))"
value: "```wing\nstatic stringify: (json: any, options: JsonStringifyOptions?): str\n```\n---\nFormats Json as string.\n\n\n### Returns\nstring representation of the Json"
sortText: ff|stringify
insertText: stringify($0)
insertTextFormat: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ source: libs/wingc/src/lsp/completions.rs
command: editor.action.triggerParameterHints
- label: stringify
kind: 2
detail: "(json: any, indent: num?): str"
detail: "(json: any, options: JsonStringifyOptions?): str"
documentation:
kind: markdown
value: "```wing\nstatic stringify: (json: any, indent: num?): str\n```\n---\nFormats Json as string.\n\n\n### Returns\nstring representation of the Json\n\n### Remarks\n(JSON.stringify($args$))"
value: "```wing\nstatic stringify: (json: any, options: JsonStringifyOptions?): str\n```\n---\nFormats Json as string.\n\n\n### Returns\nstring representation of the Json"
sortText: ff|stringify
insertText: stringify($0)
insertTextFormat: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ source: libs/wingc/src/lsp/completions.rs
command: editor.action.triggerParameterHints
- label: stringify
kind: 2
detail: "(json: any, indent: num?): str"
detail: "(json: any, options: JsonStringifyOptions?): str"
documentation:
kind: markdown
value: "```wing\nstatic stringify: (json: any, indent: num?): str\n```\n---\nFormats Json as string.\n\n\n### Returns\nstring representation of the Json\n\n### Remarks\n(JSON.stringify($args$))"
value: "```wing\nstatic stringify: (json: any, options: JsonStringifyOptions?): str\n```\n---\nFormats Json as string.\n\n\n### Returns\nstring representation of the Json"
sortText: ff|stringify
insertText: stringify($0)
insertTextFormat: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: libs/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
value: "```wing\nstatic stringify: (json: any, indent: num?): str\n```\n---\nFormats Json as string.\n\n\n### Returns\nstring representation of the Json\n\n### Remarks\n(JSON.stringify($args$))"
value: "```wing\nstatic stringify: (json: any, options: JsonStringifyOptions?): str\n```\n---\nFormats Json as string.\n\n\n### Returns\nstring representation of the Json"
range:
start:
line: 1
Expand Down
27 changes: 27 additions & 0 deletions libs/wingsdk/src/std/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,20 @@ export class MutArray {
throw new Error("Abstract");
}

/**
* Removes value from the given index of an array
*
* @macro ((obj, args) => { if (args[0] < 0 || args[0] >= $self$.length) throw new Error("Index out of bounds"); return obj.splice(args[0], 1)[0]; })($self$, [$args$])
*
* @param index the index to remove the value at
* @returns the value removed
* @throws index out of bounds error if the given index does not exist for the array
*/
public popAt(index: number): T1 {
index;
throw new Error("Macro");
}

/**
* Sets a new value at the given index of an array
*
Expand Down Expand Up @@ -279,4 +293,17 @@ export class MutArray {
value;
throw new Error("Macro");
}

/**
* Removes first occurence of a given value in an array
*
* @macro ((obj, args) => { if (obj.indexOf(args[0]) !== -1) { obj.splice(obj.indexOf(args[0]), 1); return true; } return false; })($self$, [$args$])
*
* @param value the value to remove
* @returns true if value was removed
garysassano marked this conversation as resolved.
Show resolved Hide resolved
*/
public removeFirst(value: T1): boolean {
value;
throw new Error("Macro");
}
}
Loading
Loading