From bf01c2a7bb2936b7420af02c6dbbfbfa2c9705cc Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sat, 11 Feb 2023 21:35:16 -0300 Subject: [PATCH 01/17] initial comments --- src/Core__String.resi | 284 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 284 insertions(+) create mode 100644 src/Core__String.resi diff --git a/src/Core__String.resi b/src/Core__String.resi new file mode 100644 index 00000000..cf2dc3c5 --- /dev/null +++ b/src/Core__String.resi @@ -0,0 +1,284 @@ +/*** Provide bindings to JS string.*/ + +/** +`make(value)` converts the given value to a `string`. + +```rescript +String.make(3.5) == "3.5" +String.make([1, 2, 3]) == "1,2,3" +``` +*/ +@val external make: 'a => string = "String" +/** +`fromCharCode(n)` creates a `string` containing the character corresponding to +that number; `n` ranges from 0 to 65535.If out of range, the lower 16 bits of +the value are used. Thus, `fromCharCode(0x1F63A)` gives the same result as +`fromCharCode(0xF63A)`. +See [`String.fromCharCode`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode) +on MDN. +```rescript +String.fromCharCode(65) == "A" +String.fromCharCode(0x3c8) == `ψ` +String.fromCharCode(0xd55c) == `한` +String.fromCharCode(-64568) == `ψ` +``` +*/ +@val external fromCharCode: int => string = "String.fromCharCode" +/** +`fromCharCodeMany([n1, n2, n3])` creates a `string` from the characters +corresponding to the given numbers, using the same rules as `fromCharCode`. +See [`String.fromCharCode`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode) +on MDN. +*/ +@variadic @val external fromCharCodeMany: array => string = "String.fromCharCode" +/** +`fromCodePoint(n)` creates a `string` containing the character corresponding to +that numeric code point. If the number is not a valid code point, it raises +`RangeError`. Thus, `fromCodePoint(0x1F63A)` will produce a correct value, +unlike `fromCharCode(0x1F63A)`, and `fromCodePoint(-5)` will raise a +`RangeError`. +See [`String.fromCodePoint`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint) +on MDN. +```rescript +String.fromCodePoint(65) == "A" +String.fromCodePoint(0x3c8) == `ψ` +String.fromCodePoint(0xd55c) == `한` +String.fromCodePoint(0x1f63a) == `😺` +``` +*/ +@val external fromCodePoint: int => string = "String.fromCodePoint" +/** +`fromCodePointMany([n1, n2, n3])` creates a `string` from the characters +corresponding to the given code point numbers, using the same rules as +`fromCodePoint`. +See [`String.fromCodePoint`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint) +on MDN. +```rescript +String.fromCodePointMany([0xd55c, 0xae00, 0x1f63a]) == `한글😺` +``` +*/ +@variadic @val external fromCodePointMany: array => string = "String.fromCodePoint" +/** +`length(s)` returns the length of the given `string`. +See [`String.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length) +on MDN. +```rescript +String.length("abcd") == 4 +``` +*/ +@get external length: string => int = "length" + +/** +`get(s, n)` returns as a `string` the character at the given index number. If +`n` is out of range, this function returns `undefined`,so at some point this +function may be modified to return `option(string)`. +```rescript +String.get("Reason", 0) == "R" +String.get("Reason", 4) == "o" +String.get(`Rẽasöń`, 5) == `ń` +``` +*/ +@get_index external get: (string, int) => option = "" +/** +`charAt(s, n)` gets the character at index `n` within string `s`. If `n` is +negative or greater than the length of `s`, it returns the empty string. If the +string contains characters outside the range \u0000-\uffff, it will return the +first 16-bit value at that position in the string. +See [`String.charAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt) +on MDN. +```rescript +String.charAt("Reason", 0) == "R" +String.charAt("Reason", 12) == "" +String.charAt(`Rẽasöń`, 5) == `ń` +``` +*/ +@send external charAt: (string, int) => string = "charAt" +/** +`charCodeAt(s, n)` returns the character code at position `n` in string `s`; +the result is in the range 0-65535, unlke `codePointAt`, so it will not work +correctly for characters with code points greater than or equal to 0x10000. The +return type is `float` because this function returns NaN if `n` is less than +zero or greater than the length of the string. +See [`String.charCodeAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt) +on MDN. +```rescript +String.charCodeAt(`😺`, 0) == 0xd83d->Belt.Int.toFloat +String.codePointAt(`😺`, 0) == Some(0x1f63a) +``` +*/ +@send external charCodeAt: (string, int) => float = "charCodeAt" +/** +`codePointAt(s, n)` returns the code point at position `n` within string `s` as +a `Some(value)`. The return value handles code points greater than or equal to +0x10000. If there is no code point at the given position, the function returns +`None`. +See [`String.codePointAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt) +on MDN. +```rescript +Js.String2.codePointAt(`¿😺?`, 1) == Some(0x1f63a) +Js.String2.codePointAt("abc", 5) == None +``` +*/ +@send external codePointAt: (string, int) => option = "codePointAt" +/** +`concat(original, append)` returns a new `string` with `append` added after +`original`. +See [`String.concat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat) +on MDN. +```rescript +String.concat("cow", "bell") == "cowbell" +``` +*/ +@send external concat: (string, string) => string = "concat" +/** +`concatMany(original, arr)` returns a new `string` consisting of each item of an +array of strings added to the `original` string. +See [`String.concat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat) +on MDN. +```rescript +String.concatMany("1st", ["2nd", "3rd", "4th"]) == "1st2nd3rd4th" +``` +*/ +@variadic @send external concatMany: (string, array) => string = "concat" +/** +ES2015: `endsWith(str, substr)` returns `true` if the `str` ends with `substr`, +`false` otherwise. +See [`String.endsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) +on MDN. +```rescript +String.endsWith("BuckleScript", "Script") == true +String.endsWith("BuckleShoes", "Script") == false +``` +*/ +@send external endsWith: (string, string) => bool = "endsWith" +/** +`endsWithFrom(str, ending, len)` returns `true` if the first len characters of +`str` end with `ending`, `false` otherwise. If `len` is greater than or equal +to the length of `str`, then it works like `endsWith`. (Honestly, this should +have been named endsWithAt, but oh well). +See [`String.endsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) +on MDN. +```rescript +String.endsWithFrom("abcd", "cd", 4) == true +String.endsWithFrom("abcde", "cd", 3) == false +String.endsWithFrom("abcde", "cde", 99) == true +String.endsWithFrom("example.dat", "ple", 7) == true +``` +*/ +@send external endsWithFrom: (string, string, int) => bool = "endsWith" +/** +ES2015: `includes(str, searchValue)` returns `true` if `searchValue` is found +anywhere within `str`, false otherwise. +See [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) +on MDN. +```rescript +String.includes("programmer", "gram") == true +String.includes("programmer", "er") == true +String.includes("programmer", "pro") == true +String.includes("programmer.dat", "xyz") == false +``` +*/ +@send external includes: (string, string) => bool = "includes" +/** +ES2015: `includes(str, searchValue start)` returns `true` if `searchValue` is +found anywhere within `str` starting at character number `start` (where 0 is +the first character), `false` otherwise. +See [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) +on MDN. +```rescript +String.includesFrom("programmer", "gram", 1) == true +String.includesFrom("programmer", "gram", 4) == false +String.includesFrom(`대한민국`, `한`, 1) == true +``` +*/ +@send external includesFrom: (string, string, int) => bool = "includes" +/** +ES2015: `indexOf(str, searchValue)` returns the position at which `searchValue` +was first found within `str`, or -1 if `searchValue` is not in `str`. +See [`String.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) +on MDN. +```rescript +String.indexOf("bookseller", "ok") == 2 +String.indexOf("bookseller", "sell") == 4 +String.indexOf("beekeeper", "ee") == 1 +String.indexOf("bookseller", "xyz") == -1 +``` +*/ +@send external indexOf: (string, string) => int = "indexOf" +let indexOfOpt: (string, string) => option +@send external indexOfFrom: (string, string, int) => int = "indexOf" +@send external lastIndexOf: (string, string) => int = "lastIndexOf" +let lastIndexOfOpt: (string, string) => option +@send external lastIndexOfFrom: (string, string, int) => int = "lastIndexOf" +@return(nullable) @send +external match: (string, Core__RegExp.t) => option = "match" +type normalizeForm = [#NFC | #NFD | #NFKC | #NFKD] +@send external normalize: string => string = "normalize" +@send external normalizeByForm: (string, normalizeForm) => string = "normalize" +@send external repeat: (string, int) => string = "repeat" +@send external replace: (string, string, string) => string = "replace" +@send external replaceRegExp: (string, Core__RegExp.t, string) => string = "replace" +@send +external unsafeReplaceRegExpBy0: ( + string, + Core__RegExp.t, + (@uncurry ~match: string, ~offset: int, ~input: string) => string, +) => string = "replace" +@send +external unsafeReplaceRegExpBy1: ( + string, + Core__RegExp.t, + (@uncurry ~match: string, ~group1: string, ~offset: int, ~input: string) => string, +) => string = "replace" +@send +external unsafeReplaceRegExpBy2: ( + string, + Core__RegExp.t, + ( + @uncurry ~match: string, + ~group1: string, + ~group2: string, + ~offset: int, + ~input: string, + ) => string, +) => string = "replace" +@send +external unsafeReplaceRegExpBy3: ( + string, + Core__RegExp.t, + ( + @uncurry ~match: string, + ~group1: string, + ~group2: string, + ~group3: string, + ~offset: int, + ~input: string, + ) => string, +) => string = "replace" +@send external search: (string, Core__RegExp.t) => int = "search" +let searchOpt: (string, Core__RegExp.t) => option +@send external slice: (string, ~start: int, ~end: int) => string = "slice" +@send external sliceToEnd: (string, ~start: int) => string = "slice" +@send external split: (string, string) => array = "split" +@send external splitAtMost: (string, string, ~limit: int) => array = "split" +@send external splitByRegExp: (string, Core__RegExp.t) => array> = "split" +@send +external splitByRegExpAtMost: (string, Core__RegExp.t, ~limit: int) => array> = + "split" +@send external startsWith: (string, string) => bool = "startsWith" +@send external startsWithFrom: (string, string, int) => bool = "startsWith" +@send external substring: (string, ~start: int, ~end: int) => string = "substring" +@send external substringToEnd: (string, ~start: int) => string = "substring" +@send external toLowerCase: string => string = "toLowerCase" +@send external toLocaleLowerCase: string => string = "toLocaleLowerCase" +@send external toUpperCase: string => string = "toUpperCase" +@send external toLocaleUpperCase: string => string = "toLocaleUpperCase" +@send external trim: string => string = "trim" +@send external trimStart: string => string = "trimStart" +@send external trimEnd: string => string = "trimEnd" +@send external padStart: (string, int, string) => string = "padStart" +@send external padEnd: (string, int, string) => string = "padEnd" +@get_index external getSymbol: (string, Core__Symbol.t) => option<'a> = "" +@get_index external getSymbolUnsafe: (string, Core__Symbol.t) => 'a = "" +@set_index external setSymbol: (string, Core__Symbol.t, 'a) => unit = "" +@send external localeCompare: (string, string) => float = "localeCompare" From e2e0d05b8b21deabca033704f37a69d72fb4fbff Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sun, 12 Feb 2023 01:03:10 -0300 Subject: [PATCH 02/17] update --- src/Core__String.resi | 541 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 496 insertions(+), 45 deletions(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index cf2dc3c5..887311cc 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -2,13 +2,14 @@ /** `make(value)` converts the given value to a `string`. - ```rescript String.make(3.5) == "3.5" String.make([1, 2, 3]) == "1,2,3" ``` */ -@val external make: 'a => string = "String" +@val +external make: 'a => string = "String" + /** `fromCharCode(n)` creates a `string` containing the character corresponding to that number; `n` ranges from 0 to 65535.If out of range, the lower 16 bits of @@ -23,14 +24,19 @@ String.fromCharCode(0xd55c) == `한` String.fromCharCode(-64568) == `ψ` ``` */ -@val external fromCharCode: int => string = "String.fromCharCode" +@val +external fromCharCode: int => string = "String.fromCharCode" + /** `fromCharCodeMany([n1, n2, n3])` creates a `string` from the characters corresponding to the given numbers, using the same rules as `fromCharCode`. See [`String.fromCharCode`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode) on MDN. */ -@variadic @val external fromCharCodeMany: array => string = "String.fromCharCode" +@variadic +@val +external fromCharCodeMany: array => string = "String.fromCharCode" + /** `fromCodePoint(n)` creates a `string` containing the character corresponding to that numeric code point. If the number is not a valid code point, it raises @@ -46,7 +52,9 @@ String.fromCodePoint(0xd55c) == `한` String.fromCodePoint(0x1f63a) == `😺` ``` */ -@val external fromCodePoint: int => string = "String.fromCodePoint" +@val +external fromCodePoint: int => string = "String.fromCodePoint" + /** `fromCodePointMany([n1, n2, n3])` creates a `string` from the characters corresponding to the given code point numbers, using the same rules as @@ -57,7 +65,10 @@ on MDN. String.fromCodePointMany([0xd55c, 0xae00, 0x1f63a]) == `한글😺` ``` */ -@variadic @val external fromCodePointMany: array => string = "String.fromCodePoint" +@variadic +@val +external fromCodePointMany: array => string = "String.fromCodePoint" + /** `length(s)` returns the length of the given `string`. See [`String.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length) @@ -66,7 +77,8 @@ on MDN. String.length("abcd") == 4 ``` */ -@get external length: string => int = "length" +@get +external length: string => int = "length" /** `get(s, n)` returns as a `string` the character at the given index number. If @@ -78,7 +90,9 @@ String.get("Reason", 4) == "o" String.get(`Rẽasöń`, 5) == `ń` ``` */ -@get_index external get: (string, int) => option = "" +@get_index +external get: (string, int) => option = "" + /** `charAt(s, n)` gets the character at index `n` within string `s`. If `n` is negative or greater than the length of `s`, it returns the empty string. If the @@ -92,7 +106,9 @@ String.charAt("Reason", 12) == "" String.charAt(`Rẽasöń`, 5) == `ń` ``` */ -@send external charAt: (string, int) => string = "charAt" +@send +external charAt: (string, int) => string = "charAt" + /** `charCodeAt(s, n)` returns the character code at position `n` in string `s`; the result is in the range 0-65535, unlke `codePointAt`, so it will not work @@ -106,7 +122,9 @@ String.charCodeAt(`😺`, 0) == 0xd83d->Belt.Int.toFloat String.codePointAt(`😺`, 0) == Some(0x1f63a) ``` */ -@send external charCodeAt: (string, int) => float = "charCodeAt" +@send +external charCodeAt: (string, int) => float = "charCodeAt" + /** `codePointAt(s, n)` returns the code point at position `n` within string `s` as a `Some(value)`. The return value handles code points greater than or equal to @@ -119,7 +137,9 @@ Js.String2.codePointAt(`¿😺?`, 1) == Some(0x1f63a) Js.String2.codePointAt("abc", 5) == None ``` */ -@send external codePointAt: (string, int) => option = "codePointAt" +@send +external codePointAt: (string, int) => option = "codePointAt" + /** `concat(original, append)` returns a new `string` with `append` added after `original`. @@ -129,7 +149,9 @@ on MDN. String.concat("cow", "bell") == "cowbell" ``` */ -@send external concat: (string, string) => string = "concat" +@send +external concat: (string, string) => string = "concat" + /** `concatMany(original, arr)` returns a new `string` consisting of each item of an array of strings added to the `original` string. @@ -139,7 +161,10 @@ on MDN. String.concatMany("1st", ["2nd", "3rd", "4th"]) == "1st2nd3rd4th" ``` */ -@variadic @send external concatMany: (string, array) => string = "concat" +@variadic +@send +external concatMany: (string, array) => string = "concat" + /** ES2015: `endsWith(str, substr)` returns `true` if the `str` ends with `substr`, `false` otherwise. @@ -150,7 +175,9 @@ String.endsWith("BuckleScript", "Script") == true String.endsWith("BuckleShoes", "Script") == false ``` */ -@send external endsWith: (string, string) => bool = "endsWith" +@send +external endsWith: (string, string) => bool = "endsWith" + /** `endsWithFrom(str, ending, len)` returns `true` if the first len characters of `str` end with `ending`, `false` otherwise. If `len` is greater than or equal @@ -165,7 +192,9 @@ String.endsWithFrom("abcde", "cde", 99) == true String.endsWithFrom("example.dat", "ple", 7) == true ``` */ -@send external endsWithFrom: (string, string, int) => bool = "endsWith" +@send +external endsWithFrom: (string, string, int) => bool = "endsWith" + /** ES2015: `includes(str, searchValue)` returns `true` if `searchValue` is found anywhere within `str`, false otherwise. @@ -178,7 +207,9 @@ String.includes("programmer", "pro") == true String.includes("programmer.dat", "xyz") == false ``` */ -@send external includes: (string, string) => bool = "includes" +@send +external includes: (string, string) => bool = "includes" + /** ES2015: `includes(str, searchValue start)` returns `true` if `searchValue` is found anywhere within `str` starting at character number `start` (where 0 is @@ -191,7 +222,9 @@ String.includesFrom("programmer", "gram", 4) == false String.includesFrom(`대한민국`, `한`, 1) == true ``` */ -@send external includesFrom: (string, string, int) => bool = "includes" +@send +external includesFrom: (string, string, int) => bool = "includes" + /** ES2015: `indexOf(str, searchValue)` returns the position at which `searchValue` was first found within `str`, or -1 if `searchValue` is not in `str`. @@ -204,32 +237,216 @@ String.indexOf("beekeeper", "ee") == 1 String.indexOf("bookseller", "xyz") == -1 ``` */ -@send external indexOf: (string, string) => int = "indexOf" +@send +external indexOf: (string, string) => int = "indexOf" let indexOfOpt: (string, string) => option -@send external indexOfFrom: (string, string, int) => int = "indexOf" -@send external lastIndexOf: (string, string) => int = "lastIndexOf" + +/** +`indexOfFrom(str, searchValue, start)` returns the position at which +`searchValue` was found within `str` starting at character position `start`, or +-1 if `searchValue` is not found in that portion of `str`. The return value is +relative to the beginning of the string, no matter where the search started +from. +See [`String.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) +on MDN. +```rescript +String.indexOfFrom("bookseller", "ok", 1) == 2 +String.indexOfFrom("bookseller", "sell", 2) == 4 +String.indexOfFrom("bookseller", "sell", 5) == -1 +``` +*/ +@send +external indexOfFrom: (string, string, int) => int = "indexOf" + +/** +`lastIndexOf(str, searchValue)` returns the position of the last occurrence of +`searchValue` within `str`, searching backwards from the end of the string. +Returns -1 if `searchValue` is not in `str`. The return value is always +relative to the beginning of the string. +See [`String.lastIndexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) +on MDN. +```rescript +String.lastIndexOf("bookseller", "ok") == 2 +String.lastIndexOf("beekeeper", "ee") == 4 +String.lastIndexOf("abcdefg", "xyz") == -1 +``` +*/ +@send +external lastIndexOf: (string, string) => int = "lastIndexOf" let lastIndexOfOpt: (string, string) => option -@send external lastIndexOfFrom: (string, string, int) => int = "lastIndexOf" -@return(nullable) @send + +/** +`lastIndexOfFrom(str, searchValue, start)` returns the position of the last +occurrence of `searchValue` within `str`, searching backwards from the given +start position. Returns -1 if `searchValue` is not in `str`. The return value +is always relative to the beginning of the string. +See [`String.lastIndexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) +on MDN. +```rescript +String.lastIndexOfFrom("bookseller", "ok", 6) == 2 +String.lastIndexOfFrom("beekeeper", "ee", 8) == 4 +String.lastIndexOfFrom("beekeeper", "ee", 3) == 1 +String.lastIndexOfFrom("abcdefg", "xyz", 4) == -1 +``` +*/ +@send +external lastIndexOfFrom: (string, string, int) => int = "lastIndexOf" + +/** +`match(str, regexp)` matches a `string` against the given `regexp`. If there is +no match, it returns `None`. For regular expressions without the g modifier, if + there is a match, the return value is `Some(array)` where the array contains: +- The entire matched string +- Any capture groups if the regexp had parentheses +For regular expressions with the g modifier, a matched expression returns +`Some(array)` with all the matched substrings and no capture groups. +See [`String.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) +on MDN. +```rescript +String.match("The better bats", %re("/b[aeiou]t/")) == Some(["bet"]) +String.match("The better bats", %re("/b[aeiou]t/g")) == Some(["bet", "bat"]) +String.match("Today is 2018-04-05.", %re("/(\d+)-(\d+)-(\d+)/")) == + Some(["2018-04-05", "2018", "04", "05"]) +String.match("The large container.", %re("/b[aeiou]g/")) == None +``` +*/ +@return(nullable) +@send external match: (string, Core__RegExp.t) => option = "match" type normalizeForm = [#NFC | #NFD | #NFKC | #NFKD] -@send external normalize: string => string = "normalize" -@send external normalizeByForm: (string, normalizeForm) => string = "normalize" -@send external repeat: (string, int) => string = "repeat" -@send external replace: (string, string, string) => string = "replace" -@send external replaceRegExp: (string, Core__RegExp.t, string) => string = "replace" + +/** +`normalize(str)` returns the normalized Unicode string using Normalization Form +Canonical (NFC) Composition. Consider the character ã, which can be represented +as the single codepoint \u00e3 or the combination of a lower case letter A +\u0061 and a combining tilde \u0303. Normalization ensures that both can be +stored in an equivalent binary representation. +See [`String.normalize`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) +on MDN. See also [Unicode technical report +#15](https://unicode.org/reports/tr15/) for details. +*/ +@send +external normalize: string => string = "normalize" + +/** +ES2015: `normalize(str, form)` returns the normalized Unicode string using the +specified form of normalization, which may be one of: +- "NFC" — Normalization Form Canonical Composition. +- "NFD" — Normalization Form Canonical Decomposition. +- "NFKC" — Normalization Form Compatibility Composition. +- "NFKD" — Normalization Form Compatibility Decomposition. +See [`String.normalize`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) on MDN. +See also [Unicode technical report #15](https://unicode.org/reports/tr15/) for details. +*/ +@send +external normalizeByForm: (string, normalizeForm) => string = "normalize" + +/** +`repeat(str, n)` returns a `string` that consists of `n` repetitions of `str`. +Raises `RangeError` if `n` is negative. +See [`String.repeat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat) +on MDN. +```rescript +String.repeat("ha", 3) == "hahaha" +String.repeat("empty", 0) == "" +``` +*/ +@send +external repeat: (string, int) => string = "repeat" + +/** +ES2015: `replace(str, substr, newSubstr)` returns a new `string` which is +identical to `str` except with the first matching instance of `substr` replaced +by `newSubstr`. `substr` is treated as a verbatim string to match, not a +regular expression. +See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) +on MDN. +```rescript +String.replace("old string", "old", "new") == "new string" +String.replace("the cat and the dog", "the", "this") == "this cat and the dog" +``` +*/ +@send +external replace: (string, string, string) => string = "replace" + +/** +`replaceByRe(str, regex, replacement)` returns a new `string` where occurrences +matching regex have been replaced by `replacement`. +See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) +on MDN. +```rescript +String.replaceByRe("vowels be gone", %re("/[aeiou]/g"), "x") == "vxwxls bx gxnx" +String.replaceByRe("Juan Fulano", %re("/(\w+) (\w+)/"), "$2, $1") == "Fulano, Juan" +``` +*/ +@send +external replaceRegExp: (string, Core__RegExp.t, string) => string = "replace" + +/** +Returns a new `string` with some or all matches of a pattern with no capturing +parentheses replaced by the value returned from the given function. The +function receives as its parameters the matched string, the offset at which the +match begins, and the whole string being matched. +See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) +on MDN. +```rescript +let str = "beautiful vowels" +let re = %re("/[aeiou]/g") +let matchFn = (matchPart, _offset, _wholeString) => String.toUpperCase(matchPart) +String.unsafeReplaceBy0(str, re, matchFn) == "bEAUtIfUl vOwEls" +``` +*/ @send external unsafeReplaceRegExpBy0: ( string, Core__RegExp.t, (@uncurry ~match: string, ~offset: int, ~input: string) => string, ) => string = "replace" + +/** +Returns a new `string` with some or all matches of a pattern with one set of +capturing parentheses replaced by the value returned from the given function. +The function receives as its parameters the matched string, the captured +string, the offset at which the match begins, and the whole string being +matched. +See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) +on MDN. +```rescript +let str = "Jony is 40" +let re = %re("/(Jony is )\d+/g") +let matchFn = (_match, part1, _offset, _wholeString) => { + part1 ++ "41" +} +String.unsafeReplaceBy1(str, re, matchFn) == "Jony is 41" +``` +*/ @send external unsafeReplaceRegExpBy1: ( string, Core__RegExp.t, (@uncurry ~match: string, ~group1: string, ~offset: int, ~input: string) => string, ) => string = "replace" + +/** +Returns a new `string` with some or all matches of a pattern with two sets of +capturing parentheses replaced by the value returned from the given function. +The function receives as its parameters the matched string, the captured +strings, the offset at which the match begins, and the whole string being +matched. +See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) +on MDN. +```rescript +let str = "7 times 6" +let re = %re("/(\d+) times (\d+)/") +let matchFn = (_match, p1, p2, _offset, _wholeString) => { + switch (Belt.Int.fromString(p1), Belt.Int.fromString(p2)) { + | (Some(x), Some(y)) => Belt.Int.toString(x * y) + | _ => "???" + } +} +String.unsafeReplaceBy2(str, re, matchFn) == "42" +``` +*/ @send external unsafeReplaceRegExpBy2: ( string, @@ -242,6 +459,16 @@ external unsafeReplaceRegExpBy2: ( ~input: string, ) => string, ) => string = "replace" + +/** +Returns a new `string` with some or all matches of a pattern with three sets of +capturing parentheses replaced by the value returned from the given function. +The function receives as its parameters the matched string, the captured +strings, the offset at which the match begins, and the whole string being +matched. +See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) +on MDN. +*/ @send external unsafeReplaceRegExpBy3: ( string, @@ -255,28 +482,252 @@ external unsafeReplaceRegExpBy3: ( ~input: string, ) => string, ) => string = "replace" -@send external search: (string, Core__RegExp.t) => int = "search" + +/** +`search(str, regexp)` returns the starting position of the first match of +`regexp` in the given `str`, or -1 if there is no match. +See [`String.search`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search) +on MDN. +```rescript +String.search("testing 1 2 3", %re("/\d+/")) == 8 +String.search("no numbers", %re("/\d+/")) == -1 +``` +*/ +@send +external search: (string, Core__RegExp.t) => int = "search" let searchOpt: (string, Core__RegExp.t) => option -@send external slice: (string, ~start: int, ~end: int) => string = "slice" -@send external sliceToEnd: (string, ~start: int) => string = "slice" -@send external split: (string, string) => array = "split" -@send external splitAtMost: (string, string, ~limit: int) => array = "split" -@send external splitByRegExp: (string, Core__RegExp.t) => array> = "split" + +/** +`slice(str, start:n1, end:n2)` returns the substring of `str` starting at +character `n1` up to but not including `n2`. +- If either `n1` or `n2` is negative, then it is evaluated as `length(str - n1)` or `length(str - n2)`. +- If `n2` is greater than the length of `str`, then it is treated as `length(str)`. +- If `n1` is greater than `n2`, slice returns the empty string. +See [`String.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) on MDN. +```rescript +String.slice("abcdefg", ~start=2, ~end=5) == "cde" +String.slice("abcdefg", ~start=2, ~end=9) == "cdefg" +String.slice("abcdefg", ~start=-4, ~end=-2) == "de" +String.slice("abcdefg", ~start=5, ~end=1) == "" +``` +*/ +@send +external slice: (string, ~start: int, ~end: int) => string = "slice" + +/** +`sliceToEnd(str, ~start:n)` returns the substring of `str` starting at character +`n` to the end of the string. +- If `n` is negative, then it is evaluated as `length(str - n)`. +- If `n` is greater than the length of `str`, then sliceToEnd returns the empty string. +See [`String.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) on MDN. +```rescript +String.sliceToEnd("abcdefg", ~start=4) == "efg" +String.sliceToEnd("abcdefg", ~start=-2) == "fg" +String.sliceToEnd("abcdefg", ~start=7) == "" +``` +*/ +@send +external sliceToEnd: (string, ~start: int) => string = "slice" + +/** +`split(str, delimiter)` splits the given `str` at every occurrence of +`delimiter` and returns an array of the resulting substrings. +See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) +on MDN. +```rescript +String.split("2018-01-02", "-") == ["2018", "01", "02"] +String.split("a,b,,c", ",") == ["a", "b", "", "c"] +String.split("good::bad as great::awful", "::") == ["good", "bad as great", "awful"] +String.split("has-no-delimiter", ";") == ["has-no-delimiter"] +``` +*/ +@send +external split: (string, string) => array = "split" + +/** +`splitAtMost(str, delimiter, ~limit: n)` splits the given `str` at every occurrence of `delimiter` +and returns an array of the first `n` resulting substrings. If `n` is negative or greater than +the number of substrings, the array will contain all the substrings. +```rescript +String.splitAtMost("ant/bee/cat/dog/elk", "/", ~limit=3) = ["ant", "bee", "cat"] +String.splitAtMost("ant/bee/cat/dog/elk", "/", ~limit=0) = [] +String.splitAtMost("ant/bee/cat/dog/elk", "/", ~limit=9) = ["ant", "bee", "cat", "dog", "elk"] +``` +*/ +@send +external splitAtMost: (string, string, ~limit: int) => array = "split" + +/** +`splitByRe(str, regex)` splits the given `str` at every occurrence of `regex` +and returns an array of the resulting substrings. +See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) +on MDN. +*/ +@send +external splitByRegExp: (string, Core__RegExp.t) => array> = "split" + +/** +`splitByReAtMost(str, regex, ~limit:n)` splits the given `str` at every +occurrence of `regex` and returns an array of the first `n` resulting +substrings. If `n` is negative or greater than the number of substrings, the +array will contain all the substrings. +See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) +on MDN. +*/ @send external splitByRegExpAtMost: (string, Core__RegExp.t, ~limit: int) => array> = "split" -@send external startsWith: (string, string) => bool = "startsWith" -@send external startsWithFrom: (string, string, int) => bool = "startsWith" -@send external substring: (string, ~start: int, ~end: int) => string = "substring" -@send external substringToEnd: (string, ~start: int) => string = "substring" -@send external toLowerCase: string => string = "toLowerCase" -@send external toLocaleLowerCase: string => string = "toLocaleLowerCase" -@send external toUpperCase: string => string = "toUpperCase" -@send external toLocaleUpperCase: string => string = "toLocaleUpperCase" -@send external trim: string => string = "trim" -@send external trimStart: string => string = "trimStart" -@send external trimEnd: string => string = "trimEnd" -@send external padStart: (string, int, string) => string = "padStart" + +/** +ES2015: `startsWith(str, substr)` returns `true` if the `str` starts with +`substr`, `false` otherwise. +See [`String.startsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) +on MDN. +```rescript +String.startsWith("BuckleScript", "Buckle") == true +String.startsWith("BuckleScript", "") == true +String.startsWith("JavaScript", "Buckle") == false +``` +*/ +@send +external startsWith: (string, string) => bool = "startsWith" + +/** +ES2015: `startsWithFrom(str, substr, n)` returns `true` if the `str` starts +with `substr` starting at position `n`, false otherwise. If `n` is negative, +the search starts at the beginning of `str`. +See [`String.startsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) +on MDN. +```rescript +String.startsWithFrom("BuckleScript", "kle", 3) == true +String.startsWithFrom("BuckleScript", "", 3) == true +String.startsWithFrom("JavaScript", "Buckle", 2) == false +``` +*/ +@send +external startsWithFrom: (string, string, int) => bool = "startsWith" + +/** +`substring(str, ~start: start, ~end: end)` returns characters `start` up to +but not including end from `str`. +- If `start` is less than zero, it is treated as zero. +- If `end` is zero or negative, the empty string is returned. +- If `start` is greater than `end`, the `start` and `end` points are swapped. +See [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) on MDN. +```rescript +String.substring("playground", ~start=3, ~end=6) == "ygr" +String.substring("playground", ~start=6, ~end=3) == "ygr" +String.substring("playground", ~start=4, ~end=12) == "ground" +``` +*/ +@send +external substring: (string, ~start: int, ~end: int) => string = "substring" + +/** +`substringToEnd(str, ~start: start)` returns the substring of `str` from +position `start` to the end. +- If `start` is less than or equal to zero, the entire string is returned. +- If `start` is greater than or equal to the length of `str`, the empty string is returned. +See [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) on MDN. +```rescript +String.substringToEnd("playground", ~start=4) == "ground" +String.substringToEnd("playground", ~start=-3) == "playground" +String.substringToEnd("playground", ~start=12) == "" +``` +*/ +@send +external substringToEnd: (string, ~start: int) => string = "substring" + +/** +`toLowerCase(str)` converts `str` to lower case using the locale-insensitive +case mappings in the Unicode Character Database. Notice that the conversion can +give different results depending upon context, for example with the Greek +letter sigma, which has two different lower case forms; one when it is the last +character in a string and another when it is not. +See [`String.toLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase) +on MDN. +```rescript +String.toLowerCase("ABC") == "abc" +String.toLowerCase(`ΣΠ`) == `σπ` +String.toLowerCase(`ΠΣ`) == `πς` +``` +*/ +@send +external toLowerCase: string => string = "toLowerCase" + +/** +`toLocaleLowerCase(str)` converts `str` to lower case using the current locale. +See [`String.toLocaleLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase) +on MDN. +*/ +@send +external toLocaleLowerCase: string => string = "toLocaleLowerCase" + +/** +`toUpperCase(str)` converts `str` to upper case using the locale-insensitive +case mappings in the Unicode Character Database. Notice that the conversion can +expand the number of letters in the result; for example the German ß +capitalizes to two Ses in a row. +See [`String.toUpperCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) +on MDN. +```rescript +String.toUpperCase("abc") == "ABC" +String.toUpperCase(`Straße`) == `STRASSE` +String.toUpperCase(`πς`) == `ΠΣ` +``` +*/ +@send +external toUpperCase: string => string = "toUpperCase" + +/** +`toLocaleUpperCase(str)` converts `str` to upper case using the current locale. +See [`String.to:LocaleUpperCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase) +on MDN. +*/ +@send +external toLocaleUpperCase: string => string = "toLocaleUpperCase" + +/** +`trim(str)` returns a string that is `str` with whitespace stripped from both +ends. Internal whitespace is not removed. +See [`String.trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim) +on MDN. +```rescript +String.trim(" abc def ") == "abc def" +String.trim("\n\r\t abc def \n\n\t\r ") == "abc def" +``` +*/ +@send +external trim: string => string = "trim" + +/** +`trimStart(str)` returns a string that is `str` with whitespace stripped from the beginning of a string. +Internal whitespace is not removed. +See [`String.trimStart`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart) +on MDN. +```rescript +String.trimStart(" Hello world! ") == "Hello world! " +String.trimStart(" Hello world! ") == "Hello world! " +``` +*/ +@send +external trimStart: string => string = "trimStart" + +/** +`trinEnd(str)` returns a string that is `str` with whitespace stripped from the end of a string. +Internal whitespace is not removed. +See [`String.trimStart`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart) +on MDN. +```rescript +String.trimStart(" Hello world! ") == " Hello world!" +String.trimStart(" Hello world! ") == " Hello world!" +``` +*/ +@send +external trimEnd: string => string = "trimEnd" + +@send +external padStart: (string, int, string) => string = "padStart" @send external padEnd: (string, int, string) => string = "padEnd" @get_index external getSymbol: (string, Core__Symbol.t) => option<'a> = "" @get_index external getSymbolUnsafe: (string, Core__Symbol.t) => 'a = "" From 6fa68d1eb61fc7a786d50d29bfc537e59aa78079 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sun, 12 Feb 2023 01:54:52 -0300 Subject: [PATCH 03/17] some fixes --- src/Core__String.resi | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index 887311cc..ac427892 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -81,7 +81,7 @@ String.length("abcd") == 4 external length: string => int = "length" /** -`get(s, n)` returns as a `string` the character at the given index number. If +`get(str, n)` returns as a `string` the character at the given index number. If `n` is out of range, this function returns `undefined`,so at some point this function may be modified to return `option(string)`. ```rescript @@ -94,10 +94,10 @@ String.get(`Rẽasöń`, 5) == `ń` external get: (string, int) => option = "" /** -`charAt(s, n)` gets the character at index `n` within string `s`. If `n` is -negative or greater than the length of `s`, it returns the empty string. If the -string contains characters outside the range \u0000-\uffff, it will return the -first 16-bit value at that position in the string. +`charAt(str, n)` gets the character at index `n` within string `str`. If `n` is +negative or greater than the length of `str`, it returns the empty string. If +the string contains characters outside the range \u0000-\uffff, it will return +the first 16-bit value at that position in the string. See [`String.charAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt) on MDN. ```rescript @@ -110,7 +110,7 @@ String.charAt(`Rẽasöń`, 5) == `ń` external charAt: (string, int) => string = "charAt" /** -`charCodeAt(s, n)` returns the character code at position `n` in string `s`; +`charCodeAt(str, n)` returns the character code at position `n` in string `str` the result is in the range 0-65535, unlke `codePointAt`, so it will not work correctly for characters with code points greater than or equal to 0x10000. The return type is `float` because this function returns NaN if `n` is less than @@ -126,10 +126,10 @@ String.codePointAt(`😺`, 0) == Some(0x1f63a) external charCodeAt: (string, int) => float = "charCodeAt" /** -`codePointAt(s, n)` returns the code point at position `n` within string `s` as -a `Some(value)`. The return value handles code points greater than or equal to -0x10000. If there is no code point at the given position, the function returns -`None`. +`codePointAt(str, n)` returns the code point at position `n` within string `str` +as a `Some(value)`. The return value handles code points greater than or equal +to 0x10000. If there is no code point at the given position, the function +returns `None`. See [`String.codePointAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt) on MDN. ```rescript @@ -197,7 +197,7 @@ external endsWithFrom: (string, string, int) => bool = "endsWith" /** ES2015: `includes(str, searchValue)` returns `true` if `searchValue` is found -anywhere within `str`, false otherwise. +anywhere within `str`, `false` otherwise. See [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN. ```rescript @@ -594,7 +594,7 @@ external startsWith: (string, string) => bool = "startsWith" /** ES2015: `startsWithFrom(str, substr, n)` returns `true` if the `str` starts -with `substr` starting at position `n`, false otherwise. If `n` is negative, +with `substr` starting at position `n`, `false` otherwise. If `n` is negative, the search starts at the beginning of `str`. See [`String.startsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) on MDN. @@ -681,7 +681,7 @@ external toUpperCase: string => string = "toUpperCase" /** `toLocaleUpperCase(str)` converts `str` to upper case using the current locale. -See [`String.to:LocaleUpperCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase) +See [`String.toLocaleUpperCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase) on MDN. */ @send @@ -701,8 +701,8 @@ String.trim("\n\r\t abc def \n\n\t\r ") == "abc def" external trim: string => string = "trim" /** -`trimStart(str)` returns a string that is `str` with whitespace stripped from the beginning of a string. -Internal whitespace is not removed. +`trimStart(str)` returns a string that is `str` with whitespace stripped from +the beginning of a string. Internal whitespace is not removed. See [`String.trimStart`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart) on MDN. ```rescript @@ -714,13 +714,13 @@ String.trimStart(" Hello world! ") == "Hello world! " external trimStart: string => string = "trimStart" /** -`trinEnd(str)` returns a string that is `str` with whitespace stripped from the end of a string. -Internal whitespace is not removed. -See [`String.trimStart`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart) +`trinEnd(str)` returns a string that is `str` with whitespace stripped from the +end of a string. Internal whitespace is not removed. +See [`String.trimEnd`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd) on MDN. ```rescript -String.trimStart(" Hello world! ") == " Hello world!" -String.trimStart(" Hello world! ") == " Hello world!" +String.trimEnd(" Hello world! ") == " Hello world!" +String.trimEnd(" Hello world! ") == " Hello world!" ``` */ @send From 38c95c8b821e7562dc62367fe323908b4ba20411 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sun, 12 Feb 2023 02:20:48 -0300 Subject: [PATCH 04/17] add copyright comment --- src/Core__String.resi | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index ac427892..a90a4d21 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -1,3 +1,27 @@ +/* Copyright (C) 2015-2016 Bloomberg Finance L.P. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * In addition to the permissions granted to you by the LGPL, you may combine + * or link a "work that uses the Library" with a publicly distributed version + * of this file to produce a combined library or application, then distribute + * that combined work under the terms of your choosing, with no requirement + * to comply with the obligations normally placed on you by section 4 of the + * LGPL version 3 (or the corresponding section of a later version of the LGPL + * should you choose to use a later version). + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + /*** Provide bindings to JS string.*/ /** @@ -70,7 +94,7 @@ String.fromCodePointMany([0xd55c, 0xae00, 0x1f63a]) == `한글😺` external fromCodePointMany: array => string = "String.fromCodePoint" /** -`length(s)` returns the length of the given `string`. +`length(str)` returns the length of the given `string`. See [`String.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length) on MDN. ```rescript From 2f24c1ad2a2e6c87d9da574928df20bbb55cfd2b Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sun, 12 Feb 2023 04:56:55 -0300 Subject: [PATCH 05/17] updates --- src/Core__String.resi | 74 ++++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index a90a4d21..ea08f0b6 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -105,9 +105,8 @@ String.length("abcd") == 4 external length: string => int = "length" /** -`get(str, n)` returns as a `string` the character at the given index number. If -`n` is out of range, this function returns `undefined`,so at some point this -function may be modified to return `option(string)`. +`get(str, n)` returns as a `option` at the given index number. If `n` +is out of range, this function returns `None`. ```rescript String.get("Reason", 0) == "R" String.get("Reason", 4) == "o" @@ -119,8 +118,8 @@ external get: (string, int) => option = "" /** `charAt(str, n)` gets the character at index `n` within string `str`. If `n` is -negative or greater than the length of `str`, it returns the empty string. If -the string contains characters outside the range \u0000-\uffff, it will return +negative or greater than the length of `str`, it returns the empty string. If +the string contains characters outside the range \u0000-\uffff, it will return the first 16-bit value at that position in the string. See [`String.charAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt) on MDN. @@ -251,7 +250,7 @@ external includesFrom: (string, string, int) => bool = "includes" /** ES2015: `indexOf(str, searchValue)` returns the position at which `searchValue` -was first found within `str`, or -1 if `searchValue` is not in `str`. +was first found within `str`, or `-1` if `searchValue` is not in `str`. See [`String.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) on MDN. ```rescript @@ -268,7 +267,7 @@ let indexOfOpt: (string, string) => option /** `indexOfFrom(str, searchValue, start)` returns the position at which `searchValue` was found within `str` starting at character position `start`, or --1 if `searchValue` is not found in that portion of `str`. The return value is +`-1` if `searchValue` is not found in that portion of `str`. The return value is relative to the beginning of the string, no matter where the search started from. See [`String.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) @@ -285,7 +284,7 @@ external indexOfFrom: (string, string, int) => int = "indexOf" /** `lastIndexOf(str, searchValue)` returns the position of the last occurrence of `searchValue` within `str`, searching backwards from the end of the string. -Returns -1 if `searchValue` is not in `str`. The return value is always +Returns `-1` if `searchValue` is not in `str`. The return value is always relative to the beginning of the string. See [`String.lastIndexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) on MDN. @@ -302,7 +301,7 @@ let lastIndexOfOpt: (string, string) => option /** `lastIndexOfFrom(str, searchValue, start)` returns the position of the last occurrence of `searchValue` within `str`, searching backwards from the given -start position. Returns -1 if `searchValue` is not in `str`. The return value +start position. Returns `-1` if `searchValue` is not in `str`. The return value is always relative to the beginning of the string. See [`String.lastIndexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) on MDN. @@ -329,8 +328,7 @@ on MDN. ```rescript String.match("The better bats", %re("/b[aeiou]t/")) == Some(["bet"]) String.match("The better bats", %re("/b[aeiou]t/g")) == Some(["bet", "bat"]) -String.match("Today is 2018-04-05.", %re("/(\d+)-(\d+)-(\d+)/")) == - Some(["2018-04-05", "2018", "04", "05"]) +String.match("Today is 2018-04-05.", %re("/(\d+)-(\d+)-(\d+)/")) == Some(["2018-04-05", "2018", "04", "05"]) String.match("The large container.", %re("/b[aeiou]g/")) == None ``` */ @@ -522,7 +520,7 @@ external search: (string, Core__RegExp.t) => int = "search" let searchOpt: (string, Core__RegExp.t) => option /** -`slice(str, start:n1, end:n2)` returns the substring of `str` starting at +`slice(str, ~start:n1, ~end:n2)` returns the substring of `str` starting at character `n1` up to but not including `n2`. - If either `n1` or `n2` is negative, then it is evaluated as `length(str - n1)` or `length(str - n2)`. - If `n2` is greater than the length of `str`, then it is treated as `length(str)`. @@ -569,9 +567,10 @@ String.split("has-no-delimiter", ";") == ["has-no-delimiter"] external split: (string, string) => array = "split" /** -`splitAtMost(str, delimiter, ~limit: n)` splits the given `str` at every occurrence of `delimiter` -and returns an array of the first `n` resulting substrings. If `n` is negative or greater than -the number of substrings, the array will contain all the substrings. +`splitAtMost(str, delimiter, ~limit:n)` splits the given `str` at every +occurrence of `delimiter` and returns an array of the first `n` resulting +substrings. If `n` is negative or greater than the number of substrings, the +array will contain all the substrings. ```rescript String.splitAtMost("ant/bee/cat/dog/elk", "/", ~limit=3) = ["ant", "bee", "cat"] String.splitAtMost("ant/bee/cat/dog/elk", "/", ~limit=0) = [] @@ -632,8 +631,8 @@ String.startsWithFrom("JavaScript", "Buckle", 2) == false external startsWithFrom: (string, string, int) => bool = "startsWith" /** -`substring(str, ~start: start, ~end: end)` returns characters `start` up to -but not including end from `str`. +`substring(str, ~start:start, ~end:end)` returns characters `start` up to but +not including end from `str`. - If `start` is less than zero, it is treated as zero. - If `end` is zero or negative, the empty string is returned. - If `start` is greater than `end`, the `start` and `end` points are swapped. @@ -648,7 +647,7 @@ String.substring("playground", ~start=4, ~end=12) == "ground" external substring: (string, ~start: int, ~end: int) => string = "substring" /** -`substringToEnd(str, ~start: start)` returns the substring of `str` from +`substringToEnd(str, ~start:start)` returns the substring of `str` from position `start` to the end. - If `start` is less than or equal to zero, the entire string is returned. - If `start` is greater than or equal to the length of `str`, the empty string is returned. @@ -750,10 +749,47 @@ String.trimEnd(" Hello world! ") == " Hello world!" @send external trimEnd: string => string = "trimEnd" +/** +`padStart(str, n, padStr)` returns a string that has been padded with `padStr` +(multiple times, if needed) until the resulting string reaches the given `n` +length. The padding is applied from the start of the current string. +See [`String.padStart`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart) +on MDN. +```rescript +String.padStart("abc", 5, " ") == " abc" +String.padStart("abc", 6, "123465") == "123abc" +``` +*/ @send external padStart: (string, int, string) => string = "padStart" -@send external padEnd: (string, int, string) => string = "padEnd" + +/** +`padEnd(str, n, padStr)` returns a string that has been padded with `padStr` +(multiple times, if needed) until the resulting string reaches the given `n` +length. The padding is applied from the end of the current string. +See [`String.padEnd`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd) +on MDN. +```rescript +String.padEnd("Hello", 10, ".") == "Hello....." +String.padEnd("abc", 1, "") == "abc" +``` +*/ +@send +external padEnd: (string, int, string) => string = "padEnd" @get_index external getSymbol: (string, Core__Symbol.t) => option<'a> = "" @get_index external getSymbolUnsafe: (string, Core__Symbol.t) => 'a = "" @set_index external setSymbol: (string, Core__Symbol.t, 'a) => unit = "" + +/** +`localeCompare(referenceStr, compareStr)` returns a float than indicatings +whether a reference string comes before or fater, or is the same as the given +string in sort order. If `referenceStr` occurs before `compareStr` positive if +the `referenceStr` occurs after `compareStr`; `0` if they are equivalent. +Do not rely on exact return values of `-1` or `1` +See [`String.localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) +```rescript +String.localeCompare("a", "c") < 0. == true +String.localeCompare("a", "a") == 0. +``` +*/ @send external localeCompare: (string, string) => float = "localeCompare" From e3b52216e4ec70be862572053e25fe46ff0f6368 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sun, 12 Feb 2023 05:49:12 -0300 Subject: [PATCH 06/17] update module comment Co-authored-by: Gabriel Nordeborn --- src/Core__String.resi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index ea08f0b6..23c9bd2a 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -22,7 +22,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*** Provide bindings to JS string.*/ +/*** Functions for interacting with JavaScript strings. +See: [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String).*/ /** `make(value)` converts the given value to a `string`. From 4b7206837aefc70272c470a225e01bb4f078e573 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sun, 12 Feb 2023 05:54:43 -0300 Subject: [PATCH 07/17] some fixes --- src/Core__String.resi | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index 23c9bd2a..42c2d469 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -142,7 +142,7 @@ zero or greater than the length of the string. See [`String.charCodeAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt) on MDN. ```rescript -String.charCodeAt(`😺`, 0) == 0xd83d->Belt.Int.toFloat +String.charCodeAt(`😺`, 0) == 0xd83d->Int.toFloat String.codePointAt(`😺`, 0) == Some(0x1f63a) ``` */ @@ -157,8 +157,8 @@ returns `None`. See [`String.codePointAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt) on MDN. ```rescript -Js.String2.codePointAt(`¿😺?`, 1) == Some(0x1f63a) -Js.String2.codePointAt("abc", 5) == None +String.codePointAt(`¿😺?`, 1) == Some(0x1f63a) +String.codePointAt("abc", 5) == None ``` */ @send @@ -202,11 +202,11 @@ String.endsWith("BuckleShoes", "Script") == false @send external endsWith: (string, string) => bool = "endsWith" +// NOTE: Honestly, this should have been named endsWithAt, but oh well /** `endsWithFrom(str, ending, len)` returns `true` if the first len characters of `str` end with `ending`, `false` otherwise. If `len` is greater than or equal -to the length of `str`, then it works like `endsWith`. (Honestly, this should -have been named endsWithAt, but oh well). +to the length of `str`, then it works like `endsWith`. See [`String.endsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) on MDN. ```rescript @@ -462,8 +462,8 @@ on MDN. let str = "7 times 6" let re = %re("/(\d+) times (\d+)/") let matchFn = (_match, p1, p2, _offset, _wholeString) => { - switch (Belt.Int.fromString(p1), Belt.Int.fromString(p2)) { - | (Some(x), Some(y)) => Belt.Int.toString(x * y) + switch (Int.fromString(p1), Int.fromString(p2)) { + | (Some(x), Some(y)) => Int.toString(x * y) | _ => "???" } } From e7cb1a535e0a5c4a3e05be87e316c34ce61e9c65 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sun, 12 Feb 2023 18:39:54 -0300 Subject: [PATCH 08/17] update --- src/Core__String.resi | 169 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 164 insertions(+), 5 deletions(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index 42c2d469..16dc53d9 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -27,6 +27,9 @@ See: [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc /** `make(value)` converts the given value to a `string`. + +## Examples + ```rescript String.make(3.5) == "3.5" String.make([1, 2, 3]) == "1,2,3" @@ -42,6 +45,9 @@ the value are used. Thus, `fromCharCode(0x1F63A)` gives the same result as `fromCharCode(0xF63A)`. See [`String.fromCharCode`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode) on MDN. + +## Examples + ```rescript String.fromCharCode(65) == "A" String.fromCharCode(0x3c8) == `ψ` @@ -70,6 +76,9 @@ unlike `fromCharCode(0x1F63A)`, and `fromCodePoint(-5)` will raise a `RangeError`. See [`String.fromCodePoint`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint) on MDN. + +## Examples + ```rescript String.fromCodePoint(65) == "A" String.fromCodePoint(0x3c8) == `ψ` @@ -86,6 +95,9 @@ corresponding to the given code point numbers, using the same rules as `fromCodePoint`. See [`String.fromCodePoint`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint) on MDN. + +## Examples + ```rescript String.fromCodePointMany([0xd55c, 0xae00, 0x1f63a]) == `한글😺` ``` @@ -98,6 +110,9 @@ external fromCodePointMany: array => string = "String.fromCodePoint" `length(str)` returns the length of the given `string`. See [`String.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length) on MDN. + +## Examples + ```rescript String.length("abcd") == 4 ``` @@ -108,6 +123,9 @@ external length: string => int = "length" /** `get(str, n)` returns as a `option` at the given index number. If `n` is out of range, this function returns `None`. + +## Examples + ```rescript String.get("Reason", 0) == "R" String.get("Reason", 4) == "o" @@ -124,6 +142,9 @@ the string contains characters outside the range \u0000-\uffff, it will return the first 16-bit value at that position in the string. See [`String.charAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt) on MDN. + +## Examples + ```rescript String.charAt("Reason", 0) == "R" String.charAt("Reason", 12) == "" @@ -141,6 +162,9 @@ return type is `float` because this function returns NaN if `n` is less than zero or greater than the length of the string. See [`String.charCodeAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt) on MDN. + +## Examples + ```rescript String.charCodeAt(`😺`, 0) == 0xd83d->Int.toFloat String.codePointAt(`😺`, 0) == Some(0x1f63a) @@ -156,6 +180,9 @@ to 0x10000. If there is no code point at the given position, the function returns `None`. See [`String.codePointAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt) on MDN. + +## Examples + ```rescript String.codePointAt(`¿😺?`, 1) == Some(0x1f63a) String.codePointAt("abc", 5) == None @@ -169,8 +196,12 @@ external codePointAt: (string, int) => option = "codePointAt" `original`. See [`String.concat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat) on MDN. + +## Examples + ```rescript String.concat("cow", "bell") == "cowbell" +String.concat("Re", "Script") == "ReScript" ``` */ @send @@ -181,6 +212,9 @@ external concat: (string, string) => string = "concat" array of strings added to the `original` string. See [`String.concat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat) on MDN. + +## Examples + ```rescript String.concatMany("1st", ["2nd", "3rd", "4th"]) == "1st2nd3rd4th" ``` @@ -194,6 +228,9 @@ ES2015: `endsWith(str, substr)` returns `true` if the `str` ends with `substr`, `false` otherwise. See [`String.endsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) on MDN. + +## Examples + ```rescript String.endsWith("BuckleScript", "Script") == true String.endsWith("BuckleShoes", "Script") == false @@ -209,6 +246,9 @@ external endsWith: (string, string) => bool = "endsWith" to the length of `str`, then it works like `endsWith`. See [`String.endsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) on MDN. + +## Examples + ```rescript String.endsWithFrom("abcd", "cd", 4) == true String.endsWithFrom("abcde", "cd", 3) == false @@ -224,6 +264,9 @@ ES2015: `includes(str, searchValue)` returns `true` if `searchValue` is found anywhere within `str`, `false` otherwise. See [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN. + +## Examples + ```rescript String.includes("programmer", "gram") == true String.includes("programmer", "er") == true @@ -240,6 +283,9 @@ found anywhere within `str` starting at character number `start` (where 0 is the first character), `false` otherwise. See [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN. + +## Examples + ```rescript String.includesFrom("programmer", "gram", 1) == true String.includesFrom("programmer", "gram", 4) == false @@ -254,6 +300,9 @@ ES2015: `indexOf(str, searchValue)` returns the position at which `searchValue` was first found within `str`, or `-1` if `searchValue` is not in `str`. See [`String.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) on MDN. + +## Examples + ```rescript String.indexOf("bookseller", "ok") == 2 String.indexOf("bookseller", "sell") == 4 @@ -273,6 +322,9 @@ relative to the beginning of the string, no matter where the search started from. See [`String.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) on MDN. + +## Examples + ```rescript String.indexOfFrom("bookseller", "ok", 1) == 2 String.indexOfFrom("bookseller", "sell", 2) == 4 @@ -289,6 +341,9 @@ Returns `-1` if `searchValue` is not in `str`. The return value is always relative to the beginning of the string. See [`String.lastIndexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) on MDN. + +## Examples + ```rescript String.lastIndexOf("bookseller", "ok") == 2 String.lastIndexOf("beekeeper", "ee") == 4 @@ -306,6 +361,9 @@ start position. Returns `-1` if `searchValue` is not in `str`. The return value is always relative to the beginning of the string. See [`String.lastIndexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) on MDN. + +## Examples + ```rescript String.lastIndexOfFrom("bookseller", "ok", 6) == 2 String.lastIndexOfFrom("beekeeper", "ee", 8) == 4 @@ -326,6 +384,9 @@ For regular expressions with the g modifier, a matched expression returns `Some(array)` with all the matched substrings and no capture groups. See [`String.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) on MDN. + +## Examples + ```rescript String.match("The better bats", %re("/b[aeiou]t/")) == Some(["bet"]) String.match("The better bats", %re("/b[aeiou]t/g")) == Some(["bet", "bat"]) @@ -347,6 +408,18 @@ stored in an equivalent binary representation. See [`String.normalize`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) on MDN. See also [Unicode technical report #15](https://unicode.org/reports/tr15/) for details. + +## Examples + +```rescript +let string1 = "\uFB00" +let string2 = "\u0066\u0066" +Console.log(string1 === string2) // false + +let normalizeString1 = String.normalize(string1, "NFKD") +let normalizeString2 = String.normalize(string2, "NFKD") +assert(normalizeString1 === normalizeString2) +``` */ @send external normalize: string => string = "normalize" @@ -360,6 +433,18 @@ specified form of normalization, which may be one of: - "NFKD" — Normalization Form Compatibility Decomposition. See [`String.normalize`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) on MDN. See also [Unicode technical report #15](https://unicode.org/reports/tr15/) for details. + +## Examples + +```rescript +let string1 = "\uFB00" +let string2 = "\u0066\u0066" +Console.log(string1 === string2) // false + +let normalizeString1 = String.normalizeByForm(string1, #NFKD) +let normalizeString2 = String.normalizeByForm(string2, #NFKD) +assert(normalizeString1 === normalizeString2) +``` */ @send external normalizeByForm: (string, normalizeForm) => string = "normalize" @@ -369,6 +454,9 @@ external normalizeByForm: (string, normalizeForm) => string = "normalize" Raises `RangeError` if `n` is negative. See [`String.repeat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat) on MDN. + +## Examples + ```rescript String.repeat("ha", 3) == "hahaha" String.repeat("empty", 0) == "" @@ -384,6 +472,9 @@ by `newSubstr`. `substr` is treated as a verbatim string to match, not a regular expression. See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. + +## Examples + ```rescript String.replace("old string", "old", "new") == "new string" String.replace("the cat and the dog", "the", "this") == "this cat and the dog" @@ -397,6 +488,9 @@ external replace: (string, string, string) => string = "replace" matching regex have been replaced by `replacement`. See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. + +## Examples + ```rescript String.replaceByRe("vowels be gone", %re("/[aeiou]/g"), "x") == "vxwxls bx gxnx" String.replaceByRe("Juan Fulano", %re("/(\w+) (\w+)/"), "$2, $1") == "Fulano, Juan" @@ -412,6 +506,9 @@ function receives as its parameters the matched string, the offset at which the match begins, and the whole string being matched. See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. + +## Examples + ```rescript let str = "beautiful vowels" let re = %re("/[aeiou]/g") @@ -434,6 +531,9 @@ string, the offset at which the match begins, and the whole string being matched. See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. + +## Examples + ```rescript let str = "Jony is 40" let re = %re("/(Jony is )\d+/g") @@ -458,6 +558,9 @@ strings, the offset at which the match begins, and the whole string being matched. See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. + +## Examples + ```rescript let str = "7 times 6" let re = %re("/(\d+) times (\d+)/") @@ -511,6 +614,9 @@ external unsafeReplaceRegExpBy3: ( `regexp` in the given `str`, or -1 if there is no match. See [`String.search`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search) on MDN. + +## Examples + ```rescript String.search("testing 1 2 3", %re("/\d+/")) == 8 String.search("no numbers", %re("/\d+/")) == -1 @@ -526,7 +632,11 @@ character `n1` up to but not including `n2`. - If either `n1` or `n2` is negative, then it is evaluated as `length(str - n1)` or `length(str - n2)`. - If `n2` is greater than the length of `str`, then it is treated as `length(str)`. - If `n1` is greater than `n2`, slice returns the empty string. -See [`String.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) on MDN. +See [`String.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) +on MDN. + +## Examples + ```rescript String.slice("abcdefg", ~start=2, ~end=5) == "cde" String.slice("abcdefg", ~start=2, ~end=9) == "cdefg" @@ -542,7 +652,11 @@ external slice: (string, ~start: int, ~end: int) => string = "slice" `n` to the end of the string. - If `n` is negative, then it is evaluated as `length(str - n)`. - If `n` is greater than the length of `str`, then sliceToEnd returns the empty string. -See [`String.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) on MDN. +See [`String.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) +on MDN. + +## Examples + ```rescript String.sliceToEnd("abcdefg", ~start=4) == "efg" String.sliceToEnd("abcdefg", ~start=-2) == "fg" @@ -557,6 +671,9 @@ external sliceToEnd: (string, ~start: int) => string = "slice" `delimiter` and returns an array of the resulting substrings. See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) on MDN. + +## Examples + ```rescript String.split("2018-01-02", "-") == ["2018", "01", "02"] String.split("a,b,,c", ",") == ["a", "b", "", "c"] @@ -572,6 +689,9 @@ external split: (string, string) => array = "split" occurrence of `delimiter` and returns an array of the first `n` resulting substrings. If `n` is negative or greater than the number of substrings, the array will contain all the substrings. + +## Examples + ```rescript String.splitAtMost("ant/bee/cat/dog/elk", "/", ~limit=3) = ["ant", "bee", "cat"] String.splitAtMost("ant/bee/cat/dog/elk", "/", ~limit=0) = [] @@ -607,6 +727,9 @@ ES2015: `startsWith(str, substr)` returns `true` if the `str` starts with `substr`, `false` otherwise. See [`String.startsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) on MDN. + +## Examples + ```rescript String.startsWith("BuckleScript", "Buckle") == true String.startsWith("BuckleScript", "") == true @@ -622,6 +745,9 @@ with `substr` starting at position `n`, `false` otherwise. If `n` is negative, the search starts at the beginning of `str`. See [`String.startsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) on MDN. + +## Examples + ```rescript String.startsWithFrom("BuckleScript", "kle", 3) == true String.startsWithFrom("BuckleScript", "", 3) == true @@ -637,7 +763,11 @@ not including end from `str`. - If `start` is less than zero, it is treated as zero. - If `end` is zero or negative, the empty string is returned. - If `start` is greater than `end`, the `start` and `end` points are swapped. -See [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) on MDN. +See [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) +on MDN. + +## Examples + ```rescript String.substring("playground", ~start=3, ~end=6) == "ygr" String.substring("playground", ~start=6, ~end=3) == "ygr" @@ -652,7 +782,11 @@ external substring: (string, ~start: int, ~end: int) => string = "substring" position `start` to the end. - If `start` is less than or equal to zero, the entire string is returned. - If `start` is greater than or equal to the length of `str`, the empty string is returned. -See [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) on MDN. +See [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) +on MDN. + +## Examples + ```rescript String.substringToEnd("playground", ~start=4) == "ground" String.substringToEnd("playground", ~start=-3) == "playground" @@ -670,6 +804,9 @@ letter sigma, which has two different lower case forms; one when it is the last character in a string and another when it is not. See [`String.toLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase) on MDN. + +## Examples + ```rescript String.toLowerCase("ABC") == "abc" String.toLowerCase(`ΣΠ`) == `σπ` @@ -694,6 +831,9 @@ expand the number of letters in the result; for example the German ß capitalizes to two Ses in a row. See [`String.toUpperCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) on MDN. + +## Examples + ```rescript String.toUpperCase("abc") == "ABC" String.toUpperCase(`Straße`) == `STRASSE` @@ -716,6 +856,9 @@ external toLocaleUpperCase: string => string = "toLocaleUpperCase" ends. Internal whitespace is not removed. See [`String.trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim) on MDN. + +## Examples + ```rescript String.trim(" abc def ") == "abc def" String.trim("\n\r\t abc def \n\n\t\r ") == "abc def" @@ -729,6 +872,9 @@ external trim: string => string = "trim" the beginning of a string. Internal whitespace is not removed. See [`String.trimStart`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart) on MDN. + +## Examples + ```rescript String.trimStart(" Hello world! ") == "Hello world! " String.trimStart(" Hello world! ") == "Hello world! " @@ -742,6 +888,9 @@ external trimStart: string => string = "trimStart" end of a string. Internal whitespace is not removed. See [`String.trimEnd`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd) on MDN. + +## Examples + ```rescript String.trimEnd(" Hello world! ") == " Hello world!" String.trimEnd(" Hello world! ") == " Hello world!" @@ -756,6 +905,9 @@ external trimEnd: string => string = "trimEnd" length. The padding is applied from the start of the current string. See [`String.padStart`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart) on MDN. + +## Examples + ```rescript String.padStart("abc", 5, " ") == " abc" String.padStart("abc", 6, "123465") == "123abc" @@ -770,6 +922,9 @@ external padStart: (string, int, string) => string = "padStart" length. The padding is applied from the end of the current string. See [`String.padEnd`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd) on MDN. + +## Examples + ```rescript String.padEnd("Hello", 10, ".") == "Hello....." String.padEnd("abc", 1, "") == "abc" @@ -788,9 +943,13 @@ string in sort order. If `referenceStr` occurs before `compareStr` positive if the `referenceStr` occurs after `compareStr`; `0` if they are equivalent. Do not rely on exact return values of `-1` or `1` See [`String.localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) + +## Examples + ```rescript String.localeCompare("a", "c") < 0. == true String.localeCompare("a", "a") == 0. ``` */ -@send external localeCompare: (string, string) => float = "localeCompare" +@send +external localeCompare: (string, string) => float = "localeCompare" From 70b3966abcab8f98e79d5567e6ec12ef1d95f810 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sun, 12 Feb 2023 19:46:42 -0300 Subject: [PATCH 09/17] update --- src/Core__String.resi | 44 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index 16dc53d9..5417d2cd 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -22,8 +22,10 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*** Functions for interacting with JavaScript strings. -See: [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String).*/ +/*** +Functions for interacting with JavaScript strings. +See: [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String). +*/ /** `make(value)` converts the given value to a `string`. @@ -312,6 +314,8 @@ String.indexOf("bookseller", "xyz") == -1 */ @send external indexOf: (string, string) => int = "indexOf" + +//TODO let indexOfOpt: (string, string) => option /** @@ -352,6 +356,8 @@ String.lastIndexOf("abcdefg", "xyz") == -1 */ @send external lastIndexOf: (string, string) => int = "lastIndexOf" + +//TODO let lastIndexOfOpt: (string, string) => option /** @@ -397,7 +403,6 @@ String.match("The large container.", %re("/b[aeiou]g/")) == None @return(nullable) @send external match: (string, Core__RegExp.t) => option = "match" -type normalizeForm = [#NFC | #NFD | #NFKC | #NFKD] /** `normalize(str)` returns the normalized Unicode string using Normalization Form @@ -425,7 +430,7 @@ assert(normalizeString1 === normalizeString2) external normalize: string => string = "normalize" /** -ES2015: `normalize(str, form)` returns the normalized Unicode string using the +ES2015: `normalizeByForm(str, form)` returns the normalized Unicode string using the specified form of normalization, which may be one of: - "NFC" — Normalization Form Canonical Composition. - "NFD" — Normalization Form Canonical Decomposition. @@ -446,6 +451,7 @@ let normalizeString2 = String.normalizeByForm(string2, #NFKD) assert(normalizeString1 === normalizeString2) ``` */ +type normalizeForm = [#NFC | #NFD | #NFKC | #NFKD] @send external normalizeByForm: (string, normalizeForm) => string = "normalize" @@ -484,7 +490,7 @@ String.replace("the cat and the dog", "the", "this") == "this cat and the dog" external replace: (string, string, string) => string = "replace" /** -`replaceByRe(str, regex, replacement)` returns a new `string` where occurrences +`replaceRegExp(str, regex, replacement)` returns a new `string` where occurrences matching regex have been replaced by `replacement`. See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. @@ -492,8 +498,8 @@ on MDN. ## Examples ```rescript -String.replaceByRe("vowels be gone", %re("/[aeiou]/g"), "x") == "vxwxls bx gxnx" -String.replaceByRe("Juan Fulano", %re("/(\w+) (\w+)/"), "$2, $1") == "Fulano, Juan" +String.replaceRegExp("vowels be gone", %re("/[aeiou]/g"), "x") == "vxwxls bx gxnx" +String.replaceRegExp("Juan Fulano", %re("/(\w+) (\w+)/"), "$2, $1") == "Fulano, Juan" ``` */ @send @@ -624,6 +630,8 @@ String.search("no numbers", %re("/\d+/")) == -1 */ @send external search: (string, Core__RegExp.t) => int = "search" + +// TODO: let searchOpt: (string, Core__RegExp.t) => option /** @@ -702,21 +710,37 @@ String.splitAtMost("ant/bee/cat/dog/elk", "/", ~limit=9) = ["ant", "bee", "cat", external splitAtMost: (string, string, ~limit: int) => array = "split" /** -`splitByRe(str, regex)` splits the given `str` at every occurrence of `regex` +`splitByRegExp(str, regex)` splits the given `str` at every occurrence of `regex` and returns an array of the resulting substrings. See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) on MDN. + +## Examples + +```rescript +String.splitByRegExp("Jan,Feb,Mar", %re("/,/")) == [Some("Jan"), Some("Feb"), Some("Mar")] +``` */ @send external splitByRegExp: (string, Core__RegExp.t) => array> = "split" /** -`splitByReAtMost(str, regex, ~limit:n)` splits the given `str` at every +`splitByRegExpAtMost(str, regex, ~limit:n)` splits the given `str` at every occurrence of `regex` and returns an array of the first `n` resulting substrings. If `n` is negative or greater than the number of substrings, the array will contain all the substrings. See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) on MDN. + +## Examples + +```rescript +String.splitByRegExpAtMost("Hello World. How are you doing?", %re("/ /"), ~limit=3) == [ + Some("Hello"), + Some("World."), + Some("How"), + ] +``` */ @send external splitByRegExpAtMost: (string, Core__RegExp.t, ~limit: int) => array> = @@ -932,6 +956,8 @@ String.padEnd("abc", 1, "") == "abc" */ @send external padEnd: (string, int, string) => string = "padEnd" + +//TODO @get_index external getSymbol: (string, Core__Symbol.t) => option<'a> = "" @get_index external getSymbolUnsafe: (string, Core__Symbol.t) => 'a = "" @set_index external setSymbol: (string, Core__Symbol.t, 'a) => unit = "" From 0ff5cbb3f48dfd88fc9c7a160386a61adc0cffa0 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Mon, 13 Feb 2023 00:36:21 -0300 Subject: [PATCH 10/17] update --- src/Core__String.resi | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index 5417d2cd..4e3ddbf8 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -65,6 +65,13 @@ external fromCharCode: int => string = "String.fromCharCode" corresponding to the given numbers, using the same rules as `fromCharCode`. See [`String.fromCharCode`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode) on MDN. + +## Examples + +```rescript +String.fromCharCodeMany([189, 43, 190, 61]) == "½+¾=" +String.fromCharCode([65, 66, 67]) == "ABC" +``` */ @variadic @val @@ -280,8 +287,8 @@ String.includes("programmer.dat", "xyz") == false external includes: (string, string) => bool = "includes" /** -ES2015: `includes(str, searchValue start)` returns `true` if `searchValue` is -found anywhere within `str` starting at character number `start` (where 0 is +ES2015: `includesFrom(str, searchValue start)` returns `true` if `searchValue` +is found anywhere within `str` starting at character number `start` (where 0 is the first character), `false` otherwise. See [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN. @@ -383,7 +390,7 @@ external lastIndexOfFrom: (string, string, int) => int = "lastIndexOf" /** `match(str, regexp)` matches a `string` against the given `regexp`. If there is no match, it returns `None`. For regular expressions without the g modifier, if - there is a match, the return value is `Some(array)` where the array contains: +there is a match, the return value is `Some(array)` where the array contains: - The entire matched string - Any capture groups if the regexp had parentheses For regular expressions with the g modifier, a matched expression returns @@ -519,7 +526,7 @@ on MDN. let str = "beautiful vowels" let re = %re("/[aeiou]/g") let matchFn = (matchPart, _offset, _wholeString) => String.toUpperCase(matchPart) -String.unsafeReplaceBy0(str, re, matchFn) == "bEAUtIfUl vOwEls" +String.unsafeReplaceRegExpBy0(str, re, matchFn) == "bEAUtIfUl vOwEls" ``` */ @send @@ -546,7 +553,7 @@ let re = %re("/(Jony is )\d+/g") let matchFn = (_match, part1, _offset, _wholeString) => { part1 ++ "41" } -String.unsafeReplaceBy1(str, re, matchFn) == "Jony is 41" +String.unsafeReplaceRegExpBy1(str, re, matchFn) == "Jony is 41" ``` */ @send @@ -576,7 +583,7 @@ let matchFn = (_match, p1, p2, _offset, _wholeString) => { | _ => "???" } } -String.unsafeReplaceBy2(str, re, matchFn) == "42" +String.unsafeReplaceRegExpBy2(str, re, matchFn) == "42" ``` */ @send From 77955bc87a138157fa57ac320ab025276a98ff1c Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Mon, 13 Feb 2023 18:26:03 -0300 Subject: [PATCH 11/17] update --- src/Core__String.resi | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index 4e3ddbf8..c5ffd124 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -136,9 +136,9 @@ is out of range, this function returns `None`. ## Examples ```rescript -String.get("Reason", 0) == "R" -String.get("Reason", 4) == "o" -String.get(`Rẽasöń`, 5) == `ń` +String.get("Reason", 0) == Some("R") +String.get("Reason", 4) == Some("o") +String.get(`Rẽasöń`, 5) == Some(`ń`) ``` */ @get_index @@ -322,7 +322,16 @@ String.indexOf("bookseller", "xyz") == -1 @send external indexOf: (string, string) => int = "indexOf" -//TODO +/** +`indexOfOpt(str, searchValue)`. Like `indexOf`, but return a `option`. + +## Examples + +```rescript +String.indexOf("bookseller", "ok") == Some(2) +String.indexOf("bookseller", "xyz") == None +``` +*/ let indexOfOpt: (string, string) => option /** @@ -364,7 +373,18 @@ String.lastIndexOf("abcdefg", "xyz") == -1 @send external lastIndexOf: (string, string) => int = "lastIndexOf" -//TODO +/** +`lastIndexOfOpt(str, searchValue)`. Like `lastIndexOfOpt`, but return a +`option`. + +## Examples + +```rescript +String.lastIndexOf("bookseller", "ok") == Some(2) +String.lastIndexOf("beekeeper", "ee") == Some(4) +String.lastIndexOf("abcdefg", "xyz") == None +``` +*/ let lastIndexOfOpt: (string, string) => option /** @@ -638,7 +658,16 @@ String.search("no numbers", %re("/\d+/")) == -1 @send external search: (string, Core__RegExp.t) => int = "search" -// TODO: +/** +`searchOpt(str, regexp)`. Like `search`, but return a `option`. + +## Examples + +```rescript +String.search("testing 1 2 3", %re("/\d+/")) == Some(8) +String.search("no numbers", %re("/\d+/")) == None +``` +*/ let searchOpt: (string, Core__RegExp.t) => option /** @@ -964,7 +993,7 @@ String.padEnd("abc", 1, "") == "abc" @send external padEnd: (string, int, string) => string = "padEnd" -//TODO +// TODO: add docs @get_index external getSymbol: (string, Core__Symbol.t) => option<'a> = "" @get_index external getSymbolUnsafe: (string, Core__Symbol.t) => 'a = "" @set_index external setSymbol: (string, Core__Symbol.t, 'a) => unit = "" From c9bcad038d286aa6fe14eb865530f548461bbba6 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Thu, 16 Feb 2023 19:51:34 -0300 Subject: [PATCH 12/17] update --- src/Core__String.resi | 174 ++++++++++++++++-------------------------- 1 file changed, 66 insertions(+), 108 deletions(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index c5ffd124..7c0bf293 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -45,8 +45,7 @@ external make: 'a => string = "String" that number; `n` ranges from 0 to 65535.If out of range, the lower 16 bits of the value are used. Thus, `fromCharCode(0x1F63A)` gives the same result as `fromCharCode(0xF63A)`. -See [`String.fromCharCode`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode) -on MDN. +See [`String.fromCharCode`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode) on MDN. ## Examples @@ -63,8 +62,7 @@ external fromCharCode: int => string = "String.fromCharCode" /** `fromCharCodeMany([n1, n2, n3])` creates a `string` from the characters corresponding to the given numbers, using the same rules as `fromCharCode`. -See [`String.fromCharCode`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode) -on MDN. +See [`String.fromCharCode`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode) on MDN. ## Examples @@ -79,12 +77,8 @@ external fromCharCodeMany: array => string = "String.fromCharCode" /** `fromCodePoint(n)` creates a `string` containing the character corresponding to -that numeric code point. If the number is not a valid code point, it raises -`RangeError`. Thus, `fromCodePoint(0x1F63A)` will produce a correct value, -unlike `fromCharCode(0x1F63A)`, and `fromCodePoint(-5)` will raise a -`RangeError`. -See [`String.fromCodePoint`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint) -on MDN. +that numeric code point. +See [`String.fromCodePoint`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint) on MDN. ## Examples @@ -94,6 +88,10 @@ String.fromCodePoint(0x3c8) == `ψ` String.fromCodePoint(0xd55c) == `한` String.fromCodePoint(0x1f63a) == `😺` ``` + +## Exceptions + +- `RangeError`: If the number is not a valid code point, like `fromCharCode(-5)`. */ @val external fromCodePoint: int => string = "String.fromCodePoint" @@ -102,8 +100,7 @@ external fromCodePoint: int => string = "String.fromCodePoint" `fromCodePointMany([n1, n2, n3])` creates a `string` from the characters corresponding to the given code point numbers, using the same rules as `fromCodePoint`. -See [`String.fromCodePoint`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint) -on MDN. +See [`String.fromCodePoint`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint) on MDN. ## Examples @@ -117,8 +114,7 @@ external fromCodePointMany: array => string = "String.fromCodePoint" /** `length(str)` returns the length of the given `string`. -See [`String.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length) -on MDN. +See [`String.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length) on MDN. ## Examples @@ -149,8 +145,7 @@ external get: (string, int) => option = "" negative or greater than the length of `str`, it returns the empty string. If the string contains characters outside the range \u0000-\uffff, it will return the first 16-bit value at that position in the string. -See [`String.charAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt) -on MDN. +See [`String.charAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt) on MDN. ## Examples @@ -169,8 +164,7 @@ the result is in the range 0-65535, unlke `codePointAt`, so it will not work correctly for characters with code points greater than or equal to 0x10000. The return type is `float` because this function returns NaN if `n` is less than zero or greater than the length of the string. -See [`String.charCodeAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt) -on MDN. +See [`String.charCodeAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt) on MDN. ## Examples @@ -187,8 +181,7 @@ external charCodeAt: (string, int) => float = "charCodeAt" as a `Some(value)`. The return value handles code points greater than or equal to 0x10000. If there is no code point at the given position, the function returns `None`. -See [`String.codePointAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt) -on MDN. +See [`String.codePointAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt) on MDN. ## Examples @@ -203,8 +196,7 @@ external codePointAt: (string, int) => option = "codePointAt" /** `concat(original, append)` returns a new `string` with `append` added after `original`. -See [`String.concat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat) -on MDN. +See [`String.concat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat) on MDN. ## Examples @@ -219,8 +211,7 @@ external concat: (string, string) => string = "concat" /** `concatMany(original, arr)` returns a new `string` consisting of each item of an array of strings added to the `original` string. -See [`String.concat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat) -on MDN. +See [`String.concat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat) on MDN. ## Examples @@ -233,10 +224,9 @@ String.concatMany("1st", ["2nd", "3rd", "4th"]) == "1st2nd3rd4th" external concatMany: (string, array) => string = "concat" /** -ES2015: `endsWith(str, substr)` returns `true` if the `str` ends with `substr`, +`endsWith(str, substr)` returns `true` if the `str` ends with `substr`, `false` otherwise. -See [`String.endsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) -on MDN. +See [`String.endsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) on MDN. ## Examples @@ -253,8 +243,7 @@ external endsWith: (string, string) => bool = "endsWith" `endsWithFrom(str, ending, len)` returns `true` if the first len characters of `str` end with `ending`, `false` otherwise. If `len` is greater than or equal to the length of `str`, then it works like `endsWith`. -See [`String.endsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) -on MDN. +See [`String.endsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) on MDN. ## Examples @@ -269,10 +258,9 @@ String.endsWithFrom("example.dat", "ple", 7) == true external endsWithFrom: (string, string, int) => bool = "endsWith" /** -ES2015: `includes(str, searchValue)` returns `true` if `searchValue` is found +`includes(str, searchValue)` returns `true` if `searchValue` is found anywhere within `str`, `false` otherwise. -See [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) -on MDN. +See [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN. ## Examples @@ -287,11 +275,10 @@ String.includes("programmer.dat", "xyz") == false external includes: (string, string) => bool = "includes" /** -ES2015: `includesFrom(str, searchValue start)` returns `true` if `searchValue` +`includesFrom(str, searchValue start)` returns `true` if `searchValue` is found anywhere within `str` starting at character number `start` (where 0 is the first character), `false` otherwise. -See [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) -on MDN. +See [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN. ## Examples @@ -305,10 +292,9 @@ String.includesFrom(`대한민국`, `한`, 1) == true external includesFrom: (string, string, int) => bool = "includes" /** -ES2015: `indexOf(str, searchValue)` returns the position at which `searchValue` +`indexOf(str, searchValue)` returns the position at which `searchValue` was first found within `str`, or `-1` if `searchValue` is not in `str`. -See [`String.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) -on MDN. +See [`String.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) on MDN. ## Examples @@ -340,8 +326,7 @@ let indexOfOpt: (string, string) => option `-1` if `searchValue` is not found in that portion of `str`. The return value is relative to the beginning of the string, no matter where the search started from. -See [`String.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) -on MDN. +See [`String.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) on MDN. ## Examples @@ -359,8 +344,7 @@ external indexOfFrom: (string, string, int) => int = "indexOf" `searchValue` within `str`, searching backwards from the end of the string. Returns `-1` if `searchValue` is not in `str`. The return value is always relative to the beginning of the string. -See [`String.lastIndexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) -on MDN. +See [`String.lastIndexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) on MDN. ## Examples @@ -392,8 +376,7 @@ let lastIndexOfOpt: (string, string) => option occurrence of `searchValue` within `str`, searching backwards from the given start position. Returns `-1` if `searchValue` is not in `str`. The return value is always relative to the beginning of the string. -See [`String.lastIndexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) -on MDN. +See [`String.lastIndexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) on MDN. ## Examples @@ -415,8 +398,7 @@ there is a match, the return value is `Some(array)` where the array contains: - Any capture groups if the regexp had parentheses For regular expressions with the g modifier, a matched expression returns `Some(array)` with all the matched substrings and no capture groups. -See [`String.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) -on MDN. +See [`String.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) on MDN. ## Examples @@ -437,9 +419,8 @@ Canonical (NFC) Composition. Consider the character ã, which can be represented as the single codepoint \u00e3 or the combination of a lower case letter A \u0061 and a combining tilde \u0303. Normalization ensures that both can be stored in an equivalent binary representation. -See [`String.normalize`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) -on MDN. See also [Unicode technical report -#15](https://unicode.org/reports/tr15/) for details. +See [`String.normalize`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) on MDN. +See also [Unicode technical report #15](https://unicode.org/reports/tr15/) for details. ## Examples @@ -457,7 +438,7 @@ assert(normalizeString1 === normalizeString2) external normalize: string => string = "normalize" /** -ES2015: `normalizeByForm(str, form)` returns the normalized Unicode string using the +`normalizeByForm(str, form)` returns the normalized Unicode string using the specified form of normalization, which may be one of: - "NFC" — Normalization Form Canonical Composition. - "NFD" — Normalization Form Canonical Decomposition. @@ -484,9 +465,7 @@ external normalizeByForm: (string, normalizeForm) => string = "normalize" /** `repeat(str, n)` returns a `string` that consists of `n` repetitions of `str`. -Raises `RangeError` if `n` is negative. -See [`String.repeat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat) -on MDN. +See [`String.repeat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat) on MDN. ## Examples @@ -494,17 +473,20 @@ on MDN. String.repeat("ha", 3) == "hahaha" String.repeat("empty", 0) == "" ``` + +## Exceptions + +- `RangeError`: if `n` is negative. */ @send external repeat: (string, int) => string = "repeat" /** -ES2015: `replace(str, substr, newSubstr)` returns a new `string` which is +`replace(str, substr, newSubstr)` returns a new `string` which is identical to `str` except with the first matching instance of `substr` replaced by `newSubstr`. `substr` is treated as a verbatim string to match, not a regular expression. -See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) -on MDN. +See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. ## Examples @@ -519,8 +501,7 @@ external replace: (string, string, string) => string = "replace" /** `replaceRegExp(str, regex, replacement)` returns a new `string` where occurrences matching regex have been replaced by `replacement`. -See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) -on MDN. +See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. ## Examples @@ -537,8 +518,7 @@ Returns a new `string` with some or all matches of a pattern with no capturing parentheses replaced by the value returned from the given function. The function receives as its parameters the matched string, the offset at which the match begins, and the whole string being matched. -See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) -on MDN. +See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. ## Examples @@ -562,8 +542,7 @@ capturing parentheses replaced by the value returned from the given function. The function receives as its parameters the matched string, the captured string, the offset at which the match begins, and the whole string being matched. -See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) -on MDN. +See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. ## Examples @@ -589,8 +568,7 @@ capturing parentheses replaced by the value returned from the given function. The function receives as its parameters the matched string, the captured strings, the offset at which the match begins, and the whole string being matched. -See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) -on MDN. +See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. ## Examples @@ -625,8 +603,7 @@ capturing parentheses replaced by the value returned from the given function. The function receives as its parameters the matched string, the captured strings, the offset at which the match begins, and the whole string being matched. -See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) -on MDN. +See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. */ @send external unsafeReplaceRegExpBy3: ( @@ -645,8 +622,7 @@ external unsafeReplaceRegExpBy3: ( /** `search(str, regexp)` returns the starting position of the first match of `regexp` in the given `str`, or -1 if there is no match. -See [`String.search`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search) -on MDN. +See [`String.search`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search) on MDN. ## Examples @@ -676,8 +652,7 @@ character `n1` up to but not including `n2`. - If either `n1` or `n2` is negative, then it is evaluated as `length(str - n1)` or `length(str - n2)`. - If `n2` is greater than the length of `str`, then it is treated as `length(str)`. - If `n1` is greater than `n2`, slice returns the empty string. -See [`String.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) -on MDN. +See [`String.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) on MDN. ## Examples @@ -696,8 +671,7 @@ external slice: (string, ~start: int, ~end: int) => string = "slice" `n` to the end of the string. - If `n` is negative, then it is evaluated as `length(str - n)`. - If `n` is greater than the length of `str`, then sliceToEnd returns the empty string. -See [`String.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) -on MDN. +See [`String.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) on MDN. ## Examples @@ -713,8 +687,7 @@ external sliceToEnd: (string, ~start: int) => string = "slice" /** `split(str, delimiter)` splits the given `str` at every occurrence of `delimiter` and returns an array of the resulting substrings. -See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) -on MDN. +See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) on MDN. ## Examples @@ -748,8 +721,7 @@ external splitAtMost: (string, string, ~limit: int) => array = "split" /** `splitByRegExp(str, regex)` splits the given `str` at every occurrence of `regex` and returns an array of the resulting substrings. -See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) -on MDN. +See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) on MDN. ## Examples @@ -765,8 +737,7 @@ external splitByRegExp: (string, Core__RegExp.t) => array> = "spl occurrence of `regex` and returns an array of the first `n` resulting substrings. If `n` is negative or greater than the number of substrings, the array will contain all the substrings. -See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) -on MDN. +See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) on MDN. ## Examples @@ -783,10 +754,9 @@ external splitByRegExpAtMost: (string, Core__RegExp.t, ~limit: int) => array bool = "startsWith" /** -ES2015: `startsWithFrom(str, substr, n)` returns `true` if the `str` starts +`startsWithFrom(str, substr, n)` returns `true` if the `str` starts with `substr` starting at position `n`, `false` otherwise. If `n` is negative, the search starts at the beginning of `str`. -See [`String.startsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) -on MDN. +See [`String.startsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) on MDN. ## Examples @@ -823,8 +792,7 @@ not including end from `str`. - If `start` is less than zero, it is treated as zero. - If `end` is zero or negative, the empty string is returned. - If `start` is greater than `end`, the `start` and `end` points are swapped. -See [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) -on MDN. +See [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) on MDN. ## Examples @@ -842,8 +810,7 @@ external substring: (string, ~start: int, ~end: int) => string = "substring" position `start` to the end. - If `start` is less than or equal to zero, the entire string is returned. - If `start` is greater than or equal to the length of `str`, the empty string is returned. -See [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) -on MDN. +See [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) on MDN. ## Examples @@ -862,8 +829,7 @@ case mappings in the Unicode Character Database. Notice that the conversion can give different results depending upon context, for example with the Greek letter sigma, which has two different lower case forms; one when it is the last character in a string and another when it is not. -See [`String.toLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase) -on MDN. +See [`String.toLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase) on MDN. ## Examples @@ -878,8 +844,7 @@ external toLowerCase: string => string = "toLowerCase" /** `toLocaleLowerCase(str)` converts `str` to lower case using the current locale. -See [`String.toLocaleLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase) -on MDN. +See [`String.toLocaleLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase) on MDN. */ @send external toLocaleLowerCase: string => string = "toLocaleLowerCase" @@ -889,8 +854,7 @@ external toLocaleLowerCase: string => string = "toLocaleLowerCase" case mappings in the Unicode Character Database. Notice that the conversion can expand the number of letters in the result; for example the German ß capitalizes to two Ses in a row. -See [`String.toUpperCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) -on MDN. +See [`String.toUpperCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) on MDN. ## Examples @@ -905,8 +869,7 @@ external toUpperCase: string => string = "toUpperCase" /** `toLocaleUpperCase(str)` converts `str` to upper case using the current locale. -See [`String.toLocaleUpperCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase) -on MDN. +See [`String.toLocaleUpperCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase) on MDN. */ @send external toLocaleUpperCase: string => string = "toLocaleUpperCase" @@ -914,8 +877,7 @@ external toLocaleUpperCase: string => string = "toLocaleUpperCase" /** `trim(str)` returns a string that is `str` with whitespace stripped from both ends. Internal whitespace is not removed. -See [`String.trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim) -on MDN. +See [`String.trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim) on MDN. ## Examples @@ -930,8 +892,7 @@ external trim: string => string = "trim" /** `trimStart(str)` returns a string that is `str` with whitespace stripped from the beginning of a string. Internal whitespace is not removed. -See [`String.trimStart`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart) -on MDN. +See [`String.trimStart`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart) on MDN. ## Examples @@ -946,8 +907,7 @@ external trimStart: string => string = "trimStart" /** `trinEnd(str)` returns a string that is `str` with whitespace stripped from the end of a string. Internal whitespace is not removed. -See [`String.trimEnd`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd) -on MDN. +See [`String.trimEnd`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd) on MDN. ## Examples @@ -963,8 +923,7 @@ external trimEnd: string => string = "trimEnd" `padStart(str, n, padStr)` returns a string that has been padded with `padStr` (multiple times, if needed) until the resulting string reaches the given `n` length. The padding is applied from the start of the current string. -See [`String.padStart`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart) -on MDN. +See [`String.padStart`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart) on MDN. ## Examples @@ -980,8 +939,7 @@ external padStart: (string, int, string) => string = "padStart" `padEnd(str, n, padStr)` returns a string that has been padded with `padStr` (multiple times, if needed) until the resulting string reaches the given `n` length. The padding is applied from the end of the current string. -See [`String.padEnd`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd) -on MDN. +See [`String.padEnd`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd) on MDN. ## Examples @@ -1004,9 +962,9 @@ whether a reference string comes before or fater, or is the same as the given string in sort order. If `referenceStr` occurs before `compareStr` positive if the `referenceStr` occurs after `compareStr`; `0` if they are equivalent. Do not rely on exact return values of `-1` or `1` -See [`String.localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) +See [`String.localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) on MDN. -## Examples +See [`String.localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) ```rescript String.localeCompare("a", "c") < 0. == true From 0db2178ff5924978d42e5a747416d5840bf85c11 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Thu, 16 Feb 2023 20:28:18 -0300 Subject: [PATCH 13/17] some fixes --- src/Core__String.resi | 179 +++++++++++++++++++++--------------------- 1 file changed, 91 insertions(+), 88 deletions(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index 7c0bf293..91802c16 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -42,7 +42,7 @@ external make: 'a => string = "String" /** `fromCharCode(n)` creates a `string` containing the character corresponding to -that number; `n` ranges from 0 to 65535.If out of range, the lower 16 bits of +that number, `n` ranges from 0 to 65535. If out of range, the lower 16 bits of the value are used. Thus, `fromCharCode(0x1F63A)` gives the same result as `fromCharCode(0xF63A)`. See [`String.fromCharCode`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode) on MDN. @@ -107,6 +107,12 @@ See [`String.fromCodePoint`](https://developer.mozilla.org/en-US/docs/Web/JavaSc ```rescript String.fromCodePointMany([0xd55c, 0xae00, 0x1f63a]) == `한글😺` ``` + +## Exceptions + +- `RangeError`: If one of the number is not a valid code point, like +`fromCharCode([1, -5])`. + */ @variadic @val @@ -126,44 +132,44 @@ String.length("abcd") == 4 external length: string => int = "length" /** -`get(str, n)` returns as a `option` at the given index number. If `n` -is out of range, this function returns `None`. +`get(str, index)` returns an `option` at the given `index` number. If +`index` is out of range, this function returns `None`. ## Examples ```rescript -String.get("Reason", 0) == Some("R") -String.get("Reason", 4) == Some("o") -String.get(`Rẽasöń`, 5) == Some(`ń`) +String.get("ReScript", 0) == Some("R") +String.get("Hello", 4) == Some("o") +String.get(`JS`, 4) == None ``` */ @get_index external get: (string, int) => option = "" /** -`charAt(str, n)` gets the character at index `n` within string `str`. If `n` is -negative or greater than the length of `str`, it returns the empty string. If -the string contains characters outside the range \u0000-\uffff, it will return -the first 16-bit value at that position in the string. +`charAt(str, index)` gets the character at `index` within string `str`. If +`index` is negative or greater than the length of `str`, it returns the empty +string. If the string contains characters outside the range \u0000-\uffff, it +will return the first 16-bit value at that position in the string. See [`String.charAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt) on MDN. ## Examples ```rescript -String.charAt("Reason", 0) == "R" -String.charAt("Reason", 12) == "" -String.charAt(`Rẽasöń`, 5) == `ń` +String.charAt("ReScript", 0) == "R" +String.charAt("Hello", 12) == "" +String.charAt(`JS`, 5) == "" ``` */ @send external charAt: (string, int) => string = "charAt" /** -`charCodeAt(str, n)` returns the character code at position `n` in string `str` -the result is in the range 0-65535, unlke `codePointAt`, so it will not work -correctly for characters with code points greater than or equal to 0x10000. The -return type is `float` because this function returns NaN if `n` is less than -zero or greater than the length of the string. +`charCodeAt(str, index)` returns the character code at position `index` in +string `str` the result is in the range 0-65535, unlike `codePointAt`, so it +will not work correctly for characters with code points greater than or equal +to 0x10000. The return type is `float` because this function returns NaN if +`index` is less than zero or greater than the length of the string. See [`String.charCodeAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt) on MDN. ## Examples @@ -177,10 +183,10 @@ String.codePointAt(`😺`, 0) == Some(0x1f63a) external charCodeAt: (string, int) => float = "charCodeAt" /** -`codePointAt(str, n)` returns the code point at position `n` within string `str` -as a `Some(value)`. The return value handles code points greater than or equal -to 0x10000. If there is no code point at the given position, the function -returns `None`. +`codePointAt(str, index)` returns the code point at position `index` within +string `str` as a `Some(value)`. The return value handles code points greater +than or equal to 0x10000. If there is no code point at the given position, the +function returns `None`. See [`String.codePointAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt) on MDN. ## Examples @@ -224,8 +230,8 @@ String.concatMany("1st", ["2nd", "3rd", "4th"]) == "1st2nd3rd4th" external concatMany: (string, array) => string = "concat" /** -`endsWith(str, substr)` returns `true` if the `str` ends with `substr`, -`false` otherwise. +`endsWith(str, substr)` returns `true` if the `str` ends with `substr`, `false` +otherwise. See [`String.endsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) on MDN. ## Examples @@ -258,8 +264,8 @@ String.endsWithFrom("example.dat", "ple", 7) == true external endsWithFrom: (string, string, int) => bool = "endsWith" /** -`includes(str, searchValue)` returns `true` if `searchValue` is found -anywhere within `str`, `false` otherwise. +`includes(str, searchValue)` returns `true` if `searchValue` is found anywhere +within `str`, `false` otherwise. See [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN. ## Examples @@ -275,9 +281,9 @@ String.includes("programmer.dat", "xyz") == false external includes: (string, string) => bool = "includes" /** -`includesFrom(str, searchValue start)` returns `true` if `searchValue` -is found anywhere within `str` starting at character number `start` (where 0 is -the first character), `false` otherwise. +`includesFrom(str, searchValue start)` returns `true` if `searchValue` is found +anywhere within `str` starting at character number `start` (where 0 is the +first character), `false` otherwise. See [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN. ## Examples @@ -292,8 +298,8 @@ String.includesFrom(`대한민국`, `한`, 1) == true external includesFrom: (string, string, int) => bool = "includes" /** -`indexOf(str, searchValue)` returns the position at which `searchValue` -was first found within `str`, or `-1` if `searchValue` is not in `str`. +`indexOf(str, searchValue)` returns the position at which `searchValue` was +first found within `str`, or `-1` if `searchValue` is not in `str`. See [`String.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) on MDN. ## Examples @@ -309,7 +315,7 @@ String.indexOf("bookseller", "xyz") == -1 external indexOf: (string, string) => int = "indexOf" /** -`indexOfOpt(str, searchValue)`. Like `indexOf`, but return a `option`. +`indexOfOpt(str, searchValue)`. Like `indexOf`, but return an `option`. ## Examples @@ -358,7 +364,7 @@ String.lastIndexOf("abcdefg", "xyz") == -1 external lastIndexOf: (string, string) => int = "lastIndexOf" /** -`lastIndexOfOpt(str, searchValue)`. Like `lastIndexOfOpt`, but return a +`lastIndexOfOpt(str, searchValue)`. Like `lastIndexOfOpt`, but return an `option`. ## Examples @@ -405,7 +411,8 @@ See [`String.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref ```rescript String.match("The better bats", %re("/b[aeiou]t/")) == Some(["bet"]) String.match("The better bats", %re("/b[aeiou]t/g")) == Some(["bet", "bat"]) -String.match("Today is 2018-04-05.", %re("/(\d+)-(\d+)-(\d+)/")) == Some(["2018-04-05", "2018", "04", "05"]) +String.match("Today is 2018-04-05.", %re("/(\d+)-(\d+)-(\d+)/")) == + Some(["2018-04-05", "2018", "04", "05"]) String.match("The large container.", %re("/b[aeiou]g/")) == None ``` */ @@ -445,18 +452,19 @@ specified form of normalization, which may be one of: - "NFKC" — Normalization Form Compatibility Composition. - "NFKD" — Normalization Form Compatibility Decomposition. See [`String.normalize`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) on MDN. -See also [Unicode technical report #15](https://unicode.org/reports/tr15/) for details. +See also [Unicode technical report #15](https://unicode.org/reports/tr15/) for +details. ## Examples ```rescript let string1 = "\uFB00" let string2 = "\u0066\u0066" -Console.log(string1 === string2) // false +Console.log(string1 == string2) // false let normalizeString1 = String.normalizeByForm(string1, #NFKD) let normalizeString2 = String.normalizeByForm(string2, #NFKD) -assert(normalizeString1 === normalizeString2) +Console.log(normalizeString1 == normalizeString2) // true ``` */ type normalizeForm = [#NFC | #NFD | #NFKC | #NFKD] @@ -499,8 +507,8 @@ String.replace("the cat and the dog", "the", "this") == "this cat and the dog" external replace: (string, string, string) => string = "replace" /** -`replaceRegExp(str, regex, replacement)` returns a new `string` where occurrences -matching regex have been replaced by `replacement`. +`replaceRegExp(str, regex, replacement)` returns a new `string` where +occurrences matching regex have been replaced by `replacement`. See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. ## Examples @@ -514,10 +522,11 @@ String.replaceRegExp("Juan Fulano", %re("/(\w+) (\w+)/"), "$2, $1") == "Fulano, external replaceRegExp: (string, Core__RegExp.t, string) => string = "replace" /** -Returns a new `string` with some or all matches of a pattern with no capturing -parentheses replaced by the value returned from the given function. The -function receives as its parameters the matched string, the offset at which the -match begins, and the whole string being matched. +`unsafeReplaceRegExpBy0(str, regex, f)` returns a new `string` with some or all +matches of a pattern with no capturing parentheses replaced by the value +returned from the given function. The function receives as its parameters the +matched string, the offset at which the match begins, and the whole string being +matched. See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. ## Examples @@ -537,11 +546,8 @@ external unsafeReplaceRegExpBy0: ( ) => string = "replace" /** -Returns a new `string` with some or all matches of a pattern with one set of -capturing parentheses replaced by the value returned from the given function. -The function receives as its parameters the matched string, the captured -string, the offset at which the match begins, and the whole string being -matched. +`unsafeReplaceRegExpBy1(str, regexp, f)`. Like `unsafeReplaceRegExpBy0`, but `f` +has `group1` parameter. See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. ## Examples @@ -563,11 +569,8 @@ external unsafeReplaceRegExpBy1: ( ) => string = "replace" /** -Returns a new `string` with some or all matches of a pattern with two sets of -capturing parentheses replaced by the value returned from the given function. -The function receives as its parameters the matched string, the captured -strings, the offset at which the match begins, and the whole string being -matched. +`unsafeReplaceRegExpBy2(str, regexp, f)`. Like `unsafeReplaceRegExpBy1`, but `f` +has two group parameters. See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. ## Examples @@ -598,11 +601,8 @@ external unsafeReplaceRegExpBy2: ( ) => string = "replace" /** -Returns a new `string` with some or all matches of a pattern with three sets of -capturing parentheses replaced by the value returned from the given function. -The function receives as its parameters the matched string, the captured -strings, the offset at which the match begins, and the whole string being -matched. +`unsafeReplaceRegExpBy3(str, regexp, f)`. Like `unsafeReplaceRegExpBy1`, but `f` +has three group parameters. See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN. */ @send @@ -635,7 +635,7 @@ String.search("no numbers", %re("/\d+/")) == -1 external search: (string, Core__RegExp.t) => int = "search" /** -`searchOpt(str, regexp)`. Like `search`, but return a `option`. +`searchOpt(str, regexp)`. Like `search`, but return an `option`. ## Examples @@ -647,11 +647,13 @@ String.search("no numbers", %re("/\d+/")) == None let searchOpt: (string, Core__RegExp.t) => option /** -`slice(str, ~start:n1, ~end:n2)` returns the substring of `str` starting at -character `n1` up to but not including `n2`. -- If either `n1` or `n2` is negative, then it is evaluated as `length(str - n1)` or `length(str - n2)`. -- If `n2` is greater than the length of `str`, then it is treated as `length(str)`. -- If `n1` is greater than `n2`, slice returns the empty string. +`slice(str, ~start, ~end)` returns the substring of `str` starting at +character `start` up to but not including `end`. +- If either `start` or `end` is negative, then it is evaluated as +`length(str - start)` or `length(str - end)`. +- If `end` is greater than the length of `str`, then it is treated as +`length(str)`. +- If `start` is greater than `end`, slice returns the empty string. See [`String.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) on MDN. ## Examples @@ -667,10 +669,10 @@ String.slice("abcdefg", ~start=5, ~end=1) == "" external slice: (string, ~start: int, ~end: int) => string = "slice" /** -`sliceToEnd(str, ~start:n)` returns the substring of `str` starting at character -`n` to the end of the string. -- If `n` is negative, then it is evaluated as `length(str - n)`. -- If `n` is greater than the length of `str`, then sliceToEnd returns the empty string. +`sliceToEnd(str, ~start)` returns the substring of `str` starting at character +`start` to the end of the string. +- If `start` is negative, then it is evaluated as `length(str - start)`. +- If `start` is greater than the length of `str`, then sliceToEnd returns the empty string. See [`String.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) on MDN. ## Examples @@ -702,10 +704,10 @@ String.split("has-no-delimiter", ";") == ["has-no-delimiter"] external split: (string, string) => array = "split" /** -`splitAtMost(str, delimiter, ~limit:n)` splits the given `str` at every -occurrence of `delimiter` and returns an array of the first `n` resulting -substrings. If `n` is negative or greater than the number of substrings, the -array will contain all the substrings. +`splitAtMost(str, delimiter, ~limit)` splits the given `str` at every +occurrence of `delimiter` and returns an array of the first `limit` resulting +substrings. If `limit` is negative or greater than the number of substrings, +the array will contain all the substrings. ## Examples @@ -719,8 +721,8 @@ String.splitAtMost("ant/bee/cat/dog/elk", "/", ~limit=9) = ["ant", "bee", "cat", external splitAtMost: (string, string, ~limit: int) => array = "split" /** -`splitByRegExp(str, regex)` splits the given `str` at every occurrence of `regex` -and returns an array of the resulting substrings. +`splitByRegExp(str, regexp)` splits the given `str` at every occurrence of +`regexp` and returns an array of the resulting substrings. See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) on MDN. ## Examples @@ -733,9 +735,9 @@ String.splitByRegExp("Jan,Feb,Mar", %re("/,/")) == [Some("Jan"), Some("Feb"), So external splitByRegExp: (string, Core__RegExp.t) => array> = "split" /** -`splitByRegExpAtMost(str, regex, ~limit:n)` splits the given `str` at every -occurrence of `regex` and returns an array of the first `n` resulting -substrings. If `n` is negative or greater than the number of substrings, the +`splitByRegExpAtMost(str, regexp, ~limit)` splits the given `str` at every +occurrence of `regexp` and returns an array of the first `limit` resulting +substrings. If `limit` is negative or greater than the number of substrings, the array will contain all the substrings. See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) on MDN. @@ -754,8 +756,8 @@ external splitByRegExpAtMost: (string, Core__RegExp.t, ~limit: int) => array bool = "startsWith" /** -`substring(str, ~start:start, ~end:end)` returns characters `start` up to but -not including end from `str`. +`substring(str, ~start, ~end)` returns characters `start` up to but not +including end from `str`. - If `start` is less than zero, it is treated as zero. - If `end` is zero or negative, the empty string is returned. - If `start` is greater than `end`, the `start` and `end` points are swapped. @@ -806,10 +808,11 @@ String.substring("playground", ~start=4, ~end=12) == "ground" external substring: (string, ~start: int, ~end: int) => string = "substring" /** -`substringToEnd(str, ~start:start)` returns the substring of `str` from -position `start` to the end. +`substringToEnd(str, ~start)` returns the substring of `str` from position +`start` to the end. - If `start` is less than or equal to zero, the entire string is returned. -- If `start` is greater than or equal to the length of `str`, the empty string is returned. +- If `start` is greater than or equal to the length of `str`, the empty string +is returned. See [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) on MDN. ## Examples @@ -827,7 +830,7 @@ external substringToEnd: (string, ~start: int) => string = "substring" `toLowerCase(str)` converts `str` to lower case using the locale-insensitive case mappings in the Unicode Character Database. Notice that the conversion can give different results depending upon context, for example with the Greek -letter sigma, which has two different lower case forms; one when it is the last +letter sigma, which has two different lower case forms, one when it is the last character in a string and another when it is not. See [`String.toLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase) on MDN. @@ -852,7 +855,7 @@ external toLocaleLowerCase: string => string = "toLocaleLowerCase" /** `toUpperCase(str)` converts `str` to upper case using the locale-insensitive case mappings in the Unicode Character Database. Notice that the conversion can -expand the number of letters in the result; for example the German ß +expand the number of letters in the result, for example the German ß capitalizes to two Ses in a row. See [`String.toUpperCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) on MDN. @@ -960,11 +963,11 @@ external padEnd: (string, int, string) => string = "padEnd" `localeCompare(referenceStr, compareStr)` returns a float than indicatings whether a reference string comes before or fater, or is the same as the given string in sort order. If `referenceStr` occurs before `compareStr` positive if -the `referenceStr` occurs after `compareStr`; `0` if they are equivalent. +the `referenceStr` occurs after `compareStr`, `0` if they are equivalent. Do not rely on exact return values of `-1` or `1` See [`String.localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) on MDN. -See [`String.localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) +## Examples ```rescript String.localeCompare("a", "c") < 0. == true From f92dec192c0572337d9365f027cc3d70993cee51 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Fri, 17 Feb 2023 23:20:48 -0300 Subject: [PATCH 14/17] Update src/Core__String.resi Co-authored-by: Gabriel Nordeborn --- src/Core__String.resi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index 91802c16..22f0c072 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -281,7 +281,7 @@ String.includes("programmer.dat", "xyz") == false external includes: (string, string) => bool = "includes" /** -`includesFrom(str, searchValue start)` returns `true` if `searchValue` is found +`includesFrom(str, searchValue, start)` returns `true` if `searchValue` is found anywhere within `str` starting at character number `start` (where 0 is the first character), `false` otherwise. See [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN. From dd77f2594325bc7f44cdd9059f34493afe77cbb1 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Fri, 17 Feb 2023 23:22:14 -0300 Subject: [PATCH 15/17] fix indentation --- src/Core__String.resi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index 22f0c072..52f24722 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -745,10 +745,10 @@ See [`String.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref ```rescript String.splitByRegExpAtMost("Hello World. How are you doing?", %re("/ /"), ~limit=3) == [ - Some("Hello"), - Some("World."), - Some("How"), - ] + Some("Hello"), + Some("World."), + Some("How"), +] ``` */ @send From bc960a3de4e5b436b761ec086e70ff6db05f77f7 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Wed, 1 Mar 2023 21:11:01 -0300 Subject: [PATCH 16/17] fix float --- src/Core__String.resi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Core__String.resi b/src/Core__String.resi index 52f24722..56486294 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -970,8 +970,8 @@ See [`String.localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaSc ## Examples ```rescript -String.localeCompare("a", "c") < 0. == true -String.localeCompare("a", "a") == 0. +String.localeCompare("a", "c") < 0.0 == true +String.localeCompare("a", "a") == 0.0 ``` */ @send From afc93c36422926e858a0741cd0d7bfa365e5b1b0 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Wed, 1 Mar 2023 21:11:46 -0300 Subject: [PATCH 17/17] update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3c1527a..95072a46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,3 +26,4 @@ - Docstrings for `Dict`. https://github.com/rescript-association/rescript-core/pull/40 - Docstrings for `RegExp`. https://github.com/rescript-association/rescript-core/pull/47 - Docstrings for `Date`. https://github.com/rescript-association/rescript-core/pull/61 +- Docstrings for `String`. https://github.com/rescript-association/rescript-core/pull/27