Skip to content

Commit

Permalink
Refactor to move implementation to lib/
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 25, 2023
1 parent ae5234c commit 537d210
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 60 deletions.
62 changes: 2 additions & 60 deletions index.js
Original file line number Diff line number Diff line change
@@ -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<unknown>} values
* @param {boolean} includeImageAlt
* @returns {string}
*/
function all(values, includeImageAlt) {
/** @type {Array<string>} */
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'
63 changes: 63 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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<unknown>} values
* @param {boolean} includeImageAlt
* @returns {string}
*/
function all(values, includeImageAlt) {
/** @type {Array<string>} */
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')
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"main": "index.js",
"types": "index.d.ts",
"files": [
"lib/",
"index.d.ts",
"index.js"
],
Expand Down

0 comments on commit 537d210

Please sign in to comment.