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

Commit

Permalink
Objects returned are mutable, so clone from the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
iarna committed Jun 27, 2017
1 parent 1149124 commit 52ad4c5
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions read-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ function stripBOM (content) {
return content
}

function jsonClone (obj) {
if (obj == null) {
return obj
} else if (typeof obj === 'object') {
var newobj = {}
for (var kk in obj) {
newobj[kk] = jsonClone[kk]
}
} else if (typeof obj === 'array') {
var newarr = new Array(obj.length)
for (var ii in obj) {
newarr[ii] = obj[ii]
}
} else {
return obj
}
}

function parseJson (file, er, d, log, strict, cb) {
if (er && er.code === 'ENOENT') {
return fs.stat(path.dirname(file), function (err, stat) {
Expand All @@ -74,7 +92,7 @@ function parseJson (file, er, d, log, strict, cb) {
}
if (er) return cb(er)

if (cache[d]) return cb(null, cache[d])
if (cache[d]) return cb(null, jsonClone(cache[d]))

var data

Expand All @@ -91,7 +109,7 @@ function parseJson (file, er, d, log, strict, cb) {
function extrasCached (file, d, data, log, strict, cb) {
extras(file, data, log, strict, (err, data) => {
if (!err) {
cache[d] = data
cache[d] = jsonClone(data)
}
cb(err, data)
})
Expand Down

0 comments on commit 52ad4c5

Please sign in to comment.