Files
2023-08-01 13:49:46 +02:00

60 lines
1.9 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
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) {
// we merge adjacent text nodes
if (Array.isArray(node)) {
// we create a new array rather than mutating the original
var arr = [];
var length = node.length;
var concatString = null;
var i = -1;
while (i++ < length) {
var child = node[i];
if (typeof child === "string" || typeof child === "number") {
if (concatString !== null) {
concatString += child;
} else {
concatString = child;
}
} else if ((typeof child === "undefined" ? "undefined" : _typeof(child)) === "object" && child !== null) {
if (concatString !== null) {
arr.push(concatString);
concatString = null;
}
arr.push(mergeAdacentJSONTextNodes(child));
}
}
if (concatString !== null) {
arr.push(concatString);
}
return arr;
} else {
for (var _key in node) {
var value = node[_key];
if ((typeof value === "undefined" ? "undefined" : _typeof(value)) === "object" && value !== null) {
node[_key] = mergeAdacentJSONTextNodes(value);
}
}
}
return node;
}
//# sourceMappingURL=json.js.map