Rez Standard Library
Matt Mower
Boolean
Boolean.rand()
Returns true
|false
with an equal probability.
> Array.nOf(50, Boolean.rand) > [true, true, true, false, false, true, false, false, false, true, true, true, false, true, false, true, > true, true, false, false, true, true, true, false, true, true, false, true, false, false, true, false, > true, true, false, true, true, true, true, true, true, false, true, false, true, true, false, true, > false, false]
Object
copy()
Returns a deep-copy of the given object.
> const a = {a: 1, b: 2, c: 'frog', d: false}; > const b = a.copy(); > a["a"] = 0; > `${a["a"]},${b["a"]}` > '0,1'
objMap(f)
where f(v, k)`
Returns a new object containing the same keys as this object where the values are the values of this object passed through f
.
> ({a: 1, b: 2, c: 4}).objMap((v) => 2*v) > {a: 2, b: 4, c: 8} > ({a: 1, b: 2, c: 3, d: 4, e: 5}).objMap((v, k) => ["a", "e", "i", "o", "u"].includes(k) ? v * 2 : v) > {a: 2, b: 2, c: 3, d: 4, e: 10}
dqWrap()
Returns a string representation of this object wrapped in double quotes.
> false.dqWrap() > '"false"'
Array
Array.equals(l1, l2)
Returns true if l1
and l2
are both arrays of the same length and have the same value at each index.
> Array.equals([1, 2, 3], [1, 2, 3]) > true > Array.equals([3, 2, 1], [1, 2, 3]) > false
Array.nOf(number, value)
Returns a new array containing number
elements of value
. If value
is a function the array will contain the results of calling the function.
> Array.nOf(10, "a") > ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'] > > Array.nOf(10, Math.random) > [0.834213541355117, 0.9961046067554495, 0.3864732125663226, 0.2319465459145894, 0.8445399411663749, > 0.875849725396377, 0.7146237562228359, 0.3918156859145241, 0.503402748321524, 0.7664109620436144]
Array.zip(arr1, arr2)
The zip method creates pairs of elements from two arrays based on their position. The resulting array will have the same length as the first array (array1). If the second array is shorter, undefined values will be paired with the remaining elements from the first array. If the second array is longer, the extra elements are ignored.
> names = ['Alice', 'Bob', 'Charlie']; > ages = [25, 30, 35]; > Array.zip(names, ages); > [['Alice', 25], ['Bob', 30], ['Charlie', 35]]
max()
Returns the maximum value of the elements of this array.
> [23, 16, 88, 45, 39, 42, 51].max() > 88
min()
Returns the minimum value of the elements of this array.
> [23, 16, 88, 45, 39, 42, 51].min() > 16
englishList()
Returns a string formatted list of the array that makes sense for 1, 2, or many items.
> [].englishList() > '' > [1].englishList() > '1' > [1, 2].englishList() > '1 and 2' > [1, 2, 3].englishList() > '1, 2, and 3' > ["a", "b", "c", "d", "e"].englishList() > 'a, b, c, d, and e'
remove(elem)
Modifies this array to remove the specified element.
> ["a", "b", "c", "d", "e"].remove("c") > ['a', 'b', 'd', 'e']
fyShuffle()
Randomizes the elements of this array using the Fisher-Yates Shuffle.
> ["a", "b", "c", "d", "e", "f", "g"].fyShuffle() > ['b', 'e', 'a', 'g', 'f', 'c', 'd']
randomElement()
Returns a randomly selected element of this array.
> ["a", "b", "c", "d"].randomElement() > 'c'
randomIndex()
Returns the index position of a randomly selected element of this array.
> ["a", "b", "c", "d"].randomIndex() > 0
frequencies()
Returns a map containing the elements of this array as keys and the number of times that element appears in the array as value.
> [1, 1, 2, 2, 2, 3, 4, 4, 5, 5, 5, 5].frequencies() > {1: 2, 2: 3, 3: 1, 4: 2, 5: 4} > ['t','h','e',' ','q','u','i','c','k',' ','b','r','o','w','n',' ','f','o','x',' ','j','u','m','p','e','d',' ','o','v','e','r',' ','t','h','e',' ','l','a','z','y',' ','d','o','g'].frequencies() > {"t" => 2, "h" => 2, "e" => 4, " " => 8, "q" => 1, "u" => 2, "i" => 1, > "c" => 1, "k" => 1, "b" => 1, "r" => 2, "o" => 4, "w" => 1, "n" => 1, > "f" => 1, "x" => 1, "j" => 1, "m" => 1, "p" => 1, "d" => 2, "v" => 1, > "l" => 1, "a" => 1, "z" => 1, "y" => 1, "g" => 1}
sum()
Returns the sum of the numeric elements of this array.
> [1, 2, 3, 4, 5].sum() > 15
startsWithSequence(lst)
Returns true if this array starts with the elements contained in the lst
parameter.
> [1, 2, 3, 4].startsWithSequence([1, 2]) > true > [2, 3, 4, 5].startsWithSequence([1, 2]) > false
take(n)
Removes the first n
elements of this array, returning them in a new array.
> const a = [1, 2, 3, 4, 5]; > const b = a.take(2); > a > [3, 4, 5] > b > [1, 2]
refs()
Given an array of Rez element ids, return a new array containing the objects with those ids.
> ["game", "player"].refs() > [RezGame, RezActor]
ids()
Given an array of Rez element objects, return a new array containing the ids of those elements.
> [$("game"), $("player")].ids() > ['game', 'player']
sample(p)
Returns an array containing a sample of the elements of this array. The size of the sample will be
approximately p%
of the size of this array.
> Array.nOf(100, (e, i) => i).sample(10) > [32, 8, 24, 43, 34, 58, 5, 51, 78, 88] > (3).times(() => console.log(Array.from({length: 26}, (_, i) => String.fromCharCode(97 + i)).sample(25))) > ['u', 'm', 'n', 'k', 'y', 'e', 'c'] > ['v', 's', 'f', 'e', 'w', 'l', 'j'] > ['v', 'i', 'b', 'g', 'c', 'f', 'n']
splitWith(pred)
Returns a new array containing two sub-arrays formed of the elements of this array. The first sub-array
contains all the elements for which pred(e)
returns true
and the second sub-array those which
return false
.
> [1,2,3,4,5,5,6,7,8,8].splitWith((f) => f % 2 == 0) > [[2,4,6,8,8],[1,3,5,5,7]]
String
beginsWithConsonant()
beginsWithVowel()
capitalize()
possessive()
camelize()
kebabToSnakeCase()
toSnakeCase()
parseTime()
randomId()
wrapWith(prefix, suffix)
Returns a string formed of the current strip wrapped with a prefix and a suffix.
> "bar".wrapWith("*") > '*bar*' > "bar".wrapWith("[", "]") > '[bar]'
indefiniteArticle()
Returns the appropriate indefinite article ("a" or "an") for the this string.
> "fox".indefiniteArticle() > 'a' > "object".indefiniteArticle() > 'an'
Set
union(otherSet)
Returns a new Set
containing the elements of this set and the otherSet
.
> a = new Set(["a", "b", "c", "d"]) > b = new Set(["c", "d", "e", "f"]) > a.union(b) > Set("a", "b", "c", "d", "e", "f")
intersection(otherSet)
Returns a new Set
containing the elements of this set that also exist in the otherSet
.
> a = new Set(["a", "b", "c", "d"]) > b = new Set(["c", "d", "e", "f"]) > a.intersection(b) > Set("c", "d")
difference(otherSet)
Returns a new Set
containing the elements of this set that do not exist in the otherSet
.
> a = new Set(["a", "b", "c", "d"]) > b = new Set(["c", "d", "e", "f"]) > a.difference(b) > Set("a", "b") > b.difference(a) > Set("e", "f")
equals(otherSet)
Return true
iff this Set
has the same elements as the otherSet
.
> a = new Set(["a", "b", "c"]) > b = new Set(["b", "c", "d"]) > c = new Set(["a", "b", "c"]) > a.equals(b) > false > a.equals(c) > true
hasSubset(otherSet)
Returns true
iff this Set
has every element of the otherSet
.
> a = new Set(["a", "b", "c", "d"]) > b = new Set(["b", "c"]) > c = new Set(["d", "e"]) > a.hasSubset(b) > true > a.hasSubset(c) > false