diff --git a/package.json b/package.json index 3dfc8462..b0fe3e4b 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "test": "npm run test-unit", "test-unit": "mocha --recursive \"./{,!(node_modules)/**/}*.spec.js\" || echo .", "start": "echo Run node client.js start [instance] to start slaves && node master.js", - "cover": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- lib/*.js -R spec" + "cover": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- \"./{,!(node_modules)/**/}*.spec.js\" -R spec" }, "keywords": [ "factorio" diff --git a/sharedPlugins/inventoryImports/isJson.js b/sharedPlugins/inventoryImports/isJson.js index 6bd89302..597564df 100644 --- a/sharedPlugins/inventoryImports/isJson.js +++ b/sharedPlugins/inventoryImports/isJson.js @@ -1,9 +1,11 @@ module.exports = function(x){ + if(x === false || x === true || x === null) return true; // === false is valid JSON, so is true and null var isNotJson = false try{ z = JSON.parse(x); } catch (e) { isNotJson = true; } + return !isNotJson; } \ No newline at end of file diff --git a/sharedPlugins/inventoryImports/isJson.spec.js b/sharedPlugins/inventoryImports/isJson.spec.js new file mode 100644 index 00000000..4f76fe48 --- /dev/null +++ b/sharedPlugins/inventoryImports/isJson.spec.js @@ -0,0 +1,20 @@ +const isJson = require("./isJson"); +const assert = require("assert"); + +describe("isJson.js()", function(){ + it("checks if a thing is valid JSON", function(){ + assert(isJson("string") === false); + assert(isJson("{js_object_notation='very false'}") === false); + assert(isJson('{ "name":"John", "age":31, "city":"New York" }') === true); + }); + it("recognizes that true and false are both valid JSON", function(){ + assert(isJson(true) === true); + assert(isJson(false) === true); + }); + it("Handles null as valid JSON (after spec)", function(){ + assert(isJson(null) === true); + }); + it("Recognizes that JSON doesn't allow trailing commas (,)", function(){ + assert(isJson('["a", "b", ]') === false); + }); +});