Skip to content

Commit 8759cfb

Browse files
committed
Add String.isEmpty and String.capitalize
1 parent 81f4b2f commit 8759cfb

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

lib/es6/Stdlib_String.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,23 @@ function searchOpt(s, re) {
2525

2626
}
2727

28+
function isEmpty(s) {
29+
return s.length === 0;
30+
}
31+
32+
function capitalize(s) {
33+
if (s.length === 0) {
34+
return s;
35+
} else {
36+
return s[0].toUpperCase() + s.slice(1);
37+
}
38+
}
39+
2840
export {
2941
indexOfOpt,
3042
lastIndexOfOpt,
3143
searchOpt,
44+
isEmpty,
45+
capitalize,
3246
}
3347
/* No side effect */

lib/js/Stdlib_String.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,21 @@ function searchOpt(s, re) {
2525

2626
}
2727

28+
function isEmpty(s) {
29+
return s.length === 0;
30+
}
31+
32+
function capitalize(s) {
33+
if (s.length === 0) {
34+
return s;
35+
} else {
36+
return s[0].toUpperCase() + s.slice(1);
37+
}
38+
}
39+
2840
exports.indexOfOpt = indexOfOpt;
2941
exports.lastIndexOfOpt = lastIndexOfOpt;
3042
exports.searchOpt = searchOpt;
43+
exports.isEmpty = isEmpty;
44+
exports.capitalize = capitalize;
3145
/* No side effect */

runtime/Stdlib_String.res

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ external substringToEnd: (string, ~start: int) => string = "substring"
168168

169169
@send external localeCompare: (string, string) => float = "localeCompare"
170170

171+
let isEmpty = s => length(s) == 0
172+
173+
let capitalize = s =>
174+
if isEmpty(s) {
175+
s
176+
} else {
177+
toUpperCase(getUnsafe(s, 0)) ++ sliceToEnd(s, ~start=1)
178+
}
179+
171180
external ignore: string => unit = "%ignore"
172181

173182
@get_index external getSymbolUnsafe: (string, Stdlib_Symbol.t) => 'a = ""

runtime/Stdlib_String.resi

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,36 @@ String.searchOpt("no numbers", /\d+/) == None
808808
*/
809809
let searchOpt: (string, Stdlib_RegExp.t) => option<int>
810810

811+
/**
812+
`isEmpty(str)` returns `true` if the string is empty (has zero length),
813+
`false` otherwise.
814+
815+
## Examples
816+
817+
```rescript
818+
String.isEmpty("") == true
819+
String.isEmpty("hello") == false
820+
String.isEmpty(" ") == true
821+
```
822+
*/
823+
let isEmpty: string => bool
824+
825+
/**
826+
`capitalize(str)` returns a new string with the first character converted to
827+
uppercase and the remaining characters unchanged. If the string is empty,
828+
returns the empty string.
829+
830+
## Examples
831+
832+
```rescript
833+
String.capitalize("hello") == "Hello"
834+
String.capitalize("HELLO") == "HELLO"
835+
String.capitalize("hello world") == "Hello world"
836+
String.capitalize("") == ""
837+
```
838+
*/
839+
let capitalize: string => string
840+
811841
/**
812842
`slice(str, ~start, ~end)` returns the substring of `str` starting at
813843
character `start` up to but not including `end`.

0 commit comments

Comments
 (0)