Skip to content

Commit 38eb39b

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

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed
 

‎lib/utils/format-search-stream.js

+6-5
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

+4-4
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: 'i@izs.me' },
261261
maintainers: [
262-
{ username: 'nlf', email: 'quitlahok@gmail.com' },
262+
{ username: '\x1B[34mnlf\x1B[39m', email: 'quitlahok@gmail.com' },
263263
{ username: 'ruyadorno', email: 'ruyadorno@hotmail.com' },
264264
{ username: 'darcyclarke', email: 'darcy@darcyclarke.me' },
265265
{ username: 'isaacs', email: 'i@izs.me' },

0 commit comments

Comments
 (0)
Please sign in to comment.