From 38eb39b8068ab4e3b0a544234ac5df804469e3d8 Mon Sep 17 00:00:00 2001 From: Gar Date: Wed, 31 May 2023 10:38:43 -0700 Subject: [PATCH] fix: strip ansi characters from search results (#6514) --- lib/utils/format-search-stream.js | 11 ++++++----- test/fixtures/libnpmsearch-stream-result.js | 8 ++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/utils/format-search-stream.js b/lib/utils/format-search-stream.js index 21929cce7d056..762dea90859d1 100644 --- a/lib/utils/format-search-stream.js +++ b/lib/utils/format-search-stream.js @@ -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: // @@ -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 && diff --git a/test/fixtures/libnpmsearch-stream-result.js b/test/fixtures/libnpmsearch-stream-result.js index b2ec20f59efeb..1ec8b7b113d6b 100644 --- a/test/fixtures/libnpmsearch-stream-result.js +++ b/test/fixtures/libnpmsearch-stream-result.js @@ -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', @@ -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', @@ -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' },