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

Commit

Permalink
fix: use jsonClone to fix cache layer (#174)
Browse files Browse the repository at this point in the history
* fix: fix jsonClone to fix cache layer

* test: add test for caching layer
  • Loading branch information
antongolub authored May 3, 2023
1 parent be327aa commit 7b516f1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/read-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ function jsonClone (obj) {
} else if (Array.isArray(obj)) {
var newarr = new Array(obj.length)
for (var ii in obj) {
newarr[ii] = obj[ii]
newarr[ii] = jsonClone(obj[ii])
}
return newarr
} else if (typeof obj === 'object') {
var newobj = {}
for (var kk in obj) {
newobj[kk] = jsonClone[kk]
newobj[kk] = jsonClone(obj[kk])
}
return newobj
} else {
return obj
}
Expand Down Expand Up @@ -115,7 +117,6 @@ function parseJson (file, er, d, log, strict, cb) {
return cb(parseError(jsonErr, file))
}
}

extrasCached(file, d, data, log, strict, cb)
}

Expand Down Expand Up @@ -505,7 +506,7 @@ function final (file, data, log, strict, cb) {
}

function fillTypes (file, data, cb) {
var index = data.main ? data.main : 'index.js'
var index = data.main || 'index.js'

if (typeof index !== 'string') {
return cb(new TypeError('The "main" attribute must be of type string.'))
Expand Down
53 changes: 53 additions & 0 deletions test/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var path = require('path')

var tap = require('tap')

var readJson = require('../')
var file = path.resolve(__dirname, 'fixtures/deepnested.json')

tap.test('cache test', function (t) {
var count = 0
var spy = function (file_, data_, then) {
count++
then()
}

readJson.extraSet.push(spy)
t.teardown(function () {
readJson.extraSet.pop()
})

readJson(file, function (er, data) {
if (er) {
throw er
}
var expectedProp = [
{
prop: {
prop: [
{
prop: 'prop',
},
],
},
},
]
t.ok(data)
t.equal(data.name, 'deep-nested')
t.equal(data.version, '1.2.3')
t.equal(data._id, data.name + '@' + data.version)
t.same(data.prop, expectedProp)

data.prop = null

readJson(file, function (er2, data2) {
if (er2) {
throw er2
}
t.same(data2.prop, expectedProp)
t.not(data2.prop, data.prop)
t.equal(count, 1)
t.end()
})
})
})
15 changes: 15 additions & 0 deletions test/fixtures/deepnested.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "deep-nested",
"version": "1.2.3",
"prop": [
{
"prop": {
"prop": [
{
"prop": "prop"
}
]
}
}
]
}

0 comments on commit 7b516f1

Please sign in to comment.