From 7a5b6aefc3723835b8c4e7255cdc6b090c40655d Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 1 Jul 2021 11:44:33 +0100 Subject: [PATCH] Formatted code with prettier --- .webpack.config.js | 6 +- docs/web/demos.js | 10 +-- docs/web/index.js | 34 +++++------ src/engine/local.js | 4 +- tests/CRUD.js | 135 +++++++++++++++++------------------------ tests/browserEngine.js | 18 +++--- tests/localEngine.js | 28 ++++----- 7 files changed, 105 insertions(+), 130 deletions(-) diff --git a/.webpack.config.js b/.webpack.config.js index 7496efa..9e3700b 100644 --- a/.webpack.config.js +++ b/.webpack.config.js @@ -5,12 +5,12 @@ const package = require("./package.json"); module.exports = { entry: { StormDB: "./src/stormdb.js", - BrowserEngine: "./src/engine/browser.js" + BrowserEngine: "./src/engine/browser.js", }, output: { path: path.resolve(__dirname, "dist"), filename: "[name].min.js", - library: "[name]" + library: "[name]", }, - plugins: [new Webpack.BannerPlugin(`StormDB ${package.version}`)] + plugins: [new Webpack.BannerPlugin(`StormDB ${package.version}`)], }; diff --git a/docs/web/demos.js b/docs/web/demos.js index 4739aa5..cdeea71 100644 --- a/docs/web/demos.js +++ b/docs/web/demos.js @@ -6,18 +6,18 @@ const mapItemCode = `db.set("newList", [1, 2, 3]);\ndb.get("newList").map(x => x const demos = { setItem: { code: setItemCode, - name: "Set Item" + name: "Set Item", }, setItemPair: { code: setItemPair, - name: "Set Item Pair" + name: "Set Item Pair", }, deleteItem: { code: deleteItemCode, - name: "Delete Item" + name: "Delete Item", }, mapItem: { code: mapItemCode, - name: "Map List" - } + name: "Map List", + }, }; diff --git a/docs/web/index.js b/docs/web/index.js index a59c65c..2b88ec8 100644 --- a/docs/web/index.js +++ b/docs/web/index.js @@ -12,7 +12,7 @@ function getQueryVariable(variable) { return new URLSearchParams(window.location.search).get(variable); } -var displayOutput = function(msg, colour = "black") { +var displayOutput = function (msg, colour = "black") { const p = document.createElement("p"); p.textContent = msg; p.classList += " output"; @@ -47,7 +47,7 @@ function syntaxHighlight(json) { .replace(/>/g, ">"); return json.replace( /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, - function(match) { + function (match) { var cls = "number"; if (/^"/.test(match)) { if (/:$/.test(match)) { @@ -65,11 +65,11 @@ function syntaxHighlight(json) { ); } -const getDBData = function() { +const getDBData = function () { return JSON.stringify(db.value(), null, 4); }; -const updateDatabaseState = function() { +const updateDatabaseState = function () { const dbData = getDBData(); stateContents.innerHTML = ""; @@ -83,7 +83,7 @@ function clearError() { // update database state on load updateDatabaseState(); -runButton.addEventListener("click", function() { +runButton.addEventListener("click", function () { clearError(); try { let codeToRun = editor.getCode(); @@ -94,7 +94,7 @@ runButton.addEventListener("click", function() { } }); -reloadButton.addEventListener("click", function() { +reloadButton.addEventListener("click", function () { clearError(); try { let loadedData = JSON.parse(localStorage.getItem("db")); @@ -118,11 +118,11 @@ reloadButton.addEventListener("click", function() { updateDatabaseState(); }); -saveButton.addEventListener("click", function() { +saveButton.addEventListener("click", function () { db.save(); }); -resetButton.addEventListener("click", function() { +resetButton.addEventListener("click", function () { clearError(); db.state = {}; localStorage.setItem("db", "{}"); @@ -130,7 +130,7 @@ resetButton.addEventListener("click", function() { }); const editor = new CodeFlask("#editor", { - lineNumbers: true + lineNumbers: true, }); // demo selector logic @@ -145,12 +145,12 @@ db.default({ string: "test", numbers: 123, objects: { - property: "test property" - } + property: "test property", + }, }); db.save(); -demoKeys.forEach(key => { +demoKeys.forEach((key) => { let demo = demos[key]; const option = document.createElement("option"); option.textContent = demo["name"]; @@ -158,7 +158,7 @@ demoKeys.forEach(key => { demoSelector.appendChild(option); }); -demoSelector.addEventListener("change", function() { +demoSelector.addEventListener("change", function () { loadDemo(demoSelector.value); }); @@ -171,22 +171,22 @@ if (queryCode !== null) { async function loadVersion() { fetch("https://unpkg.com/stormdb/package.json") - .then(function(response) { + .then(function (response) { return response.json(); }) - .then(function(data) { + .then(function (data) { let versionNumber = data.version; document.getElementById( "versionNumber" ).innerText = `StormDB v${versionNumber}`; }) - .catch(function(error) {}); + .catch(function (error) {}); } loadVersion(); // copy URL function -copyButton.addEventListener("click", function() { +copyButton.addEventListener("click", function () { let url = new URL(window.location.href); url.searchParams.set("code", encodeURI(editor.getCode())); navigator.clipboard.writeText(url); diff --git a/src/engine/local.js b/src/engine/local.js index eded57b..7596965 100644 --- a/src/engine/local.js +++ b/src/engine/local.js @@ -37,8 +37,8 @@ module.exports = class LocalEngine { // if async, return promise wrapper around async writefile if (this.async) { return new Promise( - function(resolve, reject) { - fs.writeFile(this.path, this.serialize(data), function(error) { + function (resolve, reject) { + fs.writeFile(this.path, this.serialize(data), function (error) { if (error) return reject(error); else resolve(); }); diff --git a/tests/CRUD.js b/tests/CRUD.js index 73c4c76..c1a133c 100644 --- a/tests/CRUD.js +++ b/tests/CRUD.js @@ -6,32 +6,27 @@ const StormDB = require("../index.js"); const exampleDBPath = path.resolve(__dirname, "./fixtures/example.stormdb"); const emptyDBPath = path.resolve(__dirname, "./fixtures/empty-db.stormdb"); -const deleteFile = function(fileName) { +const deleteFile = function (fileName) { fs.unlinkSync(fileName); }; -describe("StormDB", function() { - it("should successfully load local database", function() { +describe("StormDB", function () { + it("should successfully load local database", function () { const engine = new StormDB.localFileEngine(exampleDBPath); new StormDB(engine); }); - describe(".value()", function() { - it("should throw error on non-existant data", function() { + describe(".value()", function () { + it("should throw error on non-existant data", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); - assert.throws(() => - db - .get("random123") - .get("random123") - .value() - ); + assert.throws(() => db.get("random123").get("random123").value()); }); }); - describe(".get()", function() { - it("should successfully get values from database", function() { + describe(".get()", function () { + it("should successfully get values from database", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -39,7 +34,7 @@ describe("StormDB", function() { assert.strictEqual(value, "string"); }); - it("should successfully get values with multiple property accessors", function() { + it("should successfully get values with multiple property accessors", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -48,8 +43,8 @@ describe("StormDB", function() { }); }); - describe(".set()", function() { - it("should successfully change value in database", function() { + describe(".set()", function () { + it("should successfully change value in database", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -59,7 +54,7 @@ describe("StormDB", function() { assert.strictEqual(value, "changed"); }); - it("should successfully set value in database", function() { + it("should successfully set value in database", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -69,34 +64,28 @@ describe("StormDB", function() { assert.strictEqual(value, "data"); }); - it("should successfully set nested values with multiple property accessors", function() { + it("should successfully set nested values with multiple property accessors", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); db.set("one.two", "test"); - let value = db - .get("one") - .get("two") - .value(); + let value = db.get("one").get("two").value(); assert.strictEqual(value, "test"); }); - it("should successfully set on nested object", function() { + it("should successfully set on nested object", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); db.get("test-obj").set("data", "test"); - let value = db - .get("test-obj") - .get("data") - .value(); + let value = db.get("test-obj").get("data").value(); assert.strictEqual(value, "test"); }); - describe("setting key-value pair", function() { - it("should successfully set key-value pair", function() { + describe("setting key-value pair", function () { + it("should successfully set key-value pair", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -106,20 +95,17 @@ describe("StormDB", function() { assert.strictEqual(value, "testing"); }); - it("should successfully set key-value pair with shorthand syntax", function() { + it("should successfully set key-value pair with shorthand syntax", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); db.set("one.two", "testing"); - let value = db - .get("one") - .get("two") - .value(); + let value = db.get("one").get("two").value(); assert.strictEqual(value, "testing"); }); - it("should be able to use non-strings as key", function() { + it("should be able to use non-strings as key", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -162,24 +148,19 @@ describe("StormDB", function() { assert.strictEqual(value, undefined); }); - it("should successfully remove nested values", function() { + it("should successfully remove nested values", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); - db.get("test-obj") - .get("nested-key") - .delete(); + db.get("test-obj").get("nested-key").delete(); - let value = db - .get("test-obj") - .get("nested-key") - .value(); + let value = db.get("test-obj").get("nested-key").value(); assert.strictEqual(value, undefined); }); }); - describe(".save()", function() { - it("should successfully save data back to database", function() { + describe(".save()", function () { + it("should successfully save data back to database", function () { // ensure an existing database isn't used for the test if (fs.existsSync("tempDB.stormdb")) deleteFile("tempDB.stormdb"); @@ -200,21 +181,18 @@ describe("StormDB", function() { }); }); - describe(".push()", function() { - it("should push data to an array", function() { + describe(".push()", function () { + it("should push data to an array", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); db.get("test-list").push("newData"); - let newDataIncluded = db - .get("test-list") - .value() - .includes("newData"); + let newDataIncluded = db.get("test-list").value().includes("newData"); assert(newDataIncluded); }); - it("should refuse to push data to a non-array", function() { + it("should refuse to push data to a non-array", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -227,30 +205,30 @@ describe("StormDB", function() { }); }); - describe(".map()", function() { - it("should map array", function() { + describe(".map()", function () { + it("should map array", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); - db.get("test-list").map(x => x ** 2); + db.get("test-list").map((x) => x ** 2); let updatedList = db.get("test-list").value(); assert.deepStrictEqual(updatedList, [1, 4, 9, 16, 25]); }); - it("should refuse to map a non-array", function() { + it("should refuse to map a non-array", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); const tryMap = () => { - db.get("test-string").map(x => x ** 2); + db.get("test-string").map((x) => x ** 2); }; // should refuse to map string and therefore should raise an exception assert.throws(tryMap, Error); }); - it("should refuse to map using non-function", function() { + it("should refuse to map using non-function", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -263,8 +241,8 @@ describe("StormDB", function() { }); }); - describe(".sort()", function() { - it("should sort array", function() { + describe(".sort()", function () { + it("should sort array", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -274,7 +252,7 @@ describe("StormDB", function() { assert.deepStrictEqual(updatedList, [5, 4, 3, 2, 1]); }); - it("should refuse to sort a non-array", function() { + it("should refuse to sort a non-array", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -286,7 +264,7 @@ describe("StormDB", function() { assert.throws(trySort, Error); }); - it("should refuse to sort not using function or undefined", function() { + it("should refuse to sort not using function or undefined", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -299,8 +277,8 @@ describe("StormDB", function() { }); }); - describe(".reduce()", function() { - it("should reduce array", function() { + describe(".reduce()", function () { + it("should reduce array", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -312,7 +290,7 @@ describe("StormDB", function() { assert.deepStrictEqual(updatedList, 15); }); - it("should refuse to reduce a non-array", function() { + it("should refuse to reduce a non-array", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -326,7 +304,7 @@ describe("StormDB", function() { assert.throws(tryReduce, Error); }); - it("should refuse to reduce using non-function", function() { + it("should refuse to reduce using non-function", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -339,18 +317,18 @@ describe("StormDB", function() { }); }); - describe(".filter()", function() { - it("should filter array", function() { + describe(".filter()", function () { + it("should filter array", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); - db.get("test-list").filter(i => i > 3); + db.get("test-list").filter((i) => i > 3); let updatedList = db.get("test-list").value(); assert.deepStrictEqual(updatedList, [4, 5]); }); - it("should refuse to filter using non-function", function() { + it("should refuse to filter using non-function", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -362,7 +340,7 @@ describe("StormDB", function() { assert.throws(tryFilter, Error); }); - it("should refuse to filter non-array", function() { + it("should refuse to filter non-array", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -375,8 +353,8 @@ describe("StormDB", function() { }); }); - describe(".default()", function() { - it("default value should be used in place of empty db", function() { + describe(".default()", function () { + it("default value should be used in place of empty db", function () { // load empty database const engine = new StormDB.localFileEngine(emptyDBPath); const db = new StormDB(engine); @@ -387,7 +365,7 @@ describe("StormDB", function() { assert.strictEqual(defaultedKey, "defaultString"); }); - it("default value shouldn't be used with non-empty db", function() { + it("default value shouldn't be used with non-empty db", function () { // load non-empty database const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); @@ -400,15 +378,12 @@ describe("StormDB", function() { }); }); - describe(".length()", function() { - it("should get lengths of db entries on .value()", function() { + describe(".length()", function () { + it("should get lengths of db entries on .value()", function () { const engine = new StormDB.localFileEngine(exampleDBPath); const db = new StormDB(engine); - let length = db - .get("test-list") - .length() - .value(); + let length = db.get("test-list").length().value(); assert.strictEqual(length, 5); }); }); diff --git a/tests/browserEngine.js b/tests/browserEngine.js index 517f4b2..40d3c16 100644 --- a/tests/browserEngine.js +++ b/tests/browserEngine.js @@ -15,8 +15,8 @@ class LocalStorage { } } -describe("Browser Engine", function() { - it("should successfully read data", function() { +describe("Browser Engine", function () { + it("should successfully read data", function () { // simulate browser local storage global.localStorage = new LocalStorage(); localStorage.setItem("db", '{ "key": "value" }'); @@ -28,7 +28,7 @@ describe("Browser Engine", function() { assert.strictEqual(value, "value"); }); - it("should create empty database if it doesn't exist yet", function() { + it("should create empty database if it doesn't exist yet", function () { global.localStorage = new LocalStorage(); const engine = new StormDB.browserEngine("db"); @@ -38,7 +38,7 @@ describe("Browser Engine", function() { assert.deepStrictEqual(value, {}); }); - it("should successfully write data", function() { + it("should successfully write data", function () { // simulate browser local storage global.localStorage = new LocalStorage(); localStorage.setItem("db", '{ "key": "value" }'); @@ -54,7 +54,7 @@ describe("Browser Engine", function() { assert.strictEqual(savedValue, "newValue"); }); - it("should throw error if trying to read incorrect data", function() { + it("should throw error if trying to read incorrect data", function () { // simulate browser local storage with corrupted database global.localStorage = new LocalStorage(); localStorage.setItem("db", "{"); @@ -68,13 +68,13 @@ describe("Browser Engine", function() { assert.throws(loadDB, Error); }); - it("should utilise custom deserialize function", function() { + it("should utilise custom deserialize function", function () { // simulate browser local storage global.localStorage = new LocalStorage(); localStorage.setItem("db", '{ "key": "value" }'); const engine = new StormDB.browserEngine("db", { - deserialize: () => "deserialized data" + deserialize: () => "deserialized data", }); const db = new StormDB(engine); @@ -82,13 +82,13 @@ describe("Browser Engine", function() { assert.strictEqual(value, "deserialized data"); }); - it("should utilise custom serialize function", function() { + it("should utilise custom serialize function", function () { // simulate browser local storage global.localStorage = new LocalStorage(); localStorage.setItem("db", '{ "key": "value" }'); const engine = new StormDB.browserEngine("db", { - serialize: () => "serialized data" + serialize: () => "serialized data", }); const db = new StormDB(engine); diff --git a/tests/localEngine.js b/tests/localEngine.js index 504643e..433cb8e 100644 --- a/tests/localEngine.js +++ b/tests/localEngine.js @@ -10,14 +10,14 @@ const corruptedDBPath = path.resolve( "./fixtures/corrupted-db.stormdb" ); -const deleteFile = function(fileName) { +const deleteFile = function (fileName) { fs.unlinkSync(fileName); }; -describe("Local Engine", function() { - it("should utilise custom deserialize function", function() { +describe("Local Engine", function () { + it("should utilise custom deserialize function", function () { const engine = new StormDB.localFileEngine(exampleDBPath, { - deserialize: () => "deserialized data" + deserialize: () => "deserialized data", }); const db = new StormDB(engine); @@ -25,12 +25,12 @@ describe("Local Engine", function() { assert.strictEqual(value, "deserialized data"); }); - it("should utilise custom serialize function", function() { + it("should utilise custom serialize function", function () { // ensure an existing database isn't used for the test if (fs.existsSync("tempDB.stormdb")) deleteFile("tempDB.stormdb"); const engine = new StormDB.localFileEngine("tempDB.stormdb", { - serialize: () => "serialized data" + serialize: () => "serialized data", }); const db = new StormDB(engine); @@ -42,7 +42,7 @@ describe("Local Engine", function() { deleteFile("tempDB.stormdb"); }); - it("should throw error if trying to read incorrect data", function() { + it("should throw error if trying to read incorrect data", function () { const engine = new StormDB.localFileEngine(corruptedDBPath); const loadDB = () => { @@ -52,7 +52,7 @@ describe("Local Engine", function() { assert.throws(loadDB, Error); }); - it("if database file empty, empty JSON object should be used", function() { + it("if database file empty, empty JSON object should be used", function () { const engine = new StormDB.localFileEngine(emptyDBPath); const db = new StormDB(engine); @@ -60,21 +60,21 @@ describe("Local Engine", function() { assert.deepStrictEqual(dbValue, {}); }); - describe("async engine option enabled", function() { - it(".save() function should return promise", function(done) { + describe("async engine option enabled", function () { + it(".save() function should return promise", function (done) { const engine = new StormDB.localFileEngine(exampleDBPath, { - async: true + async: true, }); const db = new StormDB(engine); - db.save().then(function() { + db.save().then(function () { done(); }); }); - it(".save() promise should reject is problem writing to file", function() { + it(".save() promise should reject is problem writing to file", function () { const engine = new StormDB.localFileEngine(exampleDBPath, { - async: true + async: true, }); const db = new StormDB(engine);