diff --git a/operations/add.js b/operations/add.js index c032944..1418668 100644 --- a/operations/add.js +++ b/operations/add.js @@ -1,12 +1,21 @@ const writeDataJson = require("../helpers/writeDataJson"); const getAll = require('./getAll'); +const log = require('../utils/log'); function add(arrayName, data) { const newData = getAll(); - newData[arrayName].push(data); - writeDataJson(JSON.stringify(newData)); + if (newData[arrayName] != undefined) { + var isArray = newData[arrayName].constructor === Array ? true : false + if (isArray) { + newData[arrayName].push(data) + } else { + log(`${arrayName} is not an array.`, "Error", "error"); + } + } else { + newData[arrayName] = [] + newData[arrayName].push(data); + } + writeDataJson(JSON.stringify(newData, null, 4)); } - -module.exports = add; - +module.exports = add; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 9d1931c..54fe658 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "dbcopycat", - "version": "0.2.1", + "version": "0.3.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "dbcopycat", - "version": "0.2.1", + "version": "0.3.2", "license": "ISC", "dependencies": { "chalk": "^4.1.2",