Skip to content

Commit 704e4e4

Browse files
committed
Add String.isEmpty and String.capitalize
1 parent 66723ba commit 704e4e4

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
@@ -170,4 +170,13 @@ external splitByRegExpAtMost: (string, Stdlib_RegExp.t, ~limit: int) => array<op
170170

171171
@send external localeCompare: (string, string) => float = "localeCompare"
172172

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

runtime/Stdlib_String.resi

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

796+
/**
797+
`isEmpty(str)` returns `true` if the string is empty (has zero length),
798+
`false` otherwise.
799+
800+
## Examples
801+
802+
```rescript
803+
String.isEmpty("") == true
804+
String.isEmpty("hello") == false
805+
String.isEmpty(" ") == true
806+
```
807+
*/
808+
let isEmpty: string => bool
809+
810+
/**
811+
`capitalize(str)` returns a new string with the first character converted to
812+
uppercase and the remaining characters unchanged. If the string is empty,
813+
returns the empty string.
814+
815+
## Examples
816+
817+
```rescript
818+
String.capitalize("hello") == "Hello"
819+
String.capitalize("HELLO") == "HELLO"
820+
String.capitalize("hello world") == "Hello world"
821+
String.capitalize("") == ""
822+
```
823+
*/
824+
let capitalize: string => string
825+
796826
/**
797827
`slice(str, ~start, ~end)` returns the substring of `str` starting at
798828
character `start` up to but not including `end`.

0 commit comments

Comments
 (0)