Prefix clusterio/lib imports with lib

Use lib* prefix for the modules impored from the lib package to help
distinguish them more from ordinary variables.  Fix up the naming
convention for files to be consistently lowercase_underscore for
JavaScript files.
This commit is contained in:
Hornwitser committed 2020-10-23 21:22:43 +02:00
1 parent fbbaf3dd3a
commit 24397b75cc
55 files changed
+692 -668

No files matched your search

+34 -34
View File
@@ -3,24 +3,24 @@ const assert = require("assert").strict;
const fs = require("fs-extra");
const path = require("path");
const database = require("@clusterio/lib/database");
const libDatabase = require("@clusterio/lib/database");
describe("lib/database", function() {
describe("mapToObject()", function() {
it("should throw on non-string key", function() {
assert.throws(
() => database.mapToObject(new Map([[1, 1]])),
() => libDatabase.mapToObject(new Map([[1, 1]])),
new Error("Expected all keys to be string but got number")
);
assert.throws(
() => database.mapToObject(new Map([[Symbol("test"), 1]])),
() => libDatabase.mapToObject(new Map([[Symbol("test"), 1]])),
new Error("Expected all keys to be string but got symbol")
);
});
it("should throw convert a map to an object", function() {
assert.deepEqual(
database.mapToObject(new Map([["a", 1], ["b", true]])),
libDatabase.mapToObject(new Map([["a", 1], ["b", true]])),
{ "a": 1, "b": true }
);
});
@@ -32,7 +32,7 @@ describe("lib/database", function() {
for (let type of badTypes) {
it(`should reject on ${type} JSON`, async function() {
await assert.rejects(
database.loadJsonAsMap(path.join("test", "file", "json", `${type}.json`)),
libDatabase.loadJsonAsMap(path.join("test", "file", "json", `${type}.json`)),
new Error(`Expected object but got ${type}`)
);
});
@@ -40,21 +40,21 @@ describe("lib/database", function() {
it("should work on empty object JSON", async function() {
assert.deepEqual(
await database.loadJsonAsMap(path.join("test", "file", "json", "object.json")),
await libDatabase.loadJsonAsMap(path.join("test", "file", "json", "object.json")),
new Map()
);
});
it("should work on object JSON", async function() {
assert.deepEqual(
await database.loadJsonAsMap(path.join("test", "file", "json", "load_map.json")),
await libDatabase.loadJsonAsMap(path.join("test", "file", "json", "load_map.json")),
new Map([["a", 1], ["b", true]])
);
});
it("should give an empty Map for non-existant file", async function() {
assert.deepEqual(
await database.loadJsonAsMap(path.join("test", "file", "json", "does-not-exist")),
await libDatabase.loadJsonAsMap(path.join("test", "file", "json", "does-not-exist")),
new Map()
);
});
@@ -75,7 +75,7 @@ describe("lib/database", function() {
}
await deleteTestFile();
await database.saveMapAsJson(
await libDatabase.saveMapAsJson(
testFile, new Map([["c", {}], ["d", "foo"]])
);
@@ -92,7 +92,7 @@ describe("lib/database", function() {
for (let type of badTypes) {
it(`should reject on ${type} JSON`, async function() {
await assert.rejects(
database.loadJsonArrayAsMap(path.join("test", "file", "json", `${type}.json`)),
libDatabase.loadJsonArrayAsMap(path.join("test", "file", "json", `${type}.json`)),
new Error(`Expected array but got ${type}`)
);
});
@@ -100,35 +100,35 @@ describe("lib/database", function() {
it("should reject on null element", async function() {
await assert.rejects(
database.loadJsonArrayAsMap(path.join("test", "file", "json", "array_null.json")),
libDatabase.loadJsonArrayAsMap(path.join("test", "file", "json", "array_null.json")),
new Error("Expected all elements to be objects")
);
});
it("should reject on element missing id", async function() {
await assert.rejects(
database.loadJsonArrayAsMap(path.join("test", "file", "json", "array_object.json")),
libDatabase.loadJsonArrayAsMap(path.join("test", "file", "json", "array_object.json")),
new Error("Expected all elements to have an id property")
);
});
it("should work on empty array JSON", async function() {
assert.deepEqual(
await database.loadJsonArrayAsMap(path.join("test", "file", "json", "array.json")),
await libDatabase.loadJsonArrayAsMap(path.join("test", "file", "json", "array.json")),
new Map()
);
});
it("should work on array JSON", async function() {
assert.deepEqual(
await database.loadJsonArrayAsMap(path.join("test", "file", "json", "load_array_map.json")),
await libDatabase.loadJsonArrayAsMap(path.join("test", "file", "json", "load_array_map.json")),
new Map([["a", { id: "a" }], ["b", { id: "b" }]])
);
});
it("should give an empty Map for non-existant file", async function() {
assert.deepEqual(
await database.loadJsonArrayAsMap(path.join("test", "file", "json", "does-not-exist")),
await libDatabase.loadJsonArrayAsMap(path.join("test", "file", "json", "does-not-exist")),
new Map()
);
});
@@ -149,7 +149,7 @@ describe("lib/database", function() {
}
await deleteTestFile();
await database.saveMapAsJsonArray(
await libDatabase.saveMapAsJsonArray(
testFile, new Map([["c", { id: "c" }], ["d", { id: "d" }]])
);
@@ -163,23 +163,23 @@ describe("lib/database", function() {
describe("class ItemDatabase", function() {
describe("constructor()", function() {
it("should create an empty database with no args", function() {
let items = new database.ItemDatabase();
let items = new libDatabase.ItemDatabase();
assert.deepEqual(items._items, new Map());
});
it("should restore the passed serialized database", function() {
let items = new database.ItemDatabase({ "a": 1, "b": 2 });
let items = new libDatabase.ItemDatabase({ "a": 1, "b": 2 });
assert.deepEqual(items._items, new Map([["a", 1], ["b", 2]]));
});
it("should throw on invalid serialized database", function() {
assert.throws(
() => new database.ItemDatabase({ "a": NaN }),
() => new libDatabase.ItemDatabase({ "a": NaN }),
new Error("count must be a number")
);
assert.throws(
() => new database.ItemDatabase({ "a": "a" }),
() => new libDatabase.ItemDatabase({ "a": "a" }),
new Error("count must be a number")
);
});
@@ -187,36 +187,36 @@ describe("lib/database", function() {
describe(".serialize()", function() {
it("should return a serialized database", function() {
let items = new database.ItemDatabase({ "a": 10 });
let items = new libDatabase.ItemDatabase({ "a": 10 });
assert.deepEqual(items.serialize(), { "a": 10 });
});
it("should remove zero count entries", function() {
let items = new database.ItemDatabase({ "a": 0 });
let items = new libDatabase.ItemDatabase({ "a": 0 });
assert.deepEqual(items.serialize(), {});
});
});
describe(".size", function() {
it("should give an approximate size of the database", function() {
let items = new database.ItemDatabase({ "a": 10 });
let items = new libDatabase.ItemDatabase({ "a": 10 });
assert.equal(items.size, 1);
});
});
describe(".getItemCount()", function() {
it("should return the count of the given item", function() {
let items = new database.ItemDatabase({ "a": 10 });
let items = new libDatabase.ItemDatabase({ "a": 10 });
assert.equal(items.getItemCount("a"), 10);
});
it("should return zero if item does not exist", function() {
let items = new database.ItemDatabase();
let items = new libDatabase.ItemDatabase();
assert.equal(items.getItemCount("b"), 0);
});
it("should throw on invalid name", function() {
let items = new database.ItemDatabase();
let items = new libDatabase.ItemDatabase();
assert.throws(
() => items.getItemCount(2),
new Error("name must be a string")
@@ -226,19 +226,19 @@ describe("lib/database", function() {
describe(".addItem()", function() {
it("should add a new item", function() {
let items = new database.ItemDatabase();
let items = new libDatabase.ItemDatabase();
items.addItem("a", 10);
assert.deepEqual(items._items, new Map([["a", 10]]));
});
it("should add an existing item", function() {
let items = new database.ItemDatabase({ "a": 10 });
let items = new libDatabase.ItemDatabase({ "a": 10 });
items.addItem("a", 10);
assert.deepEqual(items._items, new Map([["a", 20]]));
});
it("should throw on invalid name", function() {
let items = new database.ItemDatabase();
let items = new libDatabase.ItemDatabase();
assert.throws(
() => items.addItem(2, 10),
new Error("name must be a string")
@@ -246,7 +246,7 @@ describe("lib/database", function() {
});
it("should throw on invalid count", function() {
let items = new database.ItemDatabase();
let items = new libDatabase.ItemDatabase();
assert.throws(
() => items.addItem("a", NaN),
new Error("count must be a number")
@@ -260,19 +260,19 @@ describe("lib/database", function() {
describe(".removeItem()", function() {
it("should remove an existing item", function() {
let items = new database.ItemDatabase({ "a": 20 });
let items = new libDatabase.ItemDatabase({ "a": 20 });
items.removeItem("a", 10);
assert.deepEqual(items._items, new Map([["a", 10]]));
});
it("should turn a non-existing item negative", function() {
let items = new database.ItemDatabase();
let items = new libDatabase.ItemDatabase();
items.removeItem("a", 10);
assert.deepEqual(items._items, new Map([["a", -10]]));
});
it("should throw on invalid name", function() {
let items = new database.ItemDatabase();
let items = new libDatabase.ItemDatabase();
assert.throws(
() => items.removeItem(2, 10),
new Error("name must be a string")
@@ -280,7 +280,7 @@ describe("lib/database", function() {
});
it("should throw on invalid count", function() {
let items = new database.ItemDatabase();
let items = new libDatabase.ItemDatabase();
assert.throws(
() => items.removeItem("a", "b"),
new Error("count must be a number")