prepack
Version:
Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.
77 lines (65 loc) • 2.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.mergeAdjacentJSONTextNodes = mergeAdjacentJSONTextNodes;
/**
* 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.
*/
// this will mutate the original JSON object
function mergeAdjacentJSONTextNodes(node, removeFunctions, visitedNodes) {
if (visitedNodes === undefined) {
visitedNodes = new Set();
}
if (visitedNodes.has(node)) {
return node;
}
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 && concatString !== "") {
arr.push(concatString);
concatString = null;
}
arr.push(mergeAdjacentJSONTextNodes(child, removeFunctions, visitedNodes));
}
}
if (concatString !== null && concatString !== "") {
arr.push(concatString);
}
return arr;
} else {
for (let key in node) {
let value = node[key];
if (typeof value === "function") {
if (removeFunctions) {
delete node[key];
} else {
node[key] = "function";
}
} else if (typeof value === "object" && value !== null) {
node[key] = mergeAdjacentJSONTextNodes(value, removeFunctions, visitedNodes);
}
}
}
return node;
}
//# sourceMappingURL=json.js.map