|
| 1 | +(* Copyright (C) 2018 Authors of BuckleScript |
| 2 | + * |
| 3 | + * This program is free software: you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU Lesser General Public License as published by |
| 5 | + * the Free Software Foundation, either version 3 of the License, or |
| 6 | + * (at your option) any later version. |
| 7 | + * |
| 8 | + * In addition to the permissions granted to you by the LGPL, you may combine |
| 9 | + * or link a "work that uses the Library" with a publicly distributed version |
| 10 | + * of this file to produce a combined library or application, then distribute |
| 11 | + * that combined work under the terms of your choosing, with no requirement |
| 12 | + * to comply with the obligations normally placed on you by section 4 of the |
| 13 | + * LGPL version 3 (or the corresponding section of a later version of the LGPL |
| 14 | + * should you choose to use a later version). |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Lesser General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Lesser General Public License |
| 22 | + * along with this program; if not, write to the Free Software |
| 23 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) |
| 24 | + |
| 25 | +(** JavaScript String API *) |
| 26 | + |
| 27 | +type t = string |
| 28 | + |
| 29 | + |
| 30 | +(** [concat append original] returns a new string with [append] added after [original]. |
| 31 | +
|
| 32 | +@example {[ |
| 33 | + concat "a" "b" = "ab";; |
| 34 | +]} |
| 35 | +*) |
| 36 | +external concat : t -> t -> t = "" [@@bs.send] |
| 37 | + |
| 38 | +(** [concat arr original] returns a new string consisting of each item of an array of strings added to the [original] string. |
| 39 | +
|
| 40 | +@example {[ |
| 41 | + concatMany "1st" [|"2nd"; "3rd"; "4th"|] = "1st2nd3rd4th";; |
| 42 | +]} |
| 43 | +*) |
| 44 | +external concatMany : t -> t array -> t = "concat" [@@bs.send] [@@bs.splice] |
| 45 | + |
| 46 | +(** ES2015: |
| 47 | + [endsWith substr str] returns [true] if the [str] ends with [substr], [false] otherwise. |
| 48 | + |
| 49 | +@example {[ |
| 50 | + endsWith "BuckleScript" "Script" = true;; |
| 51 | + endsWith "BuckleShoes" "Script" = false;; |
| 52 | +]} |
| 53 | +*) |
| 54 | +external endsWith : t -> t -> bool = "" [@@bs.send] |
| 55 | + |
| 56 | + |
| 57 | +(** |
| 58 | + [includes s searchValue ] returns [true] if [searchValue] is found anywhere within [s], [false] otherwise. |
| 59 | + |
| 60 | +@example {[ |
| 61 | + includes "programmer" "gram" = true;; |
| 62 | + includes "programmer" "er" = true;; |
| 63 | + includes "programmer" "pro" = true;; |
| 64 | + includes "programmer" "xyz" = false;; |
| 65 | +]} |
| 66 | +*) |
| 67 | +external includes : t -> t -> bool = "" [@@bs.send] (** ES2015 *) |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +(** |
| 72 | + [repeat n s] returns a string that consists of [n] repetitions of [s]. Raises [RangeError] if [n] is negative. |
| 73 | + |
| 74 | +@example {[ |
| 75 | + repeat "ha" 3 = "hahaha" |
| 76 | + repeat "empty" 0 = "" |
| 77 | +]} |
| 78 | +*) |
| 79 | +external repeat : t -> int -> t = "" [@@bs.send] (** ES2015 *) |
| 80 | + |
| 81 | +(** [replace string substr newSubstr ] returns a new string which is |
| 82 | +identical to [string] except with the first matching instance of [substr] |
| 83 | +replaced by [newSubstr]. |
| 84 | +
|
| 85 | +[substr] is treated as a verbatim string to match, not a regular |
| 86 | +expression. |
| 87 | +
|
| 88 | +@example {[ |
| 89 | + replace "old string" "old" "new" = "new string" |
| 90 | + replace "the cat and the dog" "the" "this" = "this cat and the dog" |
| 91 | +]} |
| 92 | +*) |
| 93 | +external replace : t -> t -> t -> t = "" [@@bs.send] |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +(** |
| 98 | + [split delimiter str] splits the given [str] at every occurrence of [delimiter] and returns an |
| 99 | + array of the resulting substrings. |
| 100 | + |
| 101 | +@example {[ |
| 102 | + split "2018-01-02" "-" = [|"2018"; "01"; "02"|];; |
| 103 | + split "a,b,,c" "," = [|"a"; "b"; ""; "c"|];; |
| 104 | + split "good::bad as great::awful" "::" = [|"good"; "bad as great"; "awful"|];; |
| 105 | + split "has-no-delimiter" ";" = [|"has-no-delimiter"|];; |
| 106 | +]}; |
| 107 | +*) |
| 108 | +external split : t -> t -> t array = "" [@@bs.send] |
| 109 | + |
| 110 | +(** |
| 111 | + [splitAtMost str delimiter n] splits the given [str] at every occurrence of [delimiter] and |
| 112 | + returns an array of the first [n] resulting substrings. If [n] is negative or greater than the |
| 113 | + number of substrings, the array will contain all the substrings. |
| 114 | + |
| 115 | +@example {[ |
| 116 | + splitAtMost "ant/bee/cat/dog/elk" "/" 3 = [|"ant"; "bee"; "cat"|];; |
| 117 | + splitAtMost "ant/bee/cat/dog/elk" "/" 0 = [| |];; |
| 118 | + splitAtMost "ant/bee/cat/dog/elk" "/" 9 = [|"ant"; "bee"; "cat"; "dog"; "elk"|];; |
| 119 | +]} |
| 120 | +*) |
| 121 | +external splitAtMost: t -> t -> int -> t array = "split" [@@bs.send] |
| 122 | + |
| 123 | + |
| 124 | +(** ES2015: |
| 125 | + [startsWith str substr] returns [true] if the [str] starts with [substr], [false] otherwise. |
| 126 | + |
| 127 | +@example {[ |
| 128 | + startsWith "BuckleScript" "Buckle" = true;; |
| 129 | + startsWith "BuckleScript" "" = true;; |
| 130 | + startsWith "JavaScript" "Buckle" = false;; |
| 131 | +]} |
| 132 | +*) |
| 133 | +external startsWith : t -> t -> bool = "" [@@bs.send] |
| 134 | + |
| 135 | + |
| 136 | +(** |
| 137 | + [substr ~from: n str] returns the substring of [str] from position [n] to the end of the string. |
| 138 | + |
| 139 | + If [n] is less than zero, the starting position is the length of [str] - [n]. |
| 140 | + |
| 141 | + If [n] is greater than or equal to the length of [str], returns the empty string. |
| 142 | + |
| 143 | +@example {[ |
| 144 | + substr ~from: 3 "abcdefghij" = "defghij" |
| 145 | + substr ~from: (-3) "abcdefghij" = "hij" |
| 146 | + substr ~from: 12 "abcdefghij" = "" |
| 147 | +]} |
| 148 | +*) |
| 149 | +external substr : from:int -> t = "" [@@bs.send.pipe: t] |
| 150 | + |
| 151 | + |
| 152 | +(** |
| 153 | + [trim str] returns a string that is [str] with whitespace stripped from both ends. Internal whitespace is not removed. |
| 154 | +
|
| 155 | +@example {[ |
| 156 | + trim " abc def " = "abc def" |
| 157 | + trim "\n\r\t abc def \n\n\t\r " = "abc def" |
| 158 | +]} |
| 159 | +*) |
| 160 | +external trim : t -> t = "" [@@bs.send] |
| 161 | + |
0 commit comments