diff --git a/lib/es6/Stdlib_Array.js b/lib/es6/Stdlib_Array.js index b1bbc32728..f733d81a00 100644 --- a/lib/es6/Stdlib_Array.js +++ b/lib/es6/Stdlib_Array.js @@ -22,6 +22,10 @@ function fromInitializer(length, f) { return arr; } +function isEmpty(arr) { + return arr.length === 0; +} + function equal(a, b, eq) { let len = a.length; if (len === b.length) { @@ -183,6 +187,7 @@ export { fromInitializer, equal, compare, + isEmpty, indexOfOpt, lastIndexOfOpt, reduce, diff --git a/lib/es6/Stdlib_Dict.js b/lib/es6/Stdlib_Dict.js index 51a70b3144..6741659742 100644 --- a/lib/es6/Stdlib_Dict.js +++ b/lib/es6/Stdlib_Dict.js @@ -5,6 +5,14 @@ function $$delete$1(dict, string) { delete(dict[string]); } +function size(dict) { + return Object.keys(dict).length; +} + +function isEmpty(dict) { + return Object.keys(dict).length === 0; +} + function forEach(dict, f) { Object.values(dict).forEach(value => f(value)); } @@ -24,6 +32,8 @@ function mapValues(dict, f) { export { $$delete$1 as $$delete, + size, + isEmpty, forEach, forEachWithKey, mapValues, diff --git a/lib/es6/Stdlib_Map.js b/lib/es6/Stdlib_Map.js index ae1b9f17e6..27d7851b1a 100644 --- a/lib/es6/Stdlib_Map.js +++ b/lib/es6/Stdlib_Map.js @@ -1 +1,11 @@ -/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */ + + + +function isEmpty(map) { + return map.size === 0; +} + +export { + isEmpty, +} +/* No side effect */ diff --git a/lib/es6/Stdlib_Set.js b/lib/es6/Stdlib_Set.js index ae1b9f17e6..469aca57da 100644 --- a/lib/es6/Stdlib_Set.js +++ b/lib/es6/Stdlib_Set.js @@ -1 +1,11 @@ -/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */ + + + +function isEmpty(set) { + return set.size === 0; +} + +export { + isEmpty, +} +/* No side effect */ diff --git a/lib/es6/Stdlib_String.js b/lib/es6/Stdlib_String.js index d68111d2a3..b6e5069cab 100644 --- a/lib/es6/Stdlib_String.js +++ b/lib/es6/Stdlib_String.js @@ -25,9 +25,23 @@ function searchOpt(s, re) { } +function isEmpty(s) { + return s.length === 0; +} + +function capitalize(s) { + if (s.length === 0) { + return s; + } else { + return s[0].toUpperCase() + s.slice(1); + } +} + export { indexOfOpt, lastIndexOfOpt, searchOpt, + isEmpty, + capitalize, } /* No side effect */ diff --git a/lib/js/Stdlib_Array.js b/lib/js/Stdlib_Array.js index f5313a3e3b..e62a6efb4e 100644 --- a/lib/js/Stdlib_Array.js +++ b/lib/js/Stdlib_Array.js @@ -22,6 +22,10 @@ function fromInitializer(length, f) { return arr; } +function isEmpty(arr) { + return arr.length === 0; +} + function equal(a, b, eq) { let len = a.length; if (len === b.length) { @@ -182,6 +186,7 @@ exports.make = make; exports.fromInitializer = fromInitializer; exports.equal = equal; exports.compare = compare; +exports.isEmpty = isEmpty; exports.indexOfOpt = indexOfOpt; exports.lastIndexOfOpt = lastIndexOfOpt; exports.reduce = reduce; diff --git a/lib/js/Stdlib_Dict.js b/lib/js/Stdlib_Dict.js index ecb9c90844..668d9354ba 100644 --- a/lib/js/Stdlib_Dict.js +++ b/lib/js/Stdlib_Dict.js @@ -5,6 +5,14 @@ function $$delete$1(dict, string) { delete(dict[string]); } +function size(dict) { + return Object.keys(dict).length; +} + +function isEmpty(dict) { + return Object.keys(dict).length === 0; +} + function forEach(dict, f) { Object.values(dict).forEach(value => f(value)); } @@ -23,6 +31,8 @@ function mapValues(dict, f) { } exports.$$delete = $$delete$1; +exports.size = size; +exports.isEmpty = isEmpty; exports.forEach = forEach; exports.forEachWithKey = forEachWithKey; exports.mapValues = mapValues; diff --git a/lib/js/Stdlib_Map.js b/lib/js/Stdlib_Map.js index ae1b9f17e6..a3844084bd 100644 --- a/lib/js/Stdlib_Map.js +++ b/lib/js/Stdlib_Map.js @@ -1 +1,9 @@ -/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */ +'use strict'; + + +function isEmpty(map) { + return map.size === 0; +} + +exports.isEmpty = isEmpty; +/* No side effect */ diff --git a/lib/js/Stdlib_Set.js b/lib/js/Stdlib_Set.js index ae1b9f17e6..d74649202f 100644 --- a/lib/js/Stdlib_Set.js +++ b/lib/js/Stdlib_Set.js @@ -1 +1,9 @@ -/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */ +'use strict'; + + +function isEmpty(set) { + return set.size === 0; +} + +exports.isEmpty = isEmpty; +/* No side effect */ diff --git a/lib/js/Stdlib_String.js b/lib/js/Stdlib_String.js index b653c67e12..09ffc8b903 100644 --- a/lib/js/Stdlib_String.js +++ b/lib/js/Stdlib_String.js @@ -25,7 +25,21 @@ function searchOpt(s, re) { } +function isEmpty(s) { + return s.length === 0; +} + +function capitalize(s) { + if (s.length === 0) { + return s; + } else { + return s[0].toUpperCase() + s.slice(1); + } +} + exports.indexOfOpt = indexOfOpt; exports.lastIndexOfOpt = lastIndexOfOpt; exports.searchOpt = searchOpt; +exports.isEmpty = isEmpty; +exports.capitalize = capitalize; /* No side effect */ diff --git a/runtime/Stdlib_Array.res b/runtime/Stdlib_Array.res index a88869f9a5..1ba1e1a357 100644 --- a/runtime/Stdlib_Array.res +++ b/runtime/Stdlib_Array.res @@ -43,6 +43,8 @@ let fromInitializer = (~length, f) => @get external length: array<'a> => int = "length" +let isEmpty = arr => arr->length === 0 + let rec equalFromIndex = (a, b, i, eq, len) => if i === len { true diff --git a/runtime/Stdlib_Array.resi b/runtime/Stdlib_Array.resi index fff252d987..b6d80ad47f 100644 --- a/runtime/Stdlib_Array.resi +++ b/runtime/Stdlib_Array.resi @@ -87,6 +87,24 @@ someArray @get external length: array<'a> => int = "length" +/** +`isEmpty(array)` returns `true` if the array is empty (has length 0), `false` otherwise. + +## Examples + +```rescript +[]->Array.isEmpty->assertEqual(true) +[1, 2, 3]->Array.isEmpty->assertEqual(false) + +let emptyArray = [] +emptyArray->Array.isEmpty->assertEqual(true) + +let nonEmptyArray = ["hello"] +nonEmptyArray->Array.isEmpty->assertEqual(false) +``` +*/ +let isEmpty: array<'a> => bool + // TODO: Docs @send external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin" @@ -929,7 +947,7 @@ external mapWithIndex: (array<'a>, ('a, int) => 'b) => array<'b> = "map" /** `reduce(xs, init, fn)` -Applies `fn` to each element of `xs` from beginning to end. Function `fn` has two parameters: the item from the list and an “accumulator”; which starts with a value of `init`. `reduce` returns the final value of the accumulator. +Applies `fn` to each element of `xs` from beginning to end. Function `fn` has two parameters: the item from the list and an "accumulator"; which starts with a value of `init`. `reduce` returns the final value of the accumulator. ## Examples @@ -950,7 +968,7 @@ let reduce: (array<'a>, 'b, ('b, 'a) => 'b) => 'b /** `reduceWithIndex(x, init, fn)` -Applies `fn` to each element of `xs` from beginning to end. Function `fn` has three parameters: the item from the array and an “accumulator”, which starts with a value of `init` and the index of each element. `reduceWithIndex` returns the final value of the accumulator. +Applies `fn` to each element of `xs` from beginning to end. Function `fn` has three parameters: the item from the array and an "accumulator", which starts with a value of `init` and the index of each element. `reduceWithIndex` returns the final value of the accumulator. ## Examples diff --git a/runtime/Stdlib_Dict.res b/runtime/Stdlib_Dict.res index 7604962689..21c2bbe331 100644 --- a/runtime/Stdlib_Dict.res +++ b/runtime/Stdlib_Dict.res @@ -18,6 +18,10 @@ let delete = (dict, string) => { @val external keysToArray: dict<'a> => array = "Object.keys" +let size = dict => dict->keysToArray->Stdlib_Array.length + +let isEmpty = dict => dict->size === 0 + @val external valuesToArray: dict<'a> => array<'a> = "Object.values" @val external assign: (dict<'a>, dict<'a>) => dict<'a> = "Object.assign" diff --git a/runtime/Stdlib_Dict.resi b/runtime/Stdlib_Dict.resi index 604c5d4de9..109ae22e38 100644 --- a/runtime/Stdlib_Dict.resi +++ b/runtime/Stdlib_Dict.resi @@ -145,6 +145,44 @@ Console.log(keys) // Logs `["someKey", "someKey2"]` to the console @val external keysToArray: dict<'a> => array = "Object.keys" +/** +`size(dictionary)` returns the number of key/value pairs in the dictionary. + +## Examples +```rescript +let dict = Dict.make() +dict->Dict.size->assertEqual(0) + +dict->Dict.set("someKey", 1) +dict->Dict.set("someKey2", 2) +dict->Dict.size->assertEqual(2) + +// After deleting a key +dict->Dict.delete("someKey") +dict->Dict.size->assertEqual(1) +``` +*/ +let size: dict<'a> => int + +/** +`isEmpty(dictionary)` returns `true` if the dictionary is empty (has no key/value pairs), `false` otherwise. + +## Examples +```rescript +let emptyDict = Dict.make() +emptyDict->Dict.isEmpty->assertEqual(true) + +let dict = Dict.make() +dict->Dict.set("someKey", 1) +dict->Dict.isEmpty->assertEqual(false) + +// After clearing all keys +dict->Dict.delete("someKey") +dict->Dict.isEmpty->assertEqual(true) +``` +*/ +let isEmpty: dict<'a> => bool + /** `valuesToArray(dictionary)` returns an array of all the values of the dictionary. diff --git a/runtime/Stdlib_Map.res b/runtime/Stdlib_Map.res index 7d534aa7d8..2c821c0021 100644 --- a/runtime/Stdlib_Map.res +++ b/runtime/Stdlib_Map.res @@ -7,6 +7,8 @@ type t<'k, 'v> @get external size: t<'k, 'v> => int = "size" +let isEmpty = map => map->size === 0 + @send external clear: t<'k, 'v> => unit = "clear" @send external forEach: (t<'k, 'v>, 'v => unit) => unit = "forEach" diff --git a/runtime/Stdlib_Map.resi b/runtime/Stdlib_Map.resi index 3df63191e3..69513b814a 100644 --- a/runtime/Stdlib_Map.resi +++ b/runtime/Stdlib_Map.resi @@ -96,6 +96,25 @@ let size = map->Map.size // 1 @get external size: t<'k, 'v> => int = "size" +/** +`isEmpty(map)` returns `true` if the map has no key/value pairs, `false` otherwise. + +## Examples +```rescript +let emptyMap = Map.make() +emptyMap->Map.isEmpty->assertEqual(true) + +let map = Map.make() +map->Map.set("someKey", "someValue") +map->Map.isEmpty->assertEqual(false) + +// After clearing the map +map->Map.clear +map->Map.isEmpty->assertEqual(true) +``` +*/ +let isEmpty: t<'k, 'v> => bool + /** Clears all entries in the map. diff --git a/runtime/Stdlib_Set.res b/runtime/Stdlib_Set.res index 61040dc863..02a21ad1c9 100644 --- a/runtime/Stdlib_Set.res +++ b/runtime/Stdlib_Set.res @@ -7,6 +7,8 @@ type t<'a> @get external size: t<'a> => int = "size" +let isEmpty = set => set->size === 0 + @send external clear: t<'a> => unit = "clear" @send external add: (t<'a>, 'a) => unit = "add" diff --git a/runtime/Stdlib_Set.resi b/runtime/Stdlib_Set.resi index 4945add4d1..2b00944932 100644 --- a/runtime/Stdlib_Set.resi +++ b/runtime/Stdlib_Set.resi @@ -95,6 +95,25 @@ let size = set->Set.size // 2 @get external size: t<'a> => int = "size" +/** +`isEmpty(set)` returns `true` if the set has no values, `false` otherwise. + +## Examples +```rescript +let emptySet = Set.make() +emptySet->Set.isEmpty->assertEqual(true) + +let set = Set.make() +set->Set.add("someValue") +set->Set.isEmpty->assertEqual(false) + +// After clearing the set +set->Set.clear +set->Set.isEmpty->assertEqual(true) +``` +*/ +let isEmpty: t<'a> => bool + /** Clears all entries in the set. diff --git a/runtime/Stdlib_String.res b/runtime/Stdlib_String.res index 7aaa139492..c9f68c0c08 100644 --- a/runtime/Stdlib_String.res +++ b/runtime/Stdlib_String.res @@ -170,4 +170,13 @@ external splitByRegExpAtMost: (string, Stdlib_RegExp.t, ~limit: int) => array float = "localeCompare" +let isEmpty = s => length(s) == 0 + +let capitalize = s => + if isEmpty(s) { + s + } else { + toUpperCase(getUnsafe(s, 0)) ++ sliceToEnd(s, ~start=1) + } + external ignore: string => unit = "%ignore" diff --git a/runtime/Stdlib_String.resi b/runtime/Stdlib_String.resi index c74996dea3..3162acea5e 100644 --- a/runtime/Stdlib_String.resi +++ b/runtime/Stdlib_String.resi @@ -793,6 +793,36 @@ String.searchOpt("no numbers", %re("/\d+/")) == None */ let searchOpt: (string, Stdlib_RegExp.t) => option +/** +`isEmpty(str)` returns `true` if the string is empty (has zero length), +`false` otherwise. + +## Examples + +```rescript +String.isEmpty("") == true +String.isEmpty("hello") == false +String.isEmpty(" ") == false +``` +*/ +let isEmpty: string => bool + +/** +`capitalize(str)` returns a new string with the first character converted to +uppercase and the remaining characters unchanged. If the string is empty, +returns the empty string. + +## Examples + +```rescript +String.capitalize("hello") == "Hello" +String.capitalize("HELLO") == "HELLO" +String.capitalize("hello world") == "Hello world" +String.capitalize("") == "" +``` +*/ +let capitalize: string => string + /** `slice(str, ~start, ~end)` returns the substring of `str` starting at character `start` up to but not including `end`.