Use basicType helper in lib/database

Simplify the checking logic in lib/database by using the basicType
helper.
This commit is contained in:
Hornwitser committed 2020-04-03 20:51:28 +02:00
1 parent 65494a0a91
commit 3cc84d6400
1 file changed
+7 -21
+7 -21
View File
@@ -8,6 +8,8 @@
"use strict";
const fs = require("fs-extra");
const { basicType } = require("lib/helpers");
/**
* Converts a Map with only string keys to a JavaScript object.
@@ -46,16 +48,8 @@ async function loadJsonAsMap(filePath) {
let content = await fs.readFile(filePath);
let parsed = JSON.parse(content);
if (parsed instanceof Array) {
throw new Error(`Expected object but got array`);
}
if (parsed === null) {
throw new Error(`Expected object but got null`);
}
if (typeof parsed !== "object") {
throw new Error(`Expected object but got ${typeof parsed}`);
if (basicType(parsed) !== "object") {
throw new Error(`Expected object but got ${basicType(parsed)}`);
}
return new Map(Object.entries(parsed));
@@ -104,21 +98,13 @@ async function loadJsonArrayAsMap(filePath) {
let content = await fs.readFile(filePath);
let parsed = JSON.parse(content);
if (parsed === null) {
throw new Error(`Expected array but got null`);
}
if (typeof parsed !== "object") {
throw new Error(`Expected array but got ${typeof parsed}`);
}
if (!(parsed instanceof Array)) {
throw new Error(`Expected array but got object`);
if (basicType(parsed) !== "array") {
throw new Error(`Expected array but got ${basicType(parsed)}`);
}
let map = new Map();
for (let element of parsed) {
if (element === null || element instanceof Array || typeof element !== "object") {
if (basicType(element) !== "object") {
throw new Error(`Expected all elements to be objects`);
}