summaryrefslogtreecommitdiff
path: root/src/utils.mjs
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-02-23 06:05:19 -0300
committerEuAndreh <eu@euandre.org>2024-02-23 06:05:21 -0300
commitc36bf8e3577da31cf6d575879c7e92d3e9c7e4f1 (patch)
tree4fd5414e76490e297c4c770ff35e09149ef3658f /src/utils.mjs
parentRemove C code and cleanup repository (diff)
downloadpapod-c36bf8e3577da31cf6d575879c7e92d3e9c7e4f1.tar.gz
papod-c36bf8e3577da31cf6d575879c7e92d3e9c7e4f1.tar.xz
Big cleanup
- delete all SQLite Node-API code: we'll use the C++ one instead; - implement hero.mjs, with tests! - use ESM all over.
Diffstat (limited to 'src/utils.mjs')
-rw-r--r--src/utils.mjs76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/utils.mjs b/src/utils.mjs
new file mode 100644
index 0000000..e0e20a7
--- /dev/null
+++ b/src/utils.mjs
@@ -0,0 +1,76 @@
+export const eq = (a, b) => {
+ if (a === b) {
+ return true;
+ }
+
+ if (a === null || b === null) {
+ return false;
+ }
+
+ if (typeof a != "object" || typeof b != "object") {
+ return false;
+ }
+
+ if (Array.isArray(a) !== Array.isArray(b)) {
+ return false;
+ }
+
+ if (Object.keys(a).length !== Object.keys(b).length) {
+ return false;
+ }
+
+ for (const k in a) {
+ if (!b.hasOwnProperty(k)) {
+ return false;
+ }
+ if (!eq(a[k], b[k])) {
+ return false;
+ }
+ }
+
+ return true;
+};
+
+export const keys = (ks, obj) =>
+ ks.reduce(
+ (ret, k) =>
+ obj.hasOwnProperty(k) ? {...ret, [k]: obj[k]} : ret,
+ {},
+ );
+
+export const difference = (a, b) => {
+ const diff = new Set(a);
+ for (const el of b) {
+ diff.delete(el);
+ }
+ return diff;
+};
+
+export const assocIn = (obj, path, value) =>
+ path.length === 0 ? obj :
+ path.length === 1 ? { ...obj, [path[0]]: value } :
+ {
+ ...obj,
+ [path[0]]: assocIn(
+ (obj[path[0]] || {}),
+ path.slice(1),
+ value
+ )
+ };
+
+export const getIn = (obj, path) =>
+ path.length === 0 ? obj :
+ getIn(obj?.[path[0]], path.slice(1));
+
+export const first = (arr, fn) => {
+ for (const x of arr) {
+ const ret = fn(x);
+ if (ret) {
+ return ret;
+ }
+ }
+
+ return null;
+};
+
+export const log = o => console.error(JSON.stringify(o));