Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,18 @@ const parse = (text, reviver) => {
const revive = resolver(input, lazy, new Set, $);
value = revive(value);

// Revive all containers first so that each reviver receives nested
// values that are already revived.
let i = 0;
while (i < lazy.length) {
// it could be a lazy.shift() but that's costly
const {o, k, r} = lazy[i++];
o[k] = $.call(o, k, revive(r));
while (i < lazy.length)
revive(lazy[i++].r);

// A container is always queued before the containers it owns, so a
// reverse walk fills nested references in before their parent is
// passed to the reviver, without recursion.
while (i--) {
const {o, k, r} = lazy[i];
o[k] = $.call(o, k, r);
}
}

Expand Down
2 changes: 1 addition & 1 deletion es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,18 @@ export const parse = (text, reviver) => {
const revive = resolver(input, lazy, new Set, $);
value = revive(value);

// Revive all containers first so that each reviver receives nested
// values that are already revived.
let i = 0;
while (i < lazy.length) {
// it could be a lazy.shift() but that's costly
const {o, k, r} = lazy[i++];
o[k] = $.call(o, k, revive(r));
while (i < lazy.length)
revive(lazy[i++].r);

// A container is always queued before the containers it owns, so a
// reverse walk fills nested references in before their parent is
// passed to the reviver, without recursion.
while (i--) {
const {o, k, r} = lazy[i];
o[k] = $.call(o, k, r);
}
}

Expand Down
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,21 @@ self.Flatted = (function (exports) {
var lazy = [];
var revive = resolver(input, lazy, new Set(), $);
value = revive(value);

// Revive all containers first so that each reviver receives nested
// values that are already revived.
var i = 0;
while (i < lazy.length) {
// it could be a lazy.shift() but that's costly
var _lazy$i = lazy[i++],
while (i < lazy.length) revive(lazy[i++].r);

// A container is always queued before the containers it owns, so a
// reverse walk fills nested references in before their parent is
// passed to the reviver, without recursion.
while (i--) {
var _lazy$i = lazy[i],
o = _lazy$i.o,
k = _lazy$i.k,
r = _lazy$i.r;
o[k] = $.call(o, k, revive(r));
o[k] = $.call(o, k, r);
}
}
return $.call({
Expand Down
2 changes: 1 addition & 1 deletion min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,36 @@ console.assert(Flatted.parse(
);
}());

(function () {
var o = Flatted.parse(
Flatted.stringify({shape: {type: 'Point', pos: {x: 1, y: 2}}}),
function (key, value) {
return value && value.type === 'Point' ?
'Point(' + value.pos.x + ',' + value.pos.y + ')' :
value;
}
);
console.assert(o.shape === 'Point(1,2)', 'reviver sees nested values');

o = Flatted.parse(
Flatted.stringify({err: {message: 'boom', stack: 'Error: boom', response: {status: 500}}}),
function (key, value) {
if (value && value.message && value.stack) {
var error = new Error(value.message);
Object.keys(value).forEach(function (key) {
error[key] = value[key];
});
return error;
}
return value;
}
);
console.assert(
o.err instanceof Error && o.err.response.status === 500,
'reviver can replace a container after its values are revived'
);
}());

(function () {
var o = {};
o['~'] = o;
Expand Down