Add Helper Functions to Library

- Add `not` to simplify if expressions where the negation is wanted.
- Add `cons`, `push`, `pop` to operate on lists.
- Add `object` to make constructing an object simpler.
This commit is contained in:
Bladymir Tellez 2020-08-23 08:12:43 -05:00
parent e375668143
commit 99721595c0

View File

@ -415,6 +415,10 @@ function Library (client) {
return args[args.length - 1]
}
this.not = (a) => {
return !a
}
// Arrays
this.each = (arr, fn) => { // Run a function for each element in a list.
@ -451,6 +455,18 @@ function Library (client) {
return item.length
}
this.cons = (arr, item) => { // Retruns a new array with the item.
return array.concat([item])
}
this.push = (arr, item) => { // Appends the item item into the existing list.
return array.push(item)
}
this.pop = (arr) => { // Pop the last item from the list and return the item.
return arr.pop();
}
this.first = (arr) => { // Returns the first item of a list.
return arr[0]
}
@ -498,6 +514,14 @@ function Library (client) {
}, h)
}
this.object = (...entries) => { // Creates an object with provided entries.
const result = {}
for (let i = 0; i < entries.length; i += 2) {
result[entries[i]] = entries[i + 1]
}
return result
}
this.keys = (item) => { // Returns a list of the object's keys
return Object.keys(item)
}