Skip to content

Commit 5797986

Browse files
authored
Add String.getSymbolUnsafe back to Stdlib (#7626)
* Add String.getSymbolUnsafe back to Stdlib * Add changelog entry
1 parent 91a322b commit 5797986

File tree

7 files changed

+70
-47
lines changed

7 files changed

+70
-47
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#### :rocket: New Feature
1616

1717
- 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
18+
- Add `String.getSymbolUnsafe` back to Stdlib. https://github.com/rescript-lang/rescript/pull/7626
1819

1920
#### :house: Internal
2021

runtime/Stdlib_String.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,5 @@ external splitByRegExpAtMost: (string, Stdlib_RegExp.t, ~limit: int) => array<op
167167
@send external localeCompare: (string, string) => float = "localeCompare"
168168

169169
external ignore: string => unit = "%ignore"
170+
171+
@get_index external getSymbolUnsafe: (string, Stdlib_Symbol.t) => 'a = ""

runtime/Stdlib_String.resi

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,3 +1137,17 @@ external localeCompare: (string, string) => float = "localeCompare"
11371137
without having to store or process it further.
11381138
*/
11391139
external ignore: string => unit = "%ignore"
1140+
1141+
/**
1142+
`getSymbolUnsafe(str, symbol)` returns the value of the symbol property of a string.
1143+
1144+
## Examples
1145+
1146+
```rescript
1147+
let it: Iterator.t<string> = ("foo"->String.getSymbolUnsafe(Symbol.iterator))()
1148+
Nullable.make(it)->Nullable.isNullable == false
1149+
```
1150+
1151+
*/
1152+
@get_index
1153+
external getSymbolUnsafe: (string, Stdlib_Symbol.t) => 'a = ""

tests/analysis_tests/tests/src/expected/CompletionJsx.res.txt

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -785,30 +785,6 @@ Path s
785785
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
786786
"newText": ""
787787
}]
788-
}, {
789-
"label": "->String.startsWith",
790-
"kind": 12,
791-
"tags": [],
792-
"detail": "(string, string) => bool",
793-
"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"},
794-
"sortText": "startsWith",
795-
"insertText": "->String.startsWith",
796-
"additionalTextEdits": [{
797-
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
798-
"newText": ""
799-
}]
800-
}, {
801-
"label": "->String.splitAtMost",
802-
"kind": 12,
803-
"tags": [],
804-
"detail": "(string, string, ~limit: int) => array<string>",
805-
"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"},
806-
"sortText": "splitAtMost",
807-
"insertText": "->String.splitAtMost",
808-
"additionalTextEdits": [{
809-
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
810-
"newText": ""
811-
}]
812788
}, {
813789
"label": "->String.searchOpt",
814790
"kind": 12,
@@ -845,6 +821,42 @@ Path s
845821
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
846822
"newText": ""
847823
}]
824+
}, {
825+
"label": "->String.substringToEnd",
826+
"kind": 12,
827+
"tags": [],
828+
"detail": "(string, ~start: int) => string",
829+
"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"},
830+
"sortText": "substringToEnd",
831+
"insertText": "->String.substringToEnd",
832+
"additionalTextEdits": [{
833+
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
834+
"newText": ""
835+
}]
836+
}, {
837+
"label": "->String.startsWith",
838+
"kind": 12,
839+
"tags": [],
840+
"detail": "(string, string) => bool",
841+
"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"},
842+
"sortText": "startsWith",
843+
"insertText": "->String.startsWith",
844+
"additionalTextEdits": [{
845+
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
846+
"newText": ""
847+
}]
848+
}, {
849+
"label": "->String.splitAtMost",
850+
"kind": 12,
851+
"tags": [],
852+
"detail": "(string, string, ~limit: int) => array<string>",
853+
"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"},
854+
"sortText": "splitAtMost",
855+
"insertText": "->String.splitAtMost",
856+
"additionalTextEdits": [{
857+
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
858+
"newText": ""
859+
}]
848860
}, {
849861
"label": "->String.sliceToEnd",
850862
"kind": 12,
@@ -917,17 +929,5 @@ Path s
917929
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
918930
"newText": ""
919931
}]
920-
}, {
921-
"label": "->String.substringToEnd",
922-
"kind": 12,
923-
"tags": [],
924-
"detail": "(string, ~start: int) => string",
925-
"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"},
926-
"sortText": "substringToEnd",
927-
"insertText": "->String.substringToEnd",
928-
"additionalTextEdits": [{
929-
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
930-
"newText": ""
931-
}]
932932
}]
933933

tests/analysis_tests/tests/src/expected/DotPipeCompletionSpec.res.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -418,25 +418,25 @@ Path Array.joinWith
418418
Path Stdlib.String.includ
419419
Path includ
420420
[{
421-
"label": "->String.includesFrom",
421+
"label": "->String.includes",
422422
"kind": 12,
423423
"tags": [],
424-
"detail": "(string, string, int) => bool",
425-
"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"},
426-
"sortText": "includesFrom",
427-
"insertText": "->String.includesFrom",
424+
"detail": "(string, string) => bool",
425+
"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"},
426+
"sortText": "includes",
427+
"insertText": "->String.includes",
428428
"additionalTextEdits": [{
429429
"range": {"start": {"line": 89, "character": 55}, "end": {"line": 89, "character": 56}},
430430
"newText": ""
431431
}]
432432
}, {
433-
"label": "->String.includes",
433+
"label": "->String.includesFrom",
434434
"kind": 12,
435435
"tags": [],
436-
"detail": "(string, string) => bool",
437-
"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"},
438-
"sortText": "includes",
439-
"insertText": "->String.includes",
436+
"detail": "(string, string, int) => bool",
437+
"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"},
438+
"sortText": "includesFrom",
439+
"insertText": "->String.includesFrom",
440440
"additionalTextEdits": [{
441441
"range": {"start": {"line": 89, "character": 55}, "end": {"line": 89, "character": 56}},
442442
"newText": ""

tests/tests/src/core/Core_TempTests.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ let x = Symbol.for("Foo");
249249

250250
console.log(x);
251251

252+
let array$1 = "foo"[Symbol.iterator]().toArray();
253+
254+
console.log(array$1);
255+
252256
console.info("");
253257

254258
console.info("Global namespace");
@@ -334,7 +338,6 @@ export {
334338
_formatter,
335339
formatter,
336340
segments,
337-
array,
338341
date,
339342
dict,
340343
dict2,
@@ -351,6 +354,7 @@ export {
351354
set,
352355
regexp,
353356
x,
357+
array$1 as array,
354358
timeout,
355359
z,
356360
intFromBigInt,

tests/tests/src/core/Core_TempTests.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ Console.info("Symbol")
160160
Console.info("---")
161161
let x = Symbol.getFor("Foo")
162162
Console.log(x)
163+
let array: array<string> = String.getSymbolUnsafe("foo", Symbol.iterator)()->Iterator.toArray
164+
Console.log(array)
163165

164166
Console.info("")
165167
Console.info("Global namespace")

0 commit comments

Comments
 (0)