Skip to content

Commit

Permalink
fix: strip ansi characters from search results (#6514)
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar authored May 31, 2023
1 parent a63a6d8 commit 38eb39b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
11 changes: 6 additions & 5 deletions lib/utils/format-search-stream.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Minipass } = require('minipass')
const columnify = require('columnify')
const ansiTrim = require('../utils/ansi-trim.js')

// This module consumes package data in the following format:
//
Expand Down Expand Up @@ -141,13 +142,13 @@ function highlightSearchTerms (str, terms) {

function normalizePackage (data, opts) {
return {
name: data.name,
description: data.description,
author: data.maintainers.map((m) => `=${m.username}`).join(' '),
name: ansiTrim(data.name),
description: ansiTrim(data.description),
author: data.maintainers.map((m) => `=${ansiTrim(m.username)}`).join(' '),
keywords: Array.isArray(data.keywords)
? data.keywords.join(' ')
? data.keywords.map(ansiTrim).join(' ')
: typeof data.keywords === 'string'
? data.keywords.replace(/[,\s]+/, ' ')
? ansiTrim(data.keywords.replace(/[,\s]+/, ' '))
: '',
version: data.version,
date: (data.date &&
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/libnpmsearch-stream-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ module.exports = [
version: '1.0.1',
description: 'Retrieves a name:pathname Map for a given workspaces config',
keywords: [
'npm',
'\x1B[33mnpm\x1B[39m',
'npmcli',
'libnpm',
'cli',
Expand All @@ -240,10 +240,10 @@ module.exports = [
],
},
{
name: 'libnpmversion',
name: '\x1B[31mlibnpmversion\x1B[39m',
scope: 'unscoped',
version: '1.0.7',
description: "library to do the things that 'npm version' does",
description: "library to do the things that '\x1B[32mnpm version\x1B[39m' does",
date: '2020-11-04T00:21:41.069Z',
links: {
npm: 'https://www.npmjs.com/package/libnpmversion',
Expand All @@ -259,7 +259,7 @@ module.exports = [
},
publisher: { username: 'isaacs', email: 'i@izs.me' },
maintainers: [
{ username: 'nlf', email: 'quitlahok@gmail.com' },
{ username: '\x1B[34mnlf\x1B[39m', email: 'quitlahok@gmail.com' },
{ username: 'ruyadorno', email: 'ruyadorno@hotmail.com' },
{ username: 'darcyclarke', email: 'darcy@darcyclarke.me' },
{ username: 'isaacs', email: 'i@izs.me' },
Expand Down

0 comments on commit 38eb39b

Please sign in to comment.