prepack
Version:
Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.
71 lines (66 loc) • 2.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.mergeAdacentJSONTextNodes = mergeAdacentJSONTextNodes;
// this will mutate the original JSON object
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
function mergeAdacentJSONTextNodes(node, includeFunctionBodies, visitedNodes) {
if (visitedNodes === undefined) {
visitedNodes = new Set();
}
if (visitedNodes.has(node)) {
return "[Circular]";
}
visitedNodes.add(node);
// we merge adjacent text nodes
if (Array.isArray(node)) {
// we create a new array rather than mutating the original
let arr = [];
let length = node.length;
let concatString = null;
let i = -1;
while (i++ < length) {
let child = node[i];
if (typeof child === "string" || typeof child === "number") {
if (concatString !== null) {
concatString += child;
} else {
concatString = child;
}
} else if (typeof child === "object" && child !== null) {
if (concatString !== null) {
arr.push(concatString);
concatString = null;
}
arr.push(mergeAdacentJSONTextNodes(child, includeFunctionBodies, visitedNodes));
}
}
if (concatString !== null) {
arr.push(concatString);
}
return arr;
} else {
for (let key in node) {
let value = node[key];
if (typeof value === "function") {
if (!includeFunctionBodies) {
node[key] = value.toString();
} else {
delete node[key];
}
} else if (typeof value === "object" && value !== null) {
node[key] = mergeAdacentJSONTextNodes(value, includeFunctionBodies, visitedNodes);
}
}
}
return node;
}
//# sourceMappingURL=json.js.map