Skip to content

Add String.getSymbolUnsafe back to Stdlib #7626

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

Merged
merged 2 commits into from
Jul 11, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#### :rocket: New Feature

- Add experimental command to `rescript-tools` for formatting all ReScript code blocks in markdown. Either in a markdown file directly, or inside of docstrings in ReScript code. https://github.com/rescript-lang/rescript/pull/7598
- Add `String.getSymbolUnsafe` back to Stdlib. https://github.com/rescript-lang/rescript/pull/7626

#### :house: Internal

Expand Down
2 changes: 2 additions & 0 deletions runtime/Stdlib_String.res
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,5 @@ external splitByRegExpAtMost: (string, Stdlib_RegExp.t, ~limit: int) => array<op
@send external localeCompare: (string, string) => float = "localeCompare"

external ignore: string => unit = "%ignore"

@get_index external getSymbolUnsafe: (string, Stdlib_Symbol.t) => 'a = ""
14 changes: 14 additions & 0 deletions runtime/Stdlib_String.resi
Original file line number Diff line number Diff line change
Expand Up @@ -1137,3 +1137,17 @@ external localeCompare: (string, string) => float = "localeCompare"
without having to store or process it further.
*/
external ignore: string => unit = "%ignore"

/**
`getSymbolUnsafe(str, symbol)` returns the value of the symbol property of a string.

## Examples

```rescript
let it: Iterator.t<string> = ("foo"->String.getSymbolUnsafe(Symbol.iterator))()
Nullable.make(it)->Nullable.isNullable == false
```

*/
@get_index
external getSymbolUnsafe: (string, Stdlib_Symbol.t) => 'a = ""
72 changes: 36 additions & 36 deletions tests/analysis_tests/tests/src/expected/CompletionJsx.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -785,30 +785,6 @@ Path s
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
"newText": ""
}]
}, {
"label": "->String.startsWith",
"kind": 12,
"tags": [],
"detail": "(string, string) => bool",
"documentation": {"kind": "markdown", "value": "\n`startsWith(str, substr)` returns `true` if the `str` starts with `substr`,\n`false` otherwise.\nSee [`String.startsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) on MDN.\n\n## Examples\n\n```rescript\nString.startsWith(\"BuckleScript\", \"Buckle\") == true\nString.startsWith(\"BuckleScript\", \"\") == true\nString.startsWith(\"JavaScript\", \"Buckle\") == false\n```\n"},
"sortText": "startsWith",
"insertText": "->String.startsWith",
"additionalTextEdits": [{
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
"newText": ""
}]
}, {
"label": "->String.splitAtMost",
"kind": 12,
"tags": [],
"detail": "(string, string, ~limit: int) => array<string>",
"documentation": {"kind": "markdown", "value": "\n`splitAtMost(str, delimiter, ~limit)` splits the given `str` at every\noccurrence of `delimiter` and returns an array of the first `limit` resulting\nsubstrings. If `limit` is negative or greater than the number of substrings,\nthe array will contain all the substrings.\n\n## Examples\n\n```rescript\nString.splitAtMost(\"ant/bee/cat/dog/elk\", \"/\", ~limit=3) == [\"ant\", \"bee\", \"cat\"]\nString.splitAtMost(\"ant/bee/cat/dog/elk\", \"/\", ~limit=0) == []\nString.splitAtMost(\"ant/bee/cat/dog/elk\", \"/\", ~limit=9) == [\"ant\", \"bee\", \"cat\", \"dog\", \"elk\"]\n```\n"},
"sortText": "splitAtMost",
"insertText": "->String.splitAtMost",
"additionalTextEdits": [{
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
"newText": ""
}]
}, {
"label": "->String.searchOpt",
"kind": 12,
Expand Down Expand Up @@ -845,6 +821,42 @@ Path s
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
"newText": ""
}]
}, {
"label": "->String.substringToEnd",
"kind": 12,
"tags": [],
"detail": "(string, ~start: int) => string",
"documentation": {"kind": "markdown", "value": "\n`substringToEnd(str, ~start)` returns the substring of `str` from position\n`start` to the end.\n- If `start` is less than or equal to zero, the entire string is returned.\n- If `start` is greater than or equal to the length of `str`, the empty string\nis returned.\nSee [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) on MDN.\n\n## Examples\n\n```rescript\nString.substringToEnd(\"playground\", ~start=4) == \"ground\"\nString.substringToEnd(\"playground\", ~start=-3) == \"playground\"\nString.substringToEnd(\"playground\", ~start=12) == \"\"\n```\n"},
"sortText": "substringToEnd",
"insertText": "->String.substringToEnd",
"additionalTextEdits": [{
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
"newText": ""
}]
}, {
"label": "->String.startsWith",
"kind": 12,
"tags": [],
"detail": "(string, string) => bool",
"documentation": {"kind": "markdown", "value": "\n`startsWith(str, substr)` returns `true` if the `str` starts with `substr`,\n`false` otherwise.\nSee [`String.startsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) on MDN.\n\n## Examples\n\n```rescript\nString.startsWith(\"BuckleScript\", \"Buckle\") == true\nString.startsWith(\"BuckleScript\", \"\") == true\nString.startsWith(\"JavaScript\", \"Buckle\") == false\n```\n"},
"sortText": "startsWith",
"insertText": "->String.startsWith",
"additionalTextEdits": [{
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
"newText": ""
}]
}, {
"label": "->String.splitAtMost",
"kind": 12,
"tags": [],
"detail": "(string, string, ~limit: int) => array<string>",
"documentation": {"kind": "markdown", "value": "\n`splitAtMost(str, delimiter, ~limit)` splits the given `str` at every\noccurrence of `delimiter` and returns an array of the first `limit` resulting\nsubstrings. If `limit` is negative or greater than the number of substrings,\nthe array will contain all the substrings.\n\n## Examples\n\n```rescript\nString.splitAtMost(\"ant/bee/cat/dog/elk\", \"/\", ~limit=3) == [\"ant\", \"bee\", \"cat\"]\nString.splitAtMost(\"ant/bee/cat/dog/elk\", \"/\", ~limit=0) == []\nString.splitAtMost(\"ant/bee/cat/dog/elk\", \"/\", ~limit=9) == [\"ant\", \"bee\", \"cat\", \"dog\", \"elk\"]\n```\n"},
"sortText": "splitAtMost",
"insertText": "->String.splitAtMost",
"additionalTextEdits": [{
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
"newText": ""
}]
}, {
"label": "->String.sliceToEnd",
"kind": 12,
Expand Down Expand Up @@ -917,17 +929,5 @@ Path s
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
"newText": ""
}]
}, {
"label": "->String.substringToEnd",
"kind": 12,
"tags": [],
"detail": "(string, ~start: int) => string",
"documentation": {"kind": "markdown", "value": "\n`substringToEnd(str, ~start)` returns the substring of `str` from position\n`start` to the end.\n- If `start` is less than or equal to zero, the entire string is returned.\n- If `start` is greater than or equal to the length of `str`, the empty string\nis returned.\nSee [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) on MDN.\n\n## Examples\n\n```rescript\nString.substringToEnd(\"playground\", ~start=4) == \"ground\"\nString.substringToEnd(\"playground\", ~start=-3) == \"playground\"\nString.substringToEnd(\"playground\", ~start=12) == \"\"\n```\n"},
"sortText": "substringToEnd",
"insertText": "->String.substringToEnd",
"additionalTextEdits": [{
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
"newText": ""
}]
}]

Original file line number Diff line number Diff line change
Expand Up @@ -418,25 +418,25 @@ Path Array.joinWith
Path Stdlib.String.includ
Path includ
[{
"label": "->String.includesFrom",
"label": "->String.includes",
"kind": 12,
"tags": [],
"detail": "(string, string, int) => bool",
"documentation": {"kind": "markdown", "value": "\n`includesFrom(str, searchValue, start)` returns `true` if `searchValue` is found\nanywhere within `str` starting at character number `start` (where 0 is the\nfirst character), `false` otherwise.\nSee [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN.\n\n## Examples\n\n```rescript\nString.includesFrom(\"programmer\", \"gram\", 1) == true\nString.includesFrom(\"programmer\", \"gram\", 4) == false\nString.includesFrom(`대한민국`, `한`, 1) == true\n```\n"},
"sortText": "includesFrom",
"insertText": "->String.includesFrom",
"detail": "(string, string) => bool",
"documentation": {"kind": "markdown", "value": "\n`includes(str, searchValue)` returns `true` if `searchValue` is found anywhere\nwithin `str`, `false` otherwise.\nSee [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN.\n\n## Examples\n\n```rescript\nString.includes(\"programmer\", \"gram\") == true\nString.includes(\"programmer\", \"er\") == true\nString.includes(\"programmer\", \"pro\") == true\nString.includes(\"programmer.dat\", \"xyz\") == false\n```\n"},
"sortText": "includes",
"insertText": "->String.includes",
"additionalTextEdits": [{
"range": {"start": {"line": 89, "character": 55}, "end": {"line": 89, "character": 56}},
"newText": ""
}]
}, {
"label": "->String.includes",
"label": "->String.includesFrom",
"kind": 12,
"tags": [],
"detail": "(string, string) => bool",
"documentation": {"kind": "markdown", "value": "\n`includes(str, searchValue)` returns `true` if `searchValue` is found anywhere\nwithin `str`, `false` otherwise.\nSee [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN.\n\n## Examples\n\n```rescript\nString.includes(\"programmer\", \"gram\") == true\nString.includes(\"programmer\", \"er\") == true\nString.includes(\"programmer\", \"pro\") == true\nString.includes(\"programmer.dat\", \"xyz\") == false\n```\n"},
"sortText": "includes",
"insertText": "->String.includes",
"detail": "(string, string, int) => bool",
"documentation": {"kind": "markdown", "value": "\n`includesFrom(str, searchValue, start)` returns `true` if `searchValue` is found\nanywhere within `str` starting at character number `start` (where 0 is the\nfirst character), `false` otherwise.\nSee [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN.\n\n## Examples\n\n```rescript\nString.includesFrom(\"programmer\", \"gram\", 1) == true\nString.includesFrom(\"programmer\", \"gram\", 4) == false\nString.includesFrom(`대한민국`, `한`, 1) == true\n```\n"},
"sortText": "includesFrom",
"insertText": "->String.includesFrom",
"additionalTextEdits": [{
"range": {"start": {"line": 89, "character": 55}, "end": {"line": 89, "character": 56}},
"newText": ""
Expand Down
6 changes: 5 additions & 1 deletion tests/tests/src/core/Core_TempTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ let x = Symbol.for("Foo");

console.log(x);

let array$1 = "foo"[Symbol.iterator]().toArray();

console.log(array$1);

console.info("");

console.info("Global namespace");
Expand Down Expand Up @@ -334,7 +338,6 @@ export {
_formatter,
formatter,
segments,
array,
date,
dict,
dict2,
Expand All @@ -351,6 +354,7 @@ export {
set,
regexp,
x,
array$1 as array,
timeout,
z,
intFromBigInt,
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/src/core/Core_TempTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ Console.info("Symbol")
Console.info("---")
let x = Symbol.getFor("Foo")
Console.log(x)
let array: array<string> = String.getSymbolUnsafe("foo", Symbol.iterator)()->Iterator.toArray
Console.log(array)

Console.info("")
Console.info("Global namespace")
Expand Down