{"id":17912338,"url":"https://github.com/kingwl/proposal-string-trim-characters","last_synced_at":"2025-04-03T06:47:26.704Z","repository":{"id":85142110,"uuid":"384023073","full_name":"Kingwl/proposal-string-trim-characters","owner":"Kingwl","description":"Proposal to add argument for .trim(), .trimStart() and .trimEnd() to allow strip the specified characters from strings.","archived":false,"fork":false,"pushed_at":"2022-11-15T17:43:01.000Z","size":32,"stargazers_count":27,"open_issues_count":3,"forks_count":1,"subscribers_count":12,"default_branch":"main","last_synced_at":"2025-02-08T20:47:20.184Z","etag":null,"topics":["ecmascript","proposal"],"latest_commit_sha":null,"homepage":"","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Kingwl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-08T06:24:45.000Z","updated_at":"2024-09-07T11:11:18.000Z","dependencies_parsed_at":"2023-06-29T11:16:32.353Z","dependency_job_id":null,"html_url":"https://github.com/Kingwl/proposal-string-trim-characters","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":"tc39/template-for-proposals","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kingwl%2Fproposal-string-trim-characters","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kingwl%2Fproposal-string-trim-characters/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kingwl%2Fproposal-string-trim-characters/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kingwl%2Fproposal-string-trim-characters/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kingwl","download_url":"https://codeload.github.com/Kingwl/proposal-string-trim-characters/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246952273,"owners_count":20859811,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ecmascript","proposal"],"created_at":"2024-10-28T19:44:37.399Z","updated_at":"2025-04-03T06:47:26.689Z","avatar_url":"https://github.com/Kingwl.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [proposal-string-trim-characters](https://github.com/Kingwl/proposal-string-trim-characters)\n\nProposal to add argument for `.trim()`, `.trimStart()` and `.trimEnd()` to allow strip the specified characters from strings.\n\n## Status\n\nThis proposal is a [stage-0 proposal](https://github.com/tc39/proposals/blob/master/stage-0-proposals.md) and waiting for feedback.\n\n## Motivation\n\nWe often remove some leading/trailing whitespaces from the beginning or end (or both) of a string by `trim`, `trimStart`, and `trimEnd`.\n\nWe can only remove the [WhiteSpace](https://262.ecma-international.org/11.0/#prod-WhiteSpace) and [LineTerminator](https://262.ecma-international.org/11.0/#prod-LineTerminator). If you want to remove some other strings who not defined by whitespace. There's no semantical and convenience way to do that.\n\n### Semantical \u0026 Convenience\nThe major point of the proposal is **semantical** and **convenience**.\n\nFor now. How could we remove some specific leading/trailing characters?\n\n- regex\n\n```ts\nconst str = \"-_-abc-_-\";\nconst characters = \"-_\";\n\nconst regex = new RegExp(`(^[${characters}]*)|([${characters}]*$)`, 'g')\n\nconsole.log(str.replaceAll(regex, '')) // abc\n```\n\n- manually\n\n```ts\nconst str = \"-_-abc-_-\";\nconst characters = \"-_\";\n\nlet start = 0;\nwhile (characters.indexOf(str[start]) \u003e= 0) {\n    start += 1;\n}\nlet end = str.length - 1;\nwhile (characters.indexOf(str[end]) \u003e= 0) {\n    end -= 1;\n}\n\nconsole.log(str.substr(start, end - start + 1)) // abc\n```\n\nAs you can see. For these both solutions, there's no idea about what does it doing when you see it, You have to pay attention on it to understand it. \n\n### Performance\n\nAnd for the regex version, there's might also performance issue as [TypeScript's implementations](https://github.com/microsoft/TypeScript/blob/main/src/compiler/core.ts#L2330-L2344): [jsbench](https://jsbench.me/gjkoxld4au/1).\n\n### Consolation\n\nThat's why we need this proposal. It's will add some **semantic** and **convenience** way to `clearly representing the operation i want`. And as a possible bonus, it also reduces the amount of very poorly performing code we write.\n\n## Core API\n\nAdd an optional argument `characters` into `String.prototype.trim` ,  `String.prototype.trimStart`and   `String.prototype.trimEnd`. \n\nThis argument will allow us which characters will be remove from the start or end (or both) from the string.\n\nThe definition of API will looks like:\n\n```ts\ninterface String {\n    trim(characters?: string): string;\n    trimStart(characters?: string): string;\n    trimEnd(characters?: string): string;\n}\n\n```\n\nWith this proposal, we could use as:\n\n```typescript\nconst str = \"-_-abc-_-\";\nconst characters = \"-_\";\n\nconsole.log(str.trim(characters)) // abc\nconsole.log(str.trimStart(characters)) // abc-_-\nconsole.log(str.trimEnd(characters)) // -_-abc\n```\n\n## Prior art\n\n- Lodash - [lodash.trim](https://lodash.com/docs/4.17.15#trim), [lodash.trimStart](https://lodash.com/docs/4.17.15#trimStart), [lodash.trimEnd](https://lodash.com/docs/4.17.15#trimEnd)\n- PHP - [function.trim](https://www.php.net/manual/en/function.trim.php), [function.ltrim](https://www.php.net/manual/en/function.ltrim.php), [function.rtrim](https://www.php.net/manual/en/function.rtrim.php)\n- Python - [str.strip](https://docs.python.org/3/library/stdtypes.html#str.strip), [str.lstrip](https://docs.python.org/3/library/stdtypes.html#str.lstrip), [str.rstrip](https://docs.python.org/3/library/stdtypes.html#str.rstrip)\n- C# - [String.Trim](https://docs.microsoft.com/en-us/dotnet/api/system.string.trim?view=net-5.0), [String.TrimStart](https://docs.microsoft.com/en-us/dotnet/api/system.string.trimstart?view=net-5.0), [String.TrimEnd](https://docs.microsoft.com/en-us/dotnet/api/system.string.trimend?view=net-5.0)\n- Go - [Trim](https://golang.org/pkg/strings/#Trim), [TrimLeft](https://golang.org/pkg/strings/#TrimLeft), [TrimRight](https://golang.org/pkg/strings/#TrimRight), [TrimPrefix](https://golang.org/pkg/strings/#TrimPrefix), [TrimSuffix](https://golang.org/pkg/strings/#TrimSuffix)\n\n## Previous discuss\n- https://github.com/tc39/proposal-string-left-right-trim/issues/22 \n- https://esdiscuss.org/topic/string-trim-chars \n- https://esdiscuss.org/topic/string-prototype-trimstart-string-prototype-trimend-with-a-given-string\n\n## Proposer\n\nChampions:\n\n- @Kingwl (Wenlu Wang, KWL)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkingwl%2Fproposal-string-trim-characters","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkingwl%2Fproposal-string-trim-characters","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkingwl%2Fproposal-string-trim-characters/lists"}