diff --git a/index.js b/index.js index 96a44dc..25ec128 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ const fs = require("fs"); const path = require("path"); const remark = require("remark"); -const { Strings } = require("./lib/functions"); +const { Objects, Strings } = require("./lib/functions"); const languages = require("./languages"); const commandLineArgs = require("command-line-args"); @@ -81,7 +81,7 @@ function getLinkTextFromLinkNodes(children) { // visit nodes in depth const walk = (children, depth) => { // not AST, maybe plain text - if (!Array.isArray(children)) return Strings.toString(children); + if (!Array.isArray(children)) return Objects.toString(children); // AST children array nodes return children.reduce((text, node, index) => { if (!node || !node.type) return text; // not AST, maybe plain text diff --git a/lib/functions/Objects.js b/lib/functions/Objects.js new file mode 100644 index 0000000..9c1fa12 --- /dev/null +++ b/lib/functions/Objects.js @@ -0,0 +1,19 @@ +/** + * To string + * @param {any} o - the object to get it text representation + * @returns {string} the `o` as string + */ +function toString(o) { + // null or undefined + if (o === null || o === void 0) return o; + // is string + if (typeof o === "string") return o; + // has a toString function in their prototype + if (typeof o.toString === "function") return o.toString(); + // as string in the latest intent + return String(o); +} + +module.exports = { + toString, +}; diff --git a/lib/functions/Strings.js b/lib/functions/Strings.js index 71fddd0..a660b8b 100644 --- a/lib/functions/Strings.js +++ b/lib/functions/Strings.js @@ -26,21 +26,6 @@ function templater(template, context = {}) { ); } -/** - * To string - * @param {any} o - the object to get it text representation - * @returns {string} the `o` as string - */ -function toString(o) { - // null or undefined - if (o === null || o === void 0) return o; - if (typeof o === "string") return o; - // has a toString function in their prototype - if (typeof o.toString === "function") return o.toString(); - // as string in the latest intent - return String(o); -} - /** * Wraps a string between other that acts as token. * @param {string} s - the text to wrap @@ -56,6 +41,5 @@ function wrap(s, token = "") { module.exports = { stripParens, templater, - toString, wrap, }; diff --git a/lib/functions/index.js b/lib/functions/index.js index 293aedc..8664237 100644 --- a/lib/functions/index.js +++ b/lib/functions/index.js @@ -1,5 +1,7 @@ +const Objects = require("./Objects"); const Strings = require("./Strings"); module.exports = { + Objects, Strings, };