Skip to content

Commit

Permalink
Correctly print differences in nested objects (github#35497)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe authored Mar 14, 2023
1 parent 5efe2a0 commit ee25e81
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions script/update-internal-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,20 @@ function printObjectDifference(objFrom, objTo, rawContent, parentKey = '') {
for (const [key, value] of Object.entries(objFrom)) {
const combinedKey = `${parentKey}.${key}`
if (Array.isArray(value) && !equalArray(value, objTo[key])) {
console.log(`In frontmatter key: ${chalk.bold(combinedKey)}`)
value.forEach((entry, i) => {
if (entry !== objTo[key][i]) {
console.log(chalk.red(`- ${entry}`))
console.log(chalk.green(`+ ${objTo[key][i]}`))
const needle = new RegExp(`- ${entry}\\b`)
const index = rawContent.split(/\n/g).findIndex((line) => needle.test(line))
console.log(' ', chalk.dim(`line ${(index && index + 1) || 'unknown'}`))
console.log('')
// If it was an array of objects, we need to go deeper!
if (isObject(entry)) {
printObjectDifference(entry, objTo[key][i], rawContent, combinedKey)
} else {
console.log(`In frontmatter key: ${chalk.bold(combinedKey)}`)
if (entry !== objTo[key][i]) {
console.log(chalk.red(`- ${entry}`))
console.log(chalk.green(`+ ${objTo[key][i]}`))
const needle = new RegExp(`- ${entry}\\b`)
const index = rawContent.split(/\n/g).findIndex((line) => needle.test(line))
console.log(' ', chalk.dim(`line ${(index && index + 1) || 'unknown'}`))
console.log('')
}
}
})
} else if (typeof value === 'object' && value !== null) {
Expand Down

0 comments on commit ee25e81

Please sign in to comment.