mdast-util-find-and-replace
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -14,4 +14,4 @@ /** | ||
find: Find, | ||
replace?: Replace, | ||
options?: Options | ||
replace?: Replace | undefined, | ||
options?: Options | undefined | ||
) => Node) & | ||
@@ -21,3 +21,3 @@ (( | ||
schema: FindAndReplaceSchema | FindAndReplaceList, | ||
options?: Options | ||
options?: Options | undefined | ||
) => Node) | ||
@@ -33,7 +33,8 @@ /** | ||
} | ||
export type Text = import('mdast').Text | ||
export type Parent = import('mdast').Parent | ||
export type Root = import('mdast').Root | ||
export type Content = import('mdast').Content | ||
export type PhrasingContent = import('mdast').PhrasingContent | ||
export type Node = Parent['children'][number] | Root | ||
export type Text = import('mdast').Text | ||
export type Node = Content | Root | ||
export type Parent = Extract<Node, import('mdast').Parent> | ||
export type Test = import('unist-util-visit-parents').Test | ||
@@ -40,0 +41,0 @@ export type VisitorResult = import('unist-util-visit-parents').VisitorResult |
64
index.js
@@ -5,7 +5,8 @@ /** | ||
* | ||
* @typedef {import('mdast').Text} Text | ||
* @typedef {import('mdast').Parent} Parent | ||
* @typedef {import('mdast').Root} Root | ||
* @typedef {import('mdast').Content} Content | ||
* @typedef {import('mdast').PhrasingContent} PhrasingContent | ||
* @typedef {Parent['children'][number]|Root} Node | ||
* @typedef {import('mdast').Text} Text | ||
* @typedef {Content|Root} Node | ||
* @typedef {Extract<Node, import('mdast').Parent>} Parent | ||
* | ||
@@ -67,3 +68,3 @@ * @typedef {import('unist-util-visit-parents').Test} Test | ||
function (tree, find, replace, options) { | ||
/** @type {Options} */ | ||
/** @type {Options|undefined} */ | ||
let settings | ||
@@ -100,10 +101,7 @@ /** @type {FindAndReplaceSchema|FindAndReplaceList} */ | ||
let index = -1 | ||
/** @type {Parent} */ | ||
let parent | ||
/** @type {Parent} */ | ||
/** @type {Parent|undefined} */ | ||
let grandparent | ||
while (++index < parents.length) { | ||
// @ts-expect-error mdast vs. unist parent. | ||
parent = parents[index] | ||
const parent = /** @type {Parent} */ (parents[index]) | ||
@@ -124,3 +122,5 @@ if ( | ||
return handler(node, grandparent) | ||
if (grandparent) { | ||
return handler(node, grandparent) | ||
} | ||
} | ||
@@ -136,16 +136,13 @@ | ||
const replace = pairs[pairIndex][1] | ||
let start = 0 | ||
// @ts-expect-error: TS is wrong, some of these children can be text. | ||
let index = parent.children.indexOf(node) | ||
/** @type {Array.<PhrasingContent>} */ | ||
let nodes = [] | ||
let start = 0 | ||
let index = parent.children.indexOf(node) | ||
/** @type {number} */ | ||
/** @type {number|undefined} */ | ||
let position | ||
/** @type {RegExpMatchArray} */ | ||
let match | ||
/** @type {Array.<PhrasingContent>|PhrasingContent|string|false|undefined|null} */ | ||
let value | ||
find.lastIndex = 0 | ||
match = find.exec(node.value) | ||
let match = find.exec(node.value) | ||
@@ -155,6 +152,9 @@ while (match) { | ||
// @ts-expect-error this is perfectly fine, typescript. | ||
value = replace(...match, {index: match.index, input: match.input}) | ||
let value = replace(...match, { | ||
index: match.index, | ||
input: match.input | ||
}) | ||
if (typeof value === 'string' && value.length > 0) { | ||
value = {type: 'text', value} | ||
if (typeof value === 'string') { | ||
value = value.length > 0 ? {type: 'text', value} : undefined | ||
} | ||
@@ -170,4 +170,6 @@ | ||
if (value) { | ||
nodes = [].concat(nodes, value) | ||
if (Array.isArray(value)) { | ||
nodes.push(...value) | ||
} else if (value) { | ||
nodes.push(value) | ||
} | ||
@@ -206,7 +208,4 @@ | ||
function toPairs(schema) { | ||
let index = -1 | ||
/** @type {Pairs} */ | ||
const result = [] | ||
/** @type {string} */ | ||
let key | ||
@@ -218,2 +217,4 @@ if (typeof schema !== 'object') { | ||
if (Array.isArray(schema)) { | ||
let index = -1 | ||
while (++index < schema.length) { | ||
@@ -226,2 +227,5 @@ result.push([ | ||
} else { | ||
/** @type {string} */ | ||
let key | ||
for (key in schema) { | ||
@@ -250,9 +254,3 @@ if (own.call(schema, key)) { | ||
function toFunction(replace) { | ||
return typeof replace === 'function' ? replace : returner | ||
/** @type {ReplaceFunction} */ | ||
function returner() { | ||
// @ts-expect-error it’s a string. | ||
return replace | ||
} | ||
return typeof replace === 'function' ? replace : () => replace | ||
} |
{ | ||
"name": "mdast-util-find-and-replace", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "mdast utility to find and replace text in a tree", | ||
@@ -50,3 +50,3 @@ "license": "MIT", | ||
"unist-builder": "^3.0.0", | ||
"xo": "^0.39.0" | ||
"xo": "^0.42.0" | ||
}, | ||
@@ -53,0 +53,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17129
270