diff --git a/runtime/Stdlib_String.resi b/runtime/Stdlib_String.resi index 3de15ffec7..d4c8fa193e 100644 --- a/runtime/Stdlib_String.resi +++ b/runtime/Stdlib_String.resi @@ -121,8 +121,30 @@ String.fromCodePointMany([0xd55c, 0xae00, 0x1f63a]) == `한글😺` @variadic @val external fromCodePointMany: array => string = "String.fromCodePoint" +/** +`equal(str1, str2)` checks if two strings are equal. + +## Examples + +```rescript +String.equal("hello", "hello") == true +String.equal("hello", "world") == false +String.equal("", "") == true +``` +*/ external equal: (string, string) => bool = "%equal" +/** +`compare(str1, str2)` compares two strings, returns an `Ordering.t` value. + +## Examples + +```rescript +String.compare("hello", "hello") == Ordering.equal +String.compare("apple", "banana") == Ordering.less +String.compare("zebra", "apple") == Ordering.greater +``` +*/ external compare: (string, string) => Stdlib_Ordering.t = "%compare" /** @@ -1090,9 +1112,48 @@ String.padEnd("abc", 1, "") == "abc" @send external padEnd: (string, int, string) => string = "padEnd" -// TODO: add docs +/** +`getSymbol(str, symbol)` returns the value associated with the given symbol on the string as an `option<'a>`. +Returns `None` if the symbol property doesn't exist. + +## Examples + +```rescript +let h = String.make("hello") +let symbol = String.getSymbol(h, Symbol.iterator) +Option.isSome(symbol) == true +``` +*/ @get_index external getSymbol: (string, Stdlib_Symbol.t) => option<'a> = "" + +/** +`getSymbolUnsafe(str, symbol)` returns the value associated with the given symbol on the string. + +This is _unsafe_, meaning it will return `undefined` if the symbol property doesn't exist. + +## Examples + +```rescript +let h = String.make("hello") +// This does required direct access to the symbol property +// Storing the iterator in a variable first, will not work. +String.getSymbolUnsafe(h, Symbol.iterator)()->Iterator.toArray->Array.length == 5 +``` +*/ @get_index external getSymbolUnsafe: (string, Stdlib_Symbol.t) => 'a = "" + +/** +`setSymbol(str, symbol, value)` sets the given symbol property on the string to the specified value. + +## Examples + +```rescript +let mySymbol = Symbol.make("test") +let h = %raw(`new String("hello")`) +String.setSymbol(h, mySymbol, 42) +String.getSymbol(h, mySymbol) == Some(42) +``` +*/ @set_index external setSymbol: (string, Stdlib_Symbol.t, 'a) => unit = "" /** diff --git a/tests/analysis_tests/tests/src/expected/CompletionJsx.res.txt b/tests/analysis_tests/tests/src/expected/CompletionJsx.res.txt index 412e810c27..a749612d28 100644 --- a/tests/analysis_tests/tests/src/expected/CompletionJsx.res.txt +++ b/tests/analysis_tests/tests/src/expected/CompletionJsx.res.txt @@ -874,7 +874,7 @@ Path s "kind": 12, "tags": [], "detail": "(string, Symbol.t, 'a) => unit", - "documentation": null, + "documentation": {"kind": "markdown", "value": "\n`setSymbol(str, symbol, value)` sets the given symbol property on the string to the specified value.\n\n## Examples\n\n```rescript\nlet mySymbol = Symbol.make(\"test\")\nlet h = %raw(`new String(\"hello\")`)\nString.setSymbol(h, mySymbol, 42)\nString.getSymbol(h, mySymbol) == Some(42)\n```\n"}, "sortText": "setSymbol", "insertText": "->String.setSymbol", "additionalTextEdits": [{ diff --git a/tests/tests/src/core/Test.mjs b/tests/tests/src/core/Test.mjs index 936183ec24..e4f2a10144 100644 --- a/tests/tests/src/core/Test.mjs +++ b/tests/tests/src/core/Test.mjs @@ -15,7 +15,7 @@ function print(value) { } else if (match === "string") { return Stdlib_Option.getExn(JSON.stringify(value), undefined); } else { - return String(value); + return new String(value); } }