Skip to content

Commit ce71910

Browse files
jdeisenbergbobzhang
authored andcommitted
Add documentation to match Belt doc style.
1 parent e655afb commit ce71910

File tree

1 file changed

+231
-19
lines changed

1 file changed

+231
-19
lines changed

jscomp/others/js_string.ml

Lines changed: 231 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,39 +130,38 @@ external codePointAt : int -> int option = "" [@@bs.send.pipe: t] [@@bs.return {
130130

131131
(** [concat append original] returns a new string with [append] added after [original].
132132
133-
@example {[
134-
concat "bell" "cow" = "cowbell";;
135-
]}
136-
133+
@example {[
134+
concat "bell" "cow" = "cowbell";;
135+
]}
137136
*)
138137
external concat : t -> t = "" [@@bs.send.pipe: t]
139138

140139
(** [concat arr original] returns a new string consisting of each item of an array of strings added to the [original] string.
141140
142-
@example {[
143-
concatMany [|"2nd"; "3rd"; "4th"|] "1st" = "1st2nd3rd4th";;
144-
]}
141+
@example {[
142+
concatMany [|"2nd"; "3rd"; "4th"|] "1st" = "1st2nd3rd4th";;
143+
]}
145144
*)
146145
external concatMany : t array -> t = "concat" [@@bs.send.pipe: t] [@@bs.splice]
147146

148147
(** ES2015:
149148
[endsWith substr str] returns [true] if the [str] ends with [substr], [false] otherwise.
150149
151-
@example {[
152-
endsWith "Script" "BuckleScript" = true;;
153-
endsWith "Script" "COBOL" = false;;
154-
]}
150+
@example {[
151+
endsWith "Script" "BuckleScript" = true;;
152+
endsWith "Script" "BuckleShoes" = false;;
153+
]}
155154
*)
156155
external endsWith : t -> bool = "" [@@bs.send.pipe: t]
157156

158157
(** [endsWithFrom ending len str] returns [true] if the first [len] characters of [str] end with [ending], [false] otherwise. If [n] 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.)
159158
160-
@example {[
161-
endsWithFrom "cd" 4 "abcd" = true;;
162-
endsWithFrom "cd" 3 "abcde" = false;;
163-
endsWithFrom "cde" 99 "abcde" = true;;
164-
endsWithFrom "ple" 7 "example.dat" = true;;
165-
]}
159+
@example {[
160+
endsWithFrom "cd" 4 "abcd" = true;;
161+
endsWithFrom "cd" 3 "abcde" = false;;
162+
endsWithFrom "cde" 99 "abcde" = true;;
163+
endsWithFrom "ple" 7 "example.dat" = true;;
164+
]}
166165
*)
167166
external endsWithFrom : t -> int -> bool = "endsWith" [@@bs.send.pipe: t] (** ES2015 *)
168167

@@ -402,38 +401,251 @@ the offset at which the match begins, and the whole string being matched.
402401
*)
403402
external unsafeReplaceBy3 : Js_re.t -> (t -> t -> t -> t -> int -> t -> t [@bs.uncurry]) -> t = "replace" [@@bs.send.pipe: t]
404403

404+
(** [search regexp str] returns the starting position of the first match of [regexp] in the given [str], or -1 if there is no match.
405+
406+
@example {[
407+
search [%re "/\\d+/"] "testing 1 2 3" = 8;;
408+
search [%re "/\\d+/"] "no numbers" = -1;;
409+
]}
410+
*)
405411
external search : Js_re.t -> int = "" [@@bs.send.pipe: t]
406412

413+
(** [slice from:n1 to_:n2 str] returns the substring of [str] starting at character [n1] up to but not including [n2]
414+
415+
If either [n1] or [n2] is negative, then it is evaluated as [length str - n1] (or [length str - n2].
416+
417+
If [n2] is greater than the length of [str], then it is treated as [length str].
418+
419+
If [n1] is greater than [n2], [slice] returns the empty string.
420+
421+
@example {[
422+
slice ~from:2 ~to_:5 "abcdefg" == "cde";;
423+
slice ~from:2 ~to_:9 "abcdefg" == "cdefg";;
424+
slice ~from:(-4) ~to_:(-2) "abcdefg" == "de";;
425+
slice ~from:5 ~to_:1 "abcdefg" == "";;
426+
]}
427+
*)
407428
external slice : from:int -> to_:int -> t = "" [@@bs.send.pipe: t]
429+
430+
(** [sliceToEnd from: n str] returns the substring of [str] starting at character [n] to the end of the string
431+
432+
If [n] is negative, then it is evaluated as [length str - n].
433+
434+
If [n] is greater than the length of [str], then [sliceToEnd] returns the empty string.
435+
436+
@example {[
437+
sliceToEnd ~from: 4 "abcdefg" == "efg";;
438+
sliceToEnd ~from: (-2) "abcdefg" == "fg";;
439+
sliceToEnd ~from: 7 "abcdefg" == "";;
440+
]}
441+
*)
408442
external sliceToEnd : from:int -> t = "slice" [@@bs.send.pipe: t]
409443

444+
(**
445+
[split delimiter str] splits the given [str] at every occurrence of [delimiter] and returns an
446+
array of the resulting substrings.
447+
448+
@example {[
449+
split "-" "2018-01-02" = [|"2018"; "01"; "02"|];;
450+
split "," "a,b,,c" = [|"a"; "b"; ""; "c"|];;
451+
split "::" "good::bad as great::awful" = [|"good"; "bad as great"; "awful"|];;
452+
split ";" "has-no-delimiter" = [|"has-no-delimiter"|];;
453+
]};
454+
*)
410455
external split : t -> t array = "" [@@bs.send.pipe: t]
456+
457+
(**
458+
[splitAtMost delimiter ~limit: n str] 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.
459+
460+
@example {[
461+
splitAtMost "/" ~limit: 3 "ant/bee/cat/dog/elk" = [|"ant"; "bee"; "cat"|];;
462+
splitAtMost "/" ~limit: 0 "ant/bee/cat/dog/elk" = [| |];;
463+
splitAtMost "/" ~limit: 9 "ant/bee/cat/dog/elk" = [|"ant"; "bee"; "cat"; "dog"; "elk"|];;
464+
]}
465+
*)
411466
external splitAtMost: t -> limit:int -> t array = "split" [@@bs.send.pipe: t]
467+
468+
(**
469+
Deprecated - Please use [splitAtMost]
470+
*)
412471
external splitLimited : t -> int -> t array = "split" [@@bs.send.pipe: t]
413472
[@@ocaml.deprecated "Please use splitAtMost"]
473+
474+
(**
475+
[splitByRe regex str] splits the given [str] at every occurrence of [regex] and returns an
476+
array of the resulting substrings.
477+
478+
@example {[
479+
splitByRe [%re "/\\s*[,;]\\s*/"] "art; bed , cog ;dad" = [|"art"; "bed"; "cog"; "dad"|];;
480+
splitByRe [%re "/[,;]/"] "has:no:match" = [|"has:no:match"|];;
481+
]};
482+
*)
414483
external splitByRe : Js_re.t -> t array = "split" [@@bs.send.pipe: t]
484+
485+
(**
486+
[splitByReAtMost regex ~limit: n str] splits the given [str] at every occurrence of [regex] and returns an
487+
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.
488+
489+
@example {[
490+
splitByReAtMost [%re "/\\s*:\\s*/"] ~limit: 3 "one: two: three: four" = [|"one"; "two"; "three"|];;
491+
splitByReAtMost [%re "/\\s*:\\s*/"] ~limit: 0 "one: two: three: four" = [| |];;
492+
splitByReAtMost [%re "/\\s*:\\s*/"] ~limit: 8 "one: two: three: four" = [|"one"; "two"; "three"; "four"|];;
493+
]};
494+
*)
415495
external splitByReAtMost : Js_re.t -> limit:int -> t array = "split" [@@bs.send.pipe: t]
496+
497+
(**
498+
Deprecated - Please use [splitByReAtMost]
499+
*)
416500
external splitRegexpLimited : Js_re.t -> int -> t array = "" [@@bs.send.pipe: t]
417501
[@@ocaml.deprecated "Please use splitByReAtMost"]
418502

419-
external startsWith : t -> bool = "" [@@bs.send.pipe: t] (** ES2015 *)
420-
external startsWithFrom : t -> int -> bool = "startsWith" [@@bs.send.pipe: t] (** ES2015 *)
503+
(** ES2015:
504+
[startsWith substr str] returns [true] if the [str] starts with [substr], [false] otherwise.
505+
506+
@example {[
507+
startsWith "Buckle" "BuckleScript" = true;;
508+
startsWith "" "BuckleScript" = true;;
509+
startsWith "Buckle" "JavaScript" = false;;
510+
]}
511+
*)
512+
external startsWith : t -> bool = "" [@@bs.send.pipe: t]
513+
514+
(** ES2015:
515+
[startsWithFrom substr n str] 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].
516+
517+
@example {[
518+
startsWithFrom "kle" 3 "BuckleScript" = true;;
519+
startsWithFrom "" 3 "BuckleScript" = true;;
520+
startsWithFrom "Buckle" 2 "JavaScript" = false;;
521+
]}
522+
*)
523+
external startsWithFrom : t -> int -> bool = "startsWith" [@@bs.send.pipe: t]
421524

525+
(**
526+
[substr ~from: n str] returns the substring of [str] from position [n] to the end of the string.
527+
528+
If [n] is less than zero, the starting position is the length of [str] - [n].
529+
530+
If [n] is greater than or equal to the length of [str], returns the empty string.
531+
532+
@example {[
533+
substr ~from: 3 "abcdefghij" = "defghij"
534+
substr ~from: (-3) "abcdefghij" = "hij"
535+
substr ~from: 12 "abcdefghij" = ""
536+
]}
537+
*)
422538
external substr : from:int -> t = "" [@@bs.send.pipe: t]
539+
540+
(**
541+
[substrAtMost ~from: pos ~length: n str] returns the substring of [str] of length [n] starting at position [pos].
542+
543+
If [pos] is less than zero, the starting position is the length of [str] - [pos].
544+
545+
If [pos] is greater than or equal to the length of [str], returns the empty string.
546+
547+
If [n] is less than or equal to zero, returns the empty string.
548+
549+
@example {[
550+
substrAtMost ~from: 3 ~length: 4 "abcdefghij" = "defghij"
551+
substrAtMost ~from: (-3) ~length: 4 "abcdefghij" = "hij"
552+
substrAtMost ~from: 12 ~ length: 2 "abcdefghij" = ""
553+
]}
554+
*)
423555
external substrAtMost : from:int -> length:int -> t = "substr" [@@bs.send.pipe: t]
424556

557+
(**
558+
[substring ~from: start ~to_: finish str] returns characters [start] up to but not including [finish] from [str].
559+
560+
If [start] is less than zero, it is treated as zero.
561+
562+
If [finish] is zero or negative, the empty string is returned.
563+
564+
If [start] is greater than [finish], the start and finish points are swapped.
565+
566+
@example {[
567+
substring ~from: 3 ~to_: 6 "playground" = "ygr";;
568+
substring ~from: 6 ~to_: 3 "playground" = "ygr";;
569+
substring ~from: 4 ~to_: 12 "playground" = "ground";;
570+
]}
571+
*)
425572
external substring : from:int -> to_:int -> t = "" [@@bs.send.pipe: t]
573+
574+
(**
575+
[substringToEnd ~from: start str] returns the substring of [str] from position [start] to the end.
576+
577+
If [start] is less than or equal to zero, the entire string is returned.
578+
579+
If [start] is greater than or equal to the length of [str], the empty string is returned.
580+
581+
@example {[
582+
substringToEnd ~from: 4 "playground" = "ground";;
583+
substringToEnd ~from: (-3) "playground" = "playground";;
584+
substringToEnd ~from: 12 "playground" = "";
585+
]}
586+
*)
426587
external substringToEnd : from:int -> t = "substring" [@@bs.send.pipe: t]
427588

589+
(**
590+
[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 when it is the last character in a string or not.
591+
592+
@example {[
593+
toLowerCase "ABC" = "abc";;
594+
toLowerCase {js|ΣΠ|js} = {js|σπ|js};;
595+
toLowerCase {js|ΠΣ|js} = {js|πς|js};;
596+
]}
597+
*)
428598
external toLowerCase : t = "" [@@bs.send.pipe: t]
599+
600+
(**
601+
[toLocaleLowerCase str] converts [str] to lower case using the current locale
602+
*)
429603
external toLocaleLowerCase : t = "" [@@bs.send.pipe: t]
604+
605+
(**
606+
[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 [S]es in a row.
607+
608+
@example {[
609+
toUpperCase "abc" = "ABC";;
610+
toUpperCase {js|Straße|js} = {js|STRASSE|js};;
611+
toLowerCase {js|πς|js} = {js|ΠΣ|js};;
612+
]}
613+
*)
430614
external toUpperCase : t = "" [@@bs.send.pipe: t]
615+
616+
(**
617+
[toLocaleUpperCase str] converts [str] to upper case using the current locale
618+
*)
431619
external toLocaleUpperCase : t = "" [@@bs.send.pipe: t]
432620

621+
(**
622+
[trim str] returns a string that is [str] with whitespace stripped from both ends. Internal whitespace is not removed.
623+
624+
@example {[
625+
trim " abc def " = "abc def"
626+
trim "\n\r\t abc def \n\n\t\r " = "abc def"
627+
]}
628+
*)
433629
external trim : t = "" [@@bs.send.pipe: t]
434630

435631
(* HTML wrappers *)
632+
633+
(**
634+
[anchor anchorName anchorText] creates a string with an HTML [<a>] element with [name] attribute of [anchorName] and [anchorText] as its content.
635+
636+
@example {[
637+
anchor "page1" "Page One" = "<a name=\"page1\">Page One</a>"
638+
]}
639+
*)
436640
external anchor : t -> t = "" [@@bs.send.pipe: t] (** ES2015 *)
641+
642+
(**
643+
[link urlText linkText] creates a string withan HTML [<a>] element with [href] attribute of [urlText] and [linkText] as its content.
644+
645+
@example {[
646+
link "page2.html" "Go to page two" = "<a href=\"page2.html\">Go to page two</a>"
647+
]}
648+
*)
437649
external link : t -> t = "" [@@bs.send.pipe: t] (** ES2015 *)
438650

439651
external castToArrayLike : t -> t Js_array.array_like = "%identity"

0 commit comments

Comments
 (0)