Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
acegoal07 committed Jul 16, 2024
1 parent f2126e9 commit ba12379
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 68 deletions.
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
"@acegoal07/file-tools": "^1.0.5"
},
"scripts": {
"build": "npm run buildTypes && npm run tscBuild && npm run copyFiles",
"build+publish": "npm run build && cd dist && npm publish",
"buildTypes": "npx -p typescript tsc src/index.js --declaration --allowJs --emitDeclarationOnly --outDir types",
"tscBuild": "npx tsc",
"copyFiles": "xcopy /s/y src dist && xcopy /y package.json dist && xcopy /y readme.md dist && xcopy /y lICENSE dist"
"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"
},
"publishConfig": {
"access": "public"
Expand Down
80 changes: 31 additions & 49 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const findValue = require("find-value"),
fs = require("fs"),
iterateObject = require("iterate-object"),
os = require('os'),
{ FileTools, JsonFileTools }= require("@acegoal07/file-tools");
{ FileTools, JsonFileTools } = require("@acegoal07/file-tools");
///////////////////////////////////////////////////////////////////////////
// Class /////////////////////////////////////////////////////////////////
/**
Expand All @@ -26,12 +26,12 @@ class JsonEditor {
* autosave?: Boolean
* }} options
*/
constructor (path, options) {
constructor(path, options) {
this.options = options = options || {}
options.stringify_width = options.stringify_width || 3
options.stringify_fn = options.stringify_fn || null
options.stringify_eol = options.stringify_eol || false
options.ignore_dots = options.ignore_dots || false;
options.stringify_width = options.stringify_width || 3
options.stringify_fn = options.stringify_fn || null
options.stringify_eol = options.stringify_eol || false
options.ignore_dots = options.ignore_dots || false;
this.path = path;
this.data = this.read();
}
Expand Down Expand Up @@ -75,9 +75,9 @@ class JsonEditor {
*/
write(content, callback) {
if (callback) {
fs.writeFile(this.path, content, callback);
fs.writeFile(this.path, content, callback);
} else {
fs.writeFileSync(this.path, content);
fs.writeFileSync(this.path, content);
}
return this;
}
Expand Down Expand Up @@ -151,15 +151,15 @@ class JsonEditor {
*/
read(callback) {
if (!callback) {
try {
return rJson(this.path);
} catch (error) {
return {};
}
try {
return rJson(this.path);
} catch (error) {
return {};
}
}
rJson(this.path, function (err, data) {
data = err ? {} : data;
callback(null, data);
data = err ? {} : data;
callback(null, data);
})
}
/**
Expand Down Expand Up @@ -190,20 +190,15 @@ class JsonEditor {
* @param {String} joiner The character to join the data with (default: `,`)
* @returns {String} The data string
*/
arrayToString(settings = {path: null, joiner: ","}) {
let data;
if (!settings.path) {
data = this.data;
} else {
data = this.get(settings.path);
}
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 (data.length === 1) {return data;}
if (data.length === 1) { return data; }
return data.join(settings.joiner);
}
/**
Expand Down Expand Up @@ -258,11 +253,11 @@ class JsonEditor {
}
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);
this.set(path, data);
return this;
}
/**
Expand All @@ -275,16 +270,11 @@ class JsonEditor {
if (!path) {
throw new Error("trigger ERROR: path is null");
}
let data = this.get(path);
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 (data) {
data = false;
} else {
data = true;
}
this.set(path, data);
this.set(path, !data);
return this;
}
/**
Expand Down Expand Up @@ -372,25 +362,17 @@ class JsonEditor {
* @returns {String[]} The keys
*/
getKeys(path = null) {
if (!path) {
const data = this.data;
if (typeof data != "object") {
throw new Error("getKeys ERROR: The data is not an object");
}
return Object.keys(data);
} else {
const data = this.get(path);
if (typeof data != "object") {
throw new Error("getKeys ERROR: The data is not an object");
}
return Object.keys(data);
const data = !path ? this.data : this.get(path);
if (typeof data != "object") {
throw new Error("getKeys ERROR: The data is not an object");
}
return Object.keys(data);
}
/**
* Deletes the file that's being edited
*/
delete() {
return FileTools().deleteFile(this.path)
return FileTools().deleteFile(this.path);
}
/**
* Copy's the data from one path to a another
Expand Down Expand Up @@ -432,7 +414,7 @@ class JsonEditor {
/**
* Renames the section of an object you specify
*
* @param {String} path The object path to the section you want to rename
* @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
*/
Expand Down Expand Up @@ -473,7 +455,7 @@ class JsonEditor {
* - `autosave` (Boolean): Save the file when setting some data in it
* @returns {JsonEditor} The `JsonEditor` instance
*/
exports.editFile = function(path, options) {
exports.editFile = function (path, options) {
if (!path) {
throw new Error("ERROR with editFile: Path is null");
}
Expand Down
46 changes: 32 additions & 14 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
///////////////////////////////////////////////////////////////////////////
const jsonEditor = require("../dist");

const file = jsonEditor.editFile("json/1.json", {autosave: true});

// specified get
if (file.get("check")) {
console.log("\x1B[32mTest 1 passed");
} else {
console.log("\x1B[31mTest 2 failed");
const file = jsonEditor.editFile("tests/json/1.json", { autosave: true });
const topBottomSize = 28;
const topBottom = `|${"-".repeat(topBottomSize + 2)}|--------|`;
// Log output ////////////////////////////////////////////////////////////
function logFormatter(testName, value = false) {
console.log(`| ${testName}${" ".repeat((topBottomSize - testName.length))} | ${value ? "\x1b[92mPASSED\x1b[0m" : "\x1b[91mFAILED\x1b[0m"} |`);
}
// non specified get
if (!file.get().check) {
console.log("\x1B[32mTest 2 passed");
} else {
console.log("\x1B[31mTest 2 failed");
}
// JSON Editor Tests /////////////////////////////////////////////////////
console.log(`${topBottom}
| JSON Editor | |
${topBottom}
| TEST | STATUS |
${topBottom}`);
// Specified get
try {
logFormatter("Specified get", file.get("check"));
} catch (error) {
logFormatter("Specified get", false);
}
// Get keys
try {
const keys = file.getKeys();
if (keys.length === 1 && keys[0] === "check") {
logFormatter("Get keys", true);
} else {
logFormatter("Get keys", false);
}
} catch (error) {
logFormatter("Get keys", false);
}
// Cleanup /////////////////////////////////////////////////////////////
console.log(topBottom);

0 comments on commit ba12379

Please sign in to comment.