summaryrefslogtreecommitdiff
path: root/deps/npm/lib/utils/parse-json.js
blob: 5c0b959a0d39eeacfec0b306a1d59c6debbecc87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'use strict'
var parseJSON = module.exports = function (content) {
  return JSON.parse(stripBOM(content))
}

parseJSON.noExceptions = function (content) {
  try {
    return parseJSON(content)
  } catch (ex) {
    return
  }
}

// from read-package-json
function stripBOM (content) {
  content = content.toString()
  // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
  // because the buffer-to-string conversion in `fs.readFileSync()`
  // translates it to FEFF, the UTF-16 BOM.
  if (content.charCodeAt(0) === 0xFEFF) {
    content = content.slice(1)
  }
  return content
}