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 string.replace() #3592

Merged
merged 7 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 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 @@ -1574,6 +1574,7 @@ String.
| <code><a href="#@winglang/sdk.std.String.endsWith">endsWith</a></code> | Does this string end with the given searchString? |
| <code><a href="#@winglang/sdk.std.String.indexOf">indexOf</a></code> | Returns the index of the first occurrence of searchString found. |
| <code><a href="#@winglang/sdk.std.String.lowercase">lowercase</a></code> | Returns this string in lower case. |
| <code><a href="#@winglang/sdk.std.String.replace">replace</a></code> | Replaces occurrences of a substring within a string. |
| <code><a href="#@winglang/sdk.std.String.split">split</a></code> | Splits string by separator. |
| <code><a href="#@winglang/sdk.std.String.startsWith">startsWith</a></code> | Does this string start with the given searchString? |
| <code><a href="#@winglang/sdk.std.String.substring">substring</a></code> | Returns a string between indexStart, indexEnd. |
Expand Down Expand Up @@ -1670,6 +1671,30 @@ lowercase(): str

Returns this string in lower case.

##### `replace` <a name="replace" id="@winglang/sdk.std.String.replace"></a>

```wing
replace(searchString: str, replaceString: str): str
```

Replaces occurrences of a substring within a string.

###### `searchString`<sup>Required</sup> <a name="searchString" id="@winglang/sdk.std.String.replace.parameter.searchString"></a>

- *Type:* str

The substring to search for.

---

###### `replaceString`<sup>Required</sup> <a name="replaceString" id="@winglang/sdk.std.String.replace.parameter.replaceString"></a>

- *Type:* str

The replacement substring.

---

##### `split` <a name="split" id="@winglang/sdk.std.String.split"></a>

```wing
Expand Down
17 changes: 17 additions & 0 deletions examples/tests/sdk_tests/std/string.w
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,20 @@ test "contains()" {
assert(!"hello wing".contains("Hello"));
assert("hello wing".contains("w"));
}

//-----------------------------------------------------------------------------
// replace()

assert("hello world".replace("world", "wing") == "hello wing");
assert("pʅɹoʍ oʅʅǝɥ".replace("pʅɹoʍ", "ɓuᴉʍ") == "ɓuᴉʍ oʅʅǝɥ");
assert("hello world".replace("wing", "☁") == "hello world");
assert("hello world".replace(" ", "-") == "hello-world");
assert("".replace("", "hello world") == "hello world");

test "replace()" {
assert("hello world".replace("world", "wing") == "hello wing");
assert("pʅɹoʍ oʅʅǝɥ".replace("pʅɹoʍ", "ɓuᴉʍ") == "ɓuᴉʍ oʅʅǝɥ");
assert("hello world".replace("wing", "☁") == "hello world");
assert("hello world".replace(" ", "-") == "hello-world");
assert("".replace("", "hello world") == "hello world");
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ source: libs/wingc/src/lsp/completions.rs
value: "```wing\nlowercase: (): str\n```\n---\nReturns this string in lower case.\n\n\n### Returns\na new lower case string."
sortText: ff|lowercase
insertText: lowercase()
- label: replace
kind: 2
detail: "(searchString: str, replaceString: str): str"
documentation:
kind: markdown
value: "```wing\nreplace: (searchString: str, replaceString: str): str\n```\n---\nReplaces occurrences of a substring within a string.\n\n\n### Returns\nThe modified string after replacement."
sortText: ff|replace
insertText: replace($0)
insertTextFormat: 2
command:
title: triggerParameterHints
command: editor.action.triggerParameterHints
- label: split
kind: 2
detail: "(separator: str): Array<str>"
Expand Down
15 changes: 15 additions & 0 deletions libs/wingsdk/src/std/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ export class String {
throw new Error("Abstract");
}

/**
* Replaces occurrences of a substring within a string.
*
* @macro $self$.replace($args$)
*
* @param searchString The substring to search for.
* @param replaceString The replacement substring.
* @returns The modified string after replacement.
*/
public replace(searchString: string, replaceString: string): string {
searchString;
replaceString;
throw new Error("Abstract");
}

/**
* Removes white spaces from start and end of this string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ module.exports = function({ }) {

```

## inflight.$Closure14.js
```js
module.exports = function({ }) {
class $Closure14 {
constructor({ }) {
const $obj = (...args) => this.handle(...args);
Object.setPrototypeOf($obj, this);
return $obj;
}
async handle() {
{((cond) => {if (!cond) throw new Error("assertion failed: \"hello world\".replace(\"world\", \"wing\") == \"hello wing\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })("hello world".replace("world","wing"),"hello wing")))};
{((cond) => {if (!cond) throw new Error("assertion failed: \"pʅɹoʍ oʅʅǝɥ\".replace(\"pʅɹoʍ\", \"ɓuᴉʍ\") == \"ɓuᴉʍ oʅʅǝɥ\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })("pʅɹoʍ oʅʅǝɥ".replace("pʅɹoʍ","ɓuᴉʍ"),"ɓuᴉʍ oʅʅǝɥ")))};
{((cond) => {if (!cond) throw new Error("assertion failed: \"hello world\".replace(\"wing\", \"☁\") == \"hello world\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })("hello world".replace("wing","☁"),"hello world")))};
{((cond) => {if (!cond) throw new Error("assertion failed: \"hello world\".replace(\" \", \"-\") == \"hello-world\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })("hello world".replace(" ","-"),"hello-world")))};
{((cond) => {if (!cond) throw new Error("assertion failed: \"\".replace(\"\", \"hello world\") == \"hello world\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })("".replace("","hello world"),"hello world")))};
}
}
return $Closure14;
}

```

## inflight.$Closure2.js
```js
module.exports = function({ }) {
Expand Down Expand Up @@ -301,7 +323,7 @@ module.exports = function({ }) {
},
"output": {
"WING_TEST_RUNNER_FUNCTION_ARNS": {
"value": "[[\"root/Default/Default/test:fromJson\",\"${aws_lambda_function.testfromJson_Handler_CA86BEEA.arn}\"],[\"root/Default/Default/test:length\",\"${aws_lambda_function.testlength_Handler_BFD8933F.arn}\"],[\"root/Default/Default/test:at()\",\"${aws_lambda_function.testat_Handler_E4F013BC.arn}\"],[\"root/Default/Default/test:concat()\",\"${aws_lambda_function.testconcat_Handler_E184D86A.arn}\"],[\"root/Default/Default/test:endsWith()\",\"${aws_lambda_function.testendsWith_Handler_9BA42993.arn}\"],[\"root/Default/Default/test:indexOf()\",\"${aws_lambda_function.testindexOf_Handler_BD91EA6F.arn}\"],[\"root/Default/Default/test:lowercase()\",\"${aws_lambda_function.testlowercase_Handler_EAADE79D.arn}\"],[\"root/Default/Default/test:uppercase()\",\"${aws_lambda_function.testuppercase_Handler_352FFA2E.arn}\"],[\"root/Default/Default/test:split()\",\"${aws_lambda_function.testsplit_Handler_4FAF6D9E.arn}\"],[\"root/Default/Default/test:startsWith()\",\"${aws_lambda_function.teststartsWith_Handler_C8752245.arn}\"],[\"root/Default/Default/test:substring()\",\"${aws_lambda_function.testsubstring_Handler_E6617207.arn}\"],[\"root/Default/Default/test:trim()\",\"${aws_lambda_function.testtrim_Handler_403ED8AD.arn}\"],[\"root/Default/Default/test:contains()\",\"${aws_lambda_function.testcontains_Handler_F60865D9.arn}\"]]"
"value": "[[\"root/Default/Default/test:fromJson\",\"${aws_lambda_function.testfromJson_Handler_CA86BEEA.arn}\"],[\"root/Default/Default/test:length\",\"${aws_lambda_function.testlength_Handler_BFD8933F.arn}\"],[\"root/Default/Default/test:at()\",\"${aws_lambda_function.testat_Handler_E4F013BC.arn}\"],[\"root/Default/Default/test:concat()\",\"${aws_lambda_function.testconcat_Handler_E184D86A.arn}\"],[\"root/Default/Default/test:endsWith()\",\"${aws_lambda_function.testendsWith_Handler_9BA42993.arn}\"],[\"root/Default/Default/test:indexOf()\",\"${aws_lambda_function.testindexOf_Handler_BD91EA6F.arn}\"],[\"root/Default/Default/test:lowercase()\",\"${aws_lambda_function.testlowercase_Handler_EAADE79D.arn}\"],[\"root/Default/Default/test:uppercase()\",\"${aws_lambda_function.testuppercase_Handler_352FFA2E.arn}\"],[\"root/Default/Default/test:split()\",\"${aws_lambda_function.testsplit_Handler_4FAF6D9E.arn}\"],[\"root/Default/Default/test:startsWith()\",\"${aws_lambda_function.teststartsWith_Handler_C8752245.arn}\"],[\"root/Default/Default/test:substring()\",\"${aws_lambda_function.testsubstring_Handler_E6617207.arn}\"],[\"root/Default/Default/test:trim()\",\"${aws_lambda_function.testtrim_Handler_403ED8AD.arn}\"],[\"root/Default/Default/test:contains()\",\"${aws_lambda_function.testcontains_Handler_F60865D9.arn}\"],[\"root/Default/Default/test:replace()\",\"${aws_lambda_function.testreplace_Handler_83836186.arn}\"]]"
}
},
"provider": {
Expand Down Expand Up @@ -383,6 +405,15 @@ module.exports = function({ }) {
},
"assume_role_policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Effect\":\"Allow\"}]}"
},
"testreplace_Handler_IamRole_8269B4F2": {
"//": {
"metadata": {
"path": "root/Default/Default/test:replace()/Handler/IamRole",
"uniqueId": "testreplace_Handler_IamRole_8269B4F2"
}
},
"assume_role_policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Effect\":\"Allow\"}]}"
},
"testsplit_Handler_IamRole_8F132662": {
"//": {
"metadata": {
Expand Down Expand Up @@ -510,6 +541,16 @@ module.exports = function({ }) {
"policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"none:null\",\"Resource\":\"*\"}]}",
"role": "${aws_iam_role.testlowercase_Handler_IamRole_AD4BFFD1.name}"
},
"testreplace_Handler_IamRolePolicy_B1BDF250": {
"//": {
"metadata": {
"path": "root/Default/Default/test:replace()/Handler/IamRolePolicy",
"uniqueId": "testreplace_Handler_IamRolePolicy_B1BDF250"
}
},
"policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"none:null\",\"Resource\":\"*\"}]}",
"role": "${aws_iam_role.testreplace_Handler_IamRole_8269B4F2.name}"
},
"testsplit_Handler_IamRolePolicy_2CBFEABE": {
"//": {
"metadata": {
Expand Down Expand Up @@ -642,6 +683,16 @@ module.exports = function({ }) {
"policy_arn": "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
"role": "${aws_iam_role.testlowercase_Handler_IamRole_AD4BFFD1.name}"
},
"testreplace_Handler_IamRolePolicyAttachment_3A28D781": {
"//": {
"metadata": {
"path": "root/Default/Default/test:replace()/Handler/IamRolePolicyAttachment",
"uniqueId": "testreplace_Handler_IamRolePolicyAttachment_3A28D781"
}
},
"policy_arn": "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
"role": "${aws_iam_role.testreplace_Handler_IamRole_8269B4F2.name}"
},
"testsplit_Handler_IamRolePolicyAttachment_50460D67": {
"//": {
"metadata": {
Expand Down Expand Up @@ -902,6 +953,32 @@ module.exports = function({ }) {
"subnet_ids": []
}
},
"testreplace_Handler_83836186": {
"//": {
"metadata": {
"path": "root/Default/Default/test:replace()/Handler/Default",
"uniqueId": "testreplace_Handler_83836186"
}
},
"environment": {
"variables": {
"WING_FUNCTION_NAME": "Handler-c876baf0",
"WING_TARGET": "tf-aws"
}
},
"function_name": "Handler-c876baf0",
"handler": "index.handler",
"publish": true,
"role": "${aws_iam_role.testreplace_Handler_IamRole_8269B4F2.arn}",
"runtime": "nodejs18.x",
"s3_bucket": "${aws_s3_bucket.Code.bucket}",
"s3_key": "${aws_s3_object.testreplace_Handler_S3Object_20DF2856.key}",
"timeout": 30,
"vpc_config": {
"security_group_ids": [],
"subnet_ids": []
}
},
"testsplit_Handler_4FAF6D9E": {
"//": {
"metadata": {
Expand Down Expand Up @@ -1133,6 +1210,17 @@ module.exports = function({ }) {
"key": "<ASSET_KEY>",
"source": "<ASSET_SOURCE>"
},
"testreplace_Handler_S3Object_20DF2856": {
"//": {
"metadata": {
"path": "root/Default/Default/test:replace()/Handler/S3Object",
"uniqueId": "testreplace_Handler_S3Object_20DF2856"
}
},
"bucket": "${aws_s3_bucket.Code.bucket}",
"key": "<ASSET_KEY>",
"source": "<ASSET_SOURCE>"
},
"testsplit_Handler_S3Object_08906E51": {
"//": {
"metadata": {
Expand Down Expand Up @@ -1529,6 +1617,30 @@ class $Root extends $stdlib.std.Resource {
`);
}
}
class $Closure14 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
require("./inflight.$Closure14.js")({
})
`);
}
_toInflight() {
return $stdlib.core.NodeJsCode.fromInline(`
(await (async () => {
const $Closure14Client = ${$Closure14._toInflightType(this).text};
const client = new $Closure14Client({
});
if (client.$inflight_init) { await client.$inflight_init(); }
return client;
})())
`);
}
}
const assertThrows = ((expected, block) => {
let error = false;
try {
Expand Down Expand Up @@ -1603,6 +1715,12 @@ class $Root extends $stdlib.std.Resource {
{((cond) => {if (!cond) throw new Error("assertion failed: !\"hello wing\".contains(\"Hello\")")})((!"hello wing".includes("Hello")))};
{((cond) => {if (!cond) throw new Error("assertion failed: \"hello wing\".contains(\"w\")")})("hello wing".includes("w"))};
this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:contains()",new $Closure13(this,"$Closure13"));
{((cond) => {if (!cond) throw new Error("assertion failed: \"hello world\".replace(\"world\", \"wing\") == \"hello wing\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })("hello world".replace("world","wing"),"hello wing")))};
{((cond) => {if (!cond) throw new Error("assertion failed: \"pʅɹoʍ oʅʅǝɥ\".replace(\"pʅɹoʍ\", \"ɓuᴉʍ\") == \"ɓuᴉʍ oʅʅǝɥ\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })("pʅɹoʍ oʅʅǝɥ".replace("pʅɹoʍ","ɓuᴉʍ"),"ɓuᴉʍ oʅʅǝɥ")))};
{((cond) => {if (!cond) throw new Error("assertion failed: \"hello world\".replace(\"wing\", \"☁\") == \"hello world\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })("hello world".replace("wing","☁"),"hello world")))};
{((cond) => {if (!cond) throw new Error("assertion failed: \"hello world\".replace(\" \", \"-\") == \"hello-world\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })("hello world".replace(" ","-"),"hello-world")))};
{((cond) => {if (!cond) throw new Error("assertion failed: \"\".replace(\"\", \"hello world\") == \"hello world\"")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })("".replace("","hello world"),"hello world")))};
this.node.root.new("@winglang/sdk.std.Test",std.Test,this,"test:replace()",new $Closure14(this,"$Closure14"));
}
}
const $App = $stdlib.core.App.for(process.env.WING_TARGET);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pass ─ string.wsim » root/env1/test:length
pass ─ string.wsim » root/env10/test:substring()
pass ─ string.wsim » root/env11/test:trim()
pass ─ string.wsim » root/env12/test:contains()
pass ─ string.wsim » root/env13/test:replace()
pass ─ string.wsim » root/env2/test:at()
pass ─ string.wsim » root/env3/test:concat()
pass ─ string.wsim » root/env4/test:endsWith()
Expand All @@ -17,7 +18,7 @@ pass ─ string.wsim » root/env8/test:split()
pass ─ string.wsim » root/env9/test:startsWith()


Tests 13 passed (13)
Tests 14 passed (14)
Test Files 1 passed (1)
Duration <DURATION>
```
Expand Down
Loading