diff --git a/cjs/index.js b/cjs/index.js index a4833a5..0232813 100644 --- a/cjs/index.js +++ b/cjs/index.js @@ -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); } } diff --git a/es.js b/es.js index 3e4d187..69a0cd7 100644 --- a/es.js +++ b/es.js @@ -1 +1 @@ -self.Flatted=function(t){"use strict";const{parse:e,stringify:n}=JSON,{keys:r}=Object,o=String,s="string",c={},l="object",f=(t,e)=>e,i=t=>t instanceof o?o(t):t,a=(t,e)=>typeof e===s?new o(e):e,u=(t,e,n)=>{const r=o(e.push(n)-1);return t.set(n,r),r},p=(t,n)=>{const s=e(t,a).map(i),u=n||f;let p=s[0];if(typeof p===l&&p){const t=[],e=((t,e,n,s)=>f=>{for(let i=r(f),{length:a}=i,u=0;u{const o=e&&typeof e===l?(t,n)=>""===t||-1p(n(t)),t.parse=p,t.stringify=g,t.toJSON=t=>e(g(t)),t}({}); +self.Flatted=function(t){"use strict";const{parse:e,stringify:n}=JSON,{keys:r}=Object,o=String,s="string",c={},l="object",f=(t,e)=>e,i=t=>t instanceof o?o(t):t,a=(t,e)=>typeof e===s?new o(e):e,u=(t,e,n)=>{const r=o(e.push(n)-1);return t.set(n,r),r},p=(t,n)=>{const s=e(t,a).map(i),u=n||f;let p=s[0];if(typeof p===l&&p){const t=[],e=((t,e,n,s)=>f=>{for(let i=r(f),{length:a}=i,u=0;u{const o=e&&typeof e===l?(t,n)=>""===t||-1p(n(t)),t.parse=p,t.stringify=g,t.toJSON=t=>e(g(t)),t}({}); diff --git a/esm.js b/esm.js index 2563a41..92acf36 100644 --- a/esm.js +++ b/esm.js @@ -1 +1 @@ -const{parse:t,stringify:e}=JSON,{keys:n}=Object,o=String,r="string",s={},c="object",l=(t,e)=>e,f=t=>t instanceof o?o(t):t,i=(t,e)=>typeof e===r?new o(e):e,a=(t,e,n)=>{const r=o(e.push(n)-1);return t.set(n,r),r},u=(e,r)=>{const a=t(e,i).map(f),u=r||l;let p=a[0];if(typeof p===c&&p){const t=[],e=((t,e,r,l)=>f=>{for(let i=n(f),{length:a}=i,u=0;u{const s=n&&typeof n===c?(t,e)=>""===t||-1t(p(e)),g=t=>u(e(t));export{g as fromJSON,u as parse,p as stringify,h as toJSON}; +const{parse:t,stringify:e}=JSON,{keys:n}=Object,r=String,o="string",s={},c="object",l=(t,e)=>e,f=t=>t instanceof r?r(t):t,i=(t,e)=>typeof e===o?new r(e):e,a=(t,e,n)=>{const o=r(e.push(n)-1);return t.set(n,o),o},u=(e,o)=>{const a=t(e,i).map(f),u=o||l;let p=a[0];if(typeof p===c&&p){const t=[],e=((t,e,o,l)=>f=>{for(let i=n(f),{length:a}=i,u=0;u{const s=n&&typeof n===c?(t,e)=>""===t||-1t(p(e)),g=t=>u(e(t));export{g as fromJSON,u as parse,p as stringify,h as toJSON}; diff --git a/esm/index.js b/esm/index.js index eb2da2d..2f27115 100644 --- a/esm/index.js +++ b/esm/index.js @@ -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); } } diff --git a/index.js b/index.js index 9386548..aa52c6e 100644 --- a/index.js +++ b/index.js @@ -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({ diff --git a/min.js b/min.js index d3a97cb..520caec 100644 --- a/min.js +++ b/min.js @@ -1 +1 @@ -self.Flatted=function(n){"use strict";function t(n){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol&&n!==Symbol.prototype?"symbol":typeof n},t(n)}var r=JSON.parse,e=JSON.stringify,o=Object.keys,u=String,f="string",i={},c="object",a=function(n,t){return t},l=function(n){return n instanceof u?u(n):n},s=function(n,r){return t(r)===f?new u(r):r},y=function(n,t,r){var e=u(t.push(r)-1);return n.set(r,e),e},p=function(n,e){var f=r(n,s).map(l),y=e||a,p=f[0];if(t(p)===c&&p){var v=[],S=function(n,r,e,f){return function(a){for(var l=o(a),s=l.length,y=0;y