From 537d210ab08dbb1d0853d653e9d44951657e1f08 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 25 Jan 2023 10:59:07 +0100 Subject: [PATCH] Refactor to move implementation to `lib/` --- index.js | 62 ++------------------------------------------------- lib/index.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 66 insertions(+), 60 deletions(-) create mode 100644 lib/index.js diff --git a/index.js b/index.js index 7f71f7b..8674f30 100644 --- a/index.js +++ b/index.js @@ -1,63 +1,5 @@ /** - * @typedef {import('mdast').Root|import('mdast').Content} Node - * - * @typedef Options - * Configuration (optional). - * @property {boolean} [includeImageAlt=true] - * Whether to use `alt` for `image`s. + * @typedef {import('./lib/index.js').Options} Options */ -/** - * Get the text content of a node or list of nodes. - * Prefers the node’s plain-text fields, otherwise serializes its children, - * and if the given value is an array, serialize the nodes in it. - * - * @param {unknown} value - * @param {Options} [options] - * @returns {string} - */ -export function toString(value, options = {}) { - const {includeImageAlt = true} = options - return one(value, includeImageAlt) -} - -/** - * @param {unknown} value - * @param {boolean} includeImageAlt - * @returns {string} - */ -function one(value, includeImageAlt) { - return ( - (node(value) && - (('value' in value && value.value) || - (includeImageAlt && 'alt' in value && value.alt) || - ('children' in value && all(value.children, includeImageAlt)))) || - (Array.isArray(value) && all(value, includeImageAlt)) || - '' - ) -} - -/** - * @param {Array} values - * @param {boolean} includeImageAlt - * @returns {string} - */ -function all(values, includeImageAlt) { - /** @type {Array} */ - const result = [] - let index = -1 - - while (++index < values.length) { - result[index] = one(values[index], includeImageAlt) - } - - return result.join('') -} - -/** - * @param {unknown} value - * @returns {value is Node} - */ -function node(value) { - return Boolean(value && typeof value === 'object') -} +export {toString} from './lib/index.js' diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..7f71f7b --- /dev/null +++ b/lib/index.js @@ -0,0 +1,63 @@ +/** + * @typedef {import('mdast').Root|import('mdast').Content} Node + * + * @typedef Options + * Configuration (optional). + * @property {boolean} [includeImageAlt=true] + * Whether to use `alt` for `image`s. + */ + +/** + * Get the text content of a node or list of nodes. + * Prefers the node’s plain-text fields, otherwise serializes its children, + * and if the given value is an array, serialize the nodes in it. + * + * @param {unknown} value + * @param {Options} [options] + * @returns {string} + */ +export function toString(value, options = {}) { + const {includeImageAlt = true} = options + return one(value, includeImageAlt) +} + +/** + * @param {unknown} value + * @param {boolean} includeImageAlt + * @returns {string} + */ +function one(value, includeImageAlt) { + return ( + (node(value) && + (('value' in value && value.value) || + (includeImageAlt && 'alt' in value && value.alt) || + ('children' in value && all(value.children, includeImageAlt)))) || + (Array.isArray(value) && all(value, includeImageAlt)) || + '' + ) +} + +/** + * @param {Array} values + * @param {boolean} includeImageAlt + * @returns {string} + */ +function all(values, includeImageAlt) { + /** @type {Array} */ + const result = [] + let index = -1 + + while (++index < values.length) { + result[index] = one(values[index], includeImageAlt) + } + + return result.join('') +} + +/** + * @param {unknown} value + * @returns {value is Node} + */ +function node(value) { + return Boolean(value && typeof value === 'object') +} diff --git a/package.json b/package.json index 28c1381..2f46508 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "main": "index.js", "types": "index.d.ts", "files": [ + "lib/", "index.d.ts", "index.js" ],