-
Notifications
You must be signed in to change notification settings - Fork 471
add belt.String #2758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
add belt.String #2758
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
(* Copyright (C) 2018 Authors of BuckleScript | ||
* | ||
* 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. *) | ||
|
||
(** JavaScript String API *) | ||
|
||
type t = string | ||
|
||
|
||
(** [concat append original] returns a new string with [append] added after [original]. | ||
|
||
@example {[ | ||
concat "a" "b" = "ab";; | ||
]} | ||
*) | ||
external concat : t -> t -> t = "" [@@bs.send] | ||
|
||
(** [concat arr original] returns a new string consisting of each item of an array of strings added to the [original] string. | ||
|
||
@example {[ | ||
concatMany "1st" [|"2nd"; "3rd"; "4th"|] = "1st2nd3rd4th";; | ||
]} | ||
*) | ||
external concatMany : t -> t array -> t = "concat" [@@bs.send] [@@bs.splice] | ||
|
||
(** ES2015: | ||
[endsWith substr str] returns [true] if the [str] ends with [substr], [false] otherwise. | ||
|
||
@example {[ | ||
endsWith "BuckleScript" "Script" = true;; | ||
endsWith "BuckleShoes" "Script" = false;; | ||
]} | ||
*) | ||
external endsWith : t -> t -> bool = "" [@@bs.send] | ||
|
||
|
||
(** | ||
[includes s searchValue ] returns [true] if [searchValue] is found anywhere within [s], [false] otherwise. | ||
|
||
@example {[ | ||
includes "programmer" "gram" = true;; | ||
includes "programmer" "er" = true;; | ||
includes "programmer" "pro" = true;; | ||
includes "programmer" "xyz" = false;; | ||
]} | ||
*) | ||
external includes : t -> t -> bool = "" [@@bs.send] (** ES2015 *) | ||
|
||
|
||
|
||
(** | ||
[repeat n s] returns a string that consists of [n] repetitions of [s]. Raises [RangeError] if [n] is negative. | ||
|
||
@example {[ | ||
repeat "ha" 3 = "hahaha" | ||
repeat "empty" 0 = "" | ||
]} | ||
*) | ||
external repeat : t -> int -> t = "" [@@bs.send] (** ES2015 *) | ||
|
||
(** [replace string substr newSubstr ] returns a new string which is | ||
identical to [string] except with the first matching instance of [substr] | ||
replaced by [newSubstr]. | ||
|
||
[substr] is treated as a verbatim string to match, not a regular | ||
expression. | ||
|
||
@example {[ | ||
replace "old string" "old" "new" = "new string" | ||
replace "the cat and the dog" "the" "this" = "this cat and the dog" | ||
]} | ||
*) | ||
external replace : t -> t -> t -> t = "" [@@bs.send] | ||
|
||
|
||
|
||
(** | ||
[split delimiter str] splits the given [str] at every occurrence of [delimiter] and returns an | ||
array of the resulting substrings. | ||
|
||
@example {[ | ||
split "2018-01-02" "-" = [|"2018"; "01"; "02"|];; | ||
split "a,b,,c" "," = [|"a"; "b"; ""; "c"|];; | ||
split "good::bad as great::awful" "::" = [|"good"; "bad as great"; "awful"|];; | ||
split "has-no-delimiter" ";" = [|"has-no-delimiter"|];; | ||
]}; | ||
*) | ||
external split : t -> t -> t array = "" [@@bs.send] | ||
|
||
(** | ||
[splitAtMost str delimiter 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. | ||
|
||
@example {[ | ||
splitAtMost "ant/bee/cat/dog/elk" "/" 3 = [|"ant"; "bee"; "cat"|];; | ||
splitAtMost "ant/bee/cat/dog/elk" "/" 0 = [| |];; | ||
splitAtMost "ant/bee/cat/dog/elk" "/" 9 = [|"ant"; "bee"; "cat"; "dog"; "elk"|];; | ||
]} | ||
*) | ||
external splitAtMost: t -> t -> int -> t array = "split" [@@bs.send] | ||
|
||
|
||
(** ES2015: | ||
[startsWith str substr] returns [true] if the [str] starts with [substr], [false] otherwise. | ||
|
||
@example {[ | ||
startsWith "BuckleScript" "Buckle" = true;; | ||
startsWith "BuckleScript" "" = true;; | ||
startsWith "JavaScript" "Buckle" = false;; | ||
]} | ||
*) | ||
external startsWith : t -> t -> bool = "" [@@bs.send] | ||
|
||
|
||
(** | ||
[substr ~from: n str] returns the substring of [str] from position [n] to the end of the string. | ||
|
||
If [n] is less than zero, the starting position is the length of [str] - [n]. | ||
|
||
If [n] is greater than or equal to the length of [str], returns the empty string. | ||
|
||
@example {[ | ||
substr ~from: 3 "abcdefghij" = "defghij" | ||
substr ~from: (-3) "abcdefghij" = "hij" | ||
substr ~from: 12 "abcdefghij" = "" | ||
]} | ||
*) | ||
external substr : from:int -> t = "" [@@bs.send.pipe: t] | ||
|
||
|
||
(** | ||
[trim str] returns a string that is [str] with whitespace stripped from both ends. Internal whitespace is not removed. | ||
|
||
@example {[ | ||
trim " abc def " = "abc def" | ||
trim "\n\r\t abc def \n\n\t\r " = "abc def" | ||
]} | ||
*) | ||
external trim : t -> t = "" [@@bs.send] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good place to use label?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can PR and target your branch if you want.
I'm planning on also adding
Belt.String.length
,Belt.String.slice/sliceToEnd
,Belt.String.get
,Belt.String.indexOf
if that's ok with you.should it also include
Belt.String.replaceRegex
,Belt.String.matchRegex
ou should they just remain inJs.String
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the type of indexOf, I suggest we avoid char type in the API, otherwise it looks good to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the type would be
t -> t -> int option