refactor: move `String.toString` -> `Objects.toString`

pull/11/head
David Ordás 2022-09-20 17:55:32 +02:00
parent d9dacf2dd1
commit f4b24bdefe
No known key found for this signature in database
GPG Key ID: 6FD751229911593E
4 changed files with 23 additions and 18 deletions

View File

@ -3,7 +3,7 @@
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const remark = require("remark"); const remark = require("remark");
const { Strings } = require("./lib/functions"); const { Objects, Strings } = require("./lib/functions");
const languages = require("./languages"); const languages = require("./languages");
const commandLineArgs = require("command-line-args"); const commandLineArgs = require("command-line-args");
@ -81,7 +81,7 @@ function getLinkTextFromLinkNodes(children) {
// visit nodes in depth // visit nodes in depth
const walk = (children, depth) => { const walk = (children, depth) => {
// not AST, maybe plain text // 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 // AST children array nodes
return children.reduce((text, node, index) => { return children.reduce((text, node, index) => {
if (!node || !node.type) return text; // not AST, maybe plain text if (!node || !node.type) return text; // not AST, maybe plain text

19
lib/functions/Objects.js Normal file
View File

@ -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,
};

View File

@ -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. * Wraps a string between other that acts as token.
* @param {string} s - the text to wrap * @param {string} s - the text to wrap
@ -56,6 +41,5 @@ function wrap(s, token = "") {
module.exports = { module.exports = {
stripParens, stripParens,
templater, templater,
toString,
wrap, wrap,
}; };

View File

@ -1,5 +1,7 @@
const Objects = require("./Objects");
const Strings = require("./Strings"); const Strings = require("./Strings");
module.exports = { module.exports = {
Objects,
Strings, Strings,
}; };