diff --git a/package.json b/package.json index 3d1fda4..b5f8fa9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@acegoal07/json-editor", - "version": "1.2.0", + "version": "1.2.1", "description": "A simple json file editor", "main": "index.js", "repository": { @@ -20,18 +20,19 @@ }, "homepage": "https://github.com/acegoal07/json-editor#readme", "dependencies": { + "@acegoal07/file-tools": "^1.0.9", "find-value": "^1.0.12", "iterate-object": "^1.3.4", "r-json": "^1.2.10", "set-value": "^4.1.0", - "w-json": "^1.3.10", - "@acegoal07/file-tools": "^1.0.5" + "w-json": "^1.3.10" }, "scripts": { "build": "npx -p typescript tsc src/index.js --declaration --allowJs --emitDeclarationOnly --outDir types && cls && npx tsc && cls && xcopy /s/y src dist && xcopy /y package.json dist && xcopy /y readme.md dist && xcopy /y lICENSE dist", "build+publish": "npm run build && cls && cd dist && cls && npm publish", "runTests": "npm i && node tests/tests.js && npx rimraf node_modules && npx rimraf package-lock.json", - "build+test": "npm run build && cls && npm test" + "build+test": "npm run build && cls && npm run runTests", + "cleanOldBuilds": "npx rimraf dist && npx rimraf types" }, "publishConfig": { "access": "public" diff --git a/src/index.js b/src/index.js index 2688231..0e8d572 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,3 @@ -/////////////////////////////////////////////////////////////////////////// // Dependencies ////////////////////////////////////////////////////////// const findValue = require("find-value"), setValue = require("set-value"), @@ -7,13 +6,9 @@ const findValue = require("find-value"), iterateObject = require("iterate-object"), os = require('os'), { FileTools, JsonFileTools } = require("@acegoal07/file-tools"); -/////////////////////////////////////////////////////////////////////////// // Class ///////////////////////////////////////////////////////////////// /** * The json editor - * - * @version `1.2.0` - * @author `acegoal07` */ class JsonEditor { /** @@ -37,7 +32,6 @@ class JsonEditor { } /** * Saves the file and any changes - * * @param {Function} callback An optional callback function which will turn the function into an asynchronous one * @returns {JsonEditor} The `JsonEditor` instance */ @@ -53,7 +47,6 @@ class JsonEditor { } /** * Get a value in a specific path - * * @param {String} path The object path * @returns {any} The object path value */ @@ -68,7 +61,6 @@ class JsonEditor { } /** * Write the JSON file - * * @param {String} content file content * @param {Function} callback An optional callback function which will turn the function into an asynchronous one * @returns {JsonEditor} The `JsonEditor` instance @@ -83,15 +75,12 @@ class JsonEditor { } /** * Copy's the data from a file into the file your editing - * * @param {String} path The path to the JSON file * @param {Boolean} layout The is used to add a layout out to the data being written to the file * @returns {JsonEditor} The `JsonEditor` instance */ writeCopy(path, layout = false) { - if (!path) { - throw new Error("writeCopy ERROR: path is null"); - } + if (!path) { throw new Error("writeCopy ERROR: path is null"); } if (layout) { this.write(JSON.stringify(JsonFileTools().readFile(path), null, 2)); } else { @@ -101,7 +90,6 @@ class JsonEditor { } /** * Empty the JSON file content - * * @param {Function} callback The callback function * @returns {JsonEditor} The `JsonEditor` instance */ @@ -113,39 +101,28 @@ class JsonEditor { } /** * Empty an arrays content - * * @param {String} path The object path * @returns {JsonEditor} The `JsonEditor` instance */ emptyArray(path) { - if (!path) { - throw new Error("emptyArray ERROR: path is null"); - } + if (!path) { throw new Error("emptyArray ERROR: path is null"); } const data = this.get(path); - if (!Array.isArray(data)) { - throw new Error("emptyArray ERROR: The data is not an array"); - } + if (!Array.isArray(data)) { throw new Error("emptyArray ERROR: The data is not an array"); } return this.set(path, []); } /** * Empty an objects content - * * @param {String} path The object path * @returns {JsonEditor} The `JsonEditor` instance */ emptyObject(path) { - if (!path) { - throw new Error("emptyObject ERROR: path is null"); - } + if (!path) { throw new Error("emptyObject ERROR: path is null"); } const data = this.get(path); - if (typeof data !== "object") { - throw new Error("emptyObject ERROR: The data is not an object"); - } + if (typeof data !== "object") { throw new Error("emptyObject ERROR: The data is not an object"); } return this.set(path, {}); } /** * Read the JSON file - * * @param {Function} callback An optional callback function which will turn the function into an asynchronous one * @returns {Object} The object parsed as object or an empty object by default */ @@ -164,7 +141,6 @@ class JsonEditor { } /** * Returns a object from the data path - * * @returns {Object} The data object */ toObject() { @@ -185,34 +161,26 @@ class JsonEditor { } /** * Returns a joined string from the array path - * * @param {String} path The object path * @param {String} joiner The character to join the data with (default: `,`) * @returns {String} The data string */ arrayToString(settings = { path: null, joiner: "," }) { const data = !settings.path ? this.data : this.get(settings.path); - if (!Array.isArray(data)) { - throw new Error("arrayToString ERROR: The data is not an array"); - } - if (!settings.joiner) { - throw new Error("arrayToString ERROR: joiner is null"); - } + if (!Array.isArray(data)) { throw new Error("arrayToString ERROR: The data is not an array"); } + if (!settings.joiner) { throw new Error("arrayToString ERROR: joiner is null"); } if (data.length === 1) { return data; } return data.join(settings.joiner); } /** * Set a value in a specific path - * * @param {String} path The object path * @param {any} value The value * @param {Object} options The options for set-value (applied only when {ignore_dots} file option is false) * @returns {JsonEditor} The `JsonEditor` instance */ set(path, value, options) { - if (!path) { - throw new Error("set ERROR: path is null"); - } + if (!path) { throw new Error("set ERROR: path is null"); } if (typeof path === "object") { iterateObject(path, (val, n) => { setValue(this.data, n, val, options); @@ -229,143 +197,105 @@ class JsonEditor { } /** * Remove a path from a JSON object - * * @param {String} path The object path * @returns {JsonEditor} The `JsonEditor` instance */ unset(path) { - if (!path) { - throw new Error("unset ERROR: path is null"); - } + if (!path) { throw new Error("unset ERROR: path is null"); } this.set(path, undefined); return this; } /** * Pushes the data to the top of the specified array - * * @param {String} path The object path * @param {any} value The value * @returns {JsonEditor} The `JsonEditor` instance */ push(path, value) { - if (!path) { - throw new Error("push ERROR: path is null"); - } + if (!path) { throw new Error("push ERROR: path is null"); } let data = this.get(path); data = (data === undefined) ? [] : data; - if (!Array.isArray(data)) { - throw new Error("push ERROR: The data is not an array"); - } + if (!Array.isArray(data)) { throw new Error("push ERROR: The data is not an array"); } data.push(value); this.set(path, data); return this; } /** * Switches a boolean data type between true and false - * * @param {String} path The object path * @returns {JsonEditor} The `JsonEditor` instance */ trigger(path) { - if (!path) { - throw new Error("trigger ERROR: path is null"); - } + if (!path) { throw new Error("trigger ERROR: path is null"); } const data = this.get(path); - if (typeof data != "boolean") { - throw new Error("trigger ERROR: The data path leads to data that is not boolean"); - } + if (typeof data != "boolean") { throw new Error("trigger ERROR: The data path leads to data that is not boolean"); } this.set(path, !data); return this; } /** * Pushes the data to the bottom of the specified array - * * @param {String} path The object path * @param {any} value The value * @returns {JsonEditor} The `JsonEditor` instance */ unshift(path, value) { - if (!path) { - throw new Error("unshift ERROR: path is null"); - } + if (!path) { throw new Error("unshift ERROR: path is null"); } let data = this.get(path); data = (data === undefined) ? [] : data; - if (!Array.isArray(data)) { - throw new Error("unshift ERROR: The data is not an array"); - } + if (!Array.isArray(data)) { throw new Error("unshift ERROR: The data is not an array"); } data.unshift(value); this.set(path, data); return this; } /** * Remove the last item from an array - * * @param {String} path The object path * @returns {JsonEditor} The `JsonEditor` instance */ popLast(path) { - if (!path) { - throw new Error("popLast ERROR: path is null"); - } + if (!path) { throw new Error("popLast ERROR: path is null"); } const data = this.get(path); - if (!Array.isArray(data)) { - throw new Error('popLast ERROR: The data is not an array'); - } + if (!Array.isArray(data)) { throw new Error('popLast ERROR: The data is not an array'); } data.pop(); this.set(path, data); return this; } /** * Removes a specific item from an array - * * @param {String} path The object path * @param {Number} position The position of the item * @returns {JsonEditor} The `JsonEditor` instance */ popTo(path, position) { - if (!path) { - throw new Error("popTo ERROR: path is null"); - } + if (!path) { throw new Error("popTo ERROR: path is null"); } const data = this.get(path); - if (!Array.isArray(data)) { - throw new Error('popTo ERROR: The data is not an array'); - } - if (!data[position]) { - throw new Error('popTo ERROR: The item does not exist on this array'); - } + if (!Array.isArray(data)) { throw new Error('popTo ERROR: The data is not an array'); } + if (!data[position]) { throw new Error('popTo ERROR: The item does not exist on this array'); } data.splice(position, position); this.set(path, data); return this; } /** * Remove the first item from an array - * * @param {String} path The object path * @returns {JsonEditor} The `JsonEditor` instance */ popFirst(path) { - if (!path) { - throw new Error("popFirst ERROR: path is null"); - } + if (!path) { throw new Error("popFirst ERROR: path is null"); } const data = this.get(path); - if (!Array.isArray(data)) { - throw new Error('popFirst ERROR: The data is not an array'); - } + if (!Array.isArray(data)) { throw new Error('popFirst ERROR: The data is not an array'); } data.shift(); this.set(path, data); return this; } /** * Gets the keys of data from the - * * @param {String} path An optional setting to get keys from a path * @returns {String[]} The keys */ getKeys(path = null) { const data = !path ? this.data : this.get(path); - if (typeof data != "object") { - throw new Error("getKeys ERROR: The data is not an object"); - } + if (typeof data != "object") { throw new Error("getKeys ERROR: The data is not an object"); } return Object.keys(data); } /** @@ -376,36 +306,26 @@ class JsonEditor { } /** * Copy's the data from one path to a another - * * @param {String} path The object path to the data you want to copy * @param {String} copyPath The object path to the place you wanna put the data * @returns {JsonEditor} The `JsonEditor` instance */ copy(path, copyPath) { - if (!path) { - throw new Error("copy ERROR: path is null"); - } - if (!copyPath) { - throw new Error("copy ERROR: copyPath is null"); - } + if (!path) { throw new Error("copy ERROR: path is null"); } + if (!copyPath) { throw new Error("copy ERROR: copyPath is null"); } const data = this.get(path); this.set(copyPath, data); return this; } /** * Moves the data to a new path and deletes the original - * * @param {String} oldPath The object path to the data you want to move * @param {String} newPath The object path to the place you wanna put the data * @returns {JsonEditor} The `JsonEditor` instance */ move(oldPath, newPath) { - if (!oldPath) { - throw new Error("move ERROR: oldPath is null"); - } - if (!newPath) { - throw new Error("move ERROR: newPath is null"); - } + if (!oldPath) { throw new Error("move ERROR: oldPath is null"); } + if (!newPath) { throw new Error("move ERROR: newPath is null"); } const data = this.get(oldPath); this.set(newPath, data); this.unset(oldPath); @@ -413,18 +333,13 @@ class JsonEditor { } /** * Renames the section of an object you specify - * * @param {String | RegExp} path The object path to the section you want to rename * @param {String} newName The new name you want to give the section * @return {JsonEditor} The `JsonEditor` instance */ rename(path, newName) { - if (!path) { - throw new Error("rename ERROR: path is null"); - } - if (!newName) { - throw new Error("rename ERROR: newName is null"); - } + if (!path) { throw new Error("rename ERROR: path is null"); } + if (!newName) { throw new Error("rename ERROR: newName is null"); } // Splits path info and creates new path const splitData = path.split("."); const newPath = path.replace(splitData.pop(), newName); @@ -435,11 +350,9 @@ class JsonEditor { return this; } } -/////////////////////////////////////////////////////////////////////////// // Function ////////////////////////////////////////////////////////////// /** * Creates an editor instance for the specified file - * * @param {String} path The path to the JSON file * @param {{ * stringify_width?: Number, @@ -455,12 +368,8 @@ class JsonEditor { * - `autosave` (Boolean): Save the file when setting some data in it * @returns {JsonEditor} The `JsonEditor` instance */ -exports.editFile = function (path, options) { - if (!path) { - throw new Error("ERROR with editFile: Path is null"); - } - if (!FileTools().fileExists(path)) { - throw new Error(`ERROR with editFile: File ${path} does not exists`); - } +exports.editFile = (path, options) => { + if (!path) { throw new Error("ERROR with editFile: Path is null"); } + if (!FileTools().fileExists(path)) { throw new Error(`ERROR with editFile: File ${path} does not exists`); } return new JsonEditor(path, options); } \ No newline at end of file diff --git a/tests/tests.js b/tests/tests.js index bf84ce2..ef963b0 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -13,16 +13,28 @@ console.log(`${topBottom} ${topBottom} | TEST | STATUS | ${topBottom}`); -// Specified get +// get try { - logFormatter("Specified get", file.get("check")); + logFormatter("get", file.get("check")); } catch (error) { - logFormatter("Specified get", false); + logFormatter("get", false); } -// Get keys +// write +try { + file.write(`{"check2":"passed"}`); + if (file.get("check2") === "passed") { + logFormatter("Write", true); + } else { + logFormatter("Write", false); + } +} catch (error) { + console.log(error); + logFormatter("Write", false); +} +// get keys try { const keys = file.getKeys(); - if (keys.length === 1 && keys[0] === "check") { + if (keys.length === 1 && keys[0] === "check2") { logFormatter("Get keys", true); } else { logFormatter("Get keys", false);