Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
fix: Strip underscore prefixed fields from file contents
Browse files Browse the repository at this point in the history
cc: @kumavis

Related-to: npm/arborist#242
Credit: @isaacs
Reviewed-by: @nlf
  • Loading branch information
isaacs committed Feb 22, 2021
1 parent 9f7049d commit ac771d8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions read-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ function parseJson (file, er, d, log, strict, cb) {

try {
data = safeJSON(stripBOM(d))
for (var key in data) {
if (/^_/.test(key)) {
delete data[key]
}
}
} catch (er) {
data = parseIndex(d)
if (!data) return cb(parseError(er, file))
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/underscores.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "underscore",
"version": "1.2.3",
"_lodash": true
}
17 changes: 17 additions & 0 deletions test/underscores.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const t = require('tap')
const rpj = require('../')
const { resolve } = require('path')
const fixture = resolve(__dirname, 'fixtures/underscores.json')
t.test('strip underscores', t => {
rpj(fixture, (er, data) => {
if (er)
throw er
t.strictSame(data, {
_id: 'underscore@1.2.3',
name: 'underscore',
version: '1.2.3',
readme: 'ERROR: No README data found!'
})
t.end()
})
})

0 comments on commit ac771d8

Please sign in to comment.