From 99721595c0d16a5cd24b7d2f0012700212b18371 Mon Sep 17 00:00:00 2001
From: Bladymir Tellez <btellez@gmail.com>
Date: Sun, 23 Aug 2020 08:12:43 -0500
Subject: [PATCH] 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.
---
 scripts/library.js | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/scripts/library.js b/scripts/library.js
index 465c366..a7d4ce0 100644
--- a/scripts/library.js
+++ b/scripts/library.js
@@ -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)
   }