Skip to content

Commit 38eb39b

Browse files
authored
fix: strip ansi characters from search results (#6514)
1 parent a63a6d8 commit 38eb39b

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

lib/utils/format-search-stream.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { Minipass } = require('minipass')
22
const columnify = require('columnify')
3+
const ansiTrim = require('../utils/ansi-trim.js')
34

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

142143
function normalizePackage (data, opts) {
143144
return {
144-
name: data.name,
145-
description: data.description,
146-
author: data.maintainers.map((m) => `=${m.username}`).join(' '),
145+
name: ansiTrim(data.name),
146+
description: ansiTrim(data.description),
147+
author: data.maintainers.map((m) => `=${ansiTrim(m.username)}`).join(' '),
147148
keywords: Array.isArray(data.keywords)
148-
? data.keywords.join(' ')
149+
? data.keywords.map(ansiTrim).join(' ')
149150
: typeof data.keywords === 'string'
150-
? data.keywords.replace(/[,\s]+/, ' ')
151+
? ansiTrim(data.keywords.replace(/[,\s]+/, ' '))
151152
: '',
152153
version: data.version,
153154
date: (data.date &&

test/fixtures/libnpmsearch-stream-result.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ module.exports = [
216216
version: '1.0.1',
217217
description: 'Retrieves a name:pathname Map for a given workspaces config',
218218
keywords: [
219-
'npm',
219+
'\x1B[33mnpm\x1B[39m',
220220
'npmcli',
221221
'libnpm',
222222
'cli',
@@ -240,10 +240,10 @@ module.exports = [
240240
],
241241
},
242242
{
243-
name: 'libnpmversion',
243+
name: '\x1B[31mlibnpmversion\x1B[39m',
244244
scope: 'unscoped',
245245
version: '1.0.7',
246-
description: "library to do the things that 'npm version' does",
246+
description: "library to do the things that '\x1B[32mnpm version\x1B[39m' does",
247247
date: '2020-11-04T00:21:41.069Z',
248248
links: {
249249
npm: 'https://www.npmjs.com/package/libnpmversion',
@@ -259,7 +259,7 @@ module.exports = [
259259
},
260260
publisher: { username: 'isaacs', email: '[email protected]' },
261261
maintainers: [
262-
{ username: 'nlf', email: '[email protected]' },
262+
{ username: '\x1B[34mnlf\x1B[39m', email: '[email protected]' },
263263
{ username: 'ruyadorno', email: '[email protected]' },
264264
{ username: 'darcyclarke', email: '[email protected]' },
265265
{ username: 'isaacs', email: '[email protected]' },

0 commit comments

Comments
 (0)