const fs = require("fs"); const path = require("path"); class AutoIndentErr extends Error { constructor(message) { super(message); } } function ForceObjectType(type, object, paramName) { if (typeof object === type) { } else { throw new AutoIndentErr("The parameter (" + paramName + ") you returned wasn't a " + type + "!") } } function CheckSettings(Settings) { if (typeof Settings === "object") { } else { throw new AutoIndentErr("The settings you returned are not an Object!") } if (Settings.IndentLength == undefined) { throw new AutoIndentErr("IndentLength in your Settings is not defined!") } else { if (typeof Settings.IndentLength == "number") { } else { throw new AutoIndentErr("IndentLength is not a number!") } } if (Settings.ReplaceIndents == undefined) { throw new AutoIndentErr("ReplaceIndents in your Settings is not defined!") } else { if (typeof Settings.ReplaceIndents == "boolean") { } else { throw new AutoIndentErr("ReplaceIndents is not a boolean!") } } } /** * Logs the indented string to the console. (LEGACY) * * @since 2.2.0 * * @param {number} indent - The amonut of indent (in spaces) * @param {string} string - The string you want to indent * @returns {void} - Doesn't return string, * * @example * lstdout(2, "Hello, World!"); // Output: " Hello, World!" */ const lstdout = (indent = 2, string) => { ForceObjectType("number", indent, "indent"); ForceObjectType("string", string, "string"); const msg = `${" ".repeat(indent)}${string}` console.log(msg) } /** * Returns indented string. (LEGACY) * * @since 2.2.0 * * @param {number} indent - The amonut of indent (in spaces) * @param {string} string - The string you want to indent * @returns {string} - The indented string. * * @example * lireturn(2, "Hello, World!"); // Output: " Hello, World!" */ const lireturn = (indent = 2, string) => { ForceObjectType("number", indent, "indent"); ForceObjectType("string", string, "string"); const msg = `${" ".repeat(indent)}${string}` return msg; } /** * Logs the indented string to the console. * * @since 1.0.0 * * @param {string} string - The string to log. * @param {Object} Settings - Your settings object. * @returns {void} - Doesn't need to return anything. * * @example * const Settings = loadSettings() // { "IndentLength": 2, "ReplaceIndents": false} * * stdout("Hello, World!"); // Output: " Hello, World!" */ const stdout = (string, Settings = { "IndentLength": 2, "ReplaceIndents": false }) => { // ^^^ Recommended settings in the Settings parameter. ForceObjectType("string", string, "string") CheckSettings(Settings); const msg = `${" ".repeat(Settings.IndentLength)}${string}`; console.log(msg); }; /** * Returns the indented string instead of logging. * * @since 1.0.0 * * @param {string} string - The string to return. * @param {object} Settings - Your settings * @returns {string} - The indented string. * * @example * const Settings = loadSettings() // { "IndentLength": 2, "ReplaceIndents": false} * * console.log(ireturn("Hello, World!", Settings)); // Output: " Hello, World!" */ const ireturn = (string = "", Settings = { "IndentLength": 2, "ReplaceIndents": false }) => { // ^^^ Recommended settings in the parameter CheckSettings(Settings); const msg = `${" ".repeat(Settings.IndentLength)}${string}`; return msg; }; /** * Loads your indent-settings.json and validates them. If they are not valid, returns error. * * @since 2.0.0 * * @param {string} Path - The path to your indent-settings.json. Only required if it's not in the root of your project. * @returns {Object} - The loadSettings() function returns your settings in JSON, this is how different Settings objects are cross compatible. * @example * const Settings = loadSettings() // Easy as that! */ const loadSettings = (Path = "indent-settings.json") => { const UserProjectPath = path.dirname(require.main.filename).replace(/\\/g, "/"); const UserSettings = JSON.parse(fs.readFileSync(UserProjectPath + "/" + Path)); return UserSettings; }; module.exports = { stdout, lstdout, ireturn, lireturn, loadSettings };