* @param {array} arr * @param {object} options * @param {Matcher} matcher - Path matcher instance * @returns object
(arr, options, matcher, readonlyMatcher)
| 46 | * @returns object |
| 47 | */ |
| 48 | function compress(arr, options, matcher, readonlyMatcher) { |
| 49 | let text; |
| 50 | const compressedObj = {}; //This is intended to be a plain object |
| 51 | for (let i = 0; i < arr.length; i++) { |
| 52 | const tagObj = arr[i]; |
| 53 | const property = propName(tagObj); |
| 54 | |
| 55 | // Push current property to matcher WITH RAW ATTRIBUTES (no prefix) |
| 56 | if (property !== undefined && property !== options.textNodeName) { |
| 57 | const rawAttrs = stripAttributePrefix( |
| 58 | tagObj[":@"] || {}, |
| 59 | options.attributeNamePrefix |
| 60 | ); |
| 61 | matcher.push(property, rawAttrs); |
| 62 | } |
| 63 | |
| 64 | if (property === options.textNodeName) { |
| 65 | if (text === undefined) text = tagObj[property]; |
| 66 | else text += "" + tagObj[property]; |
| 67 | } else if (property === undefined) { |
| 68 | continue; |
| 69 | } else if (tagObj[property]) { |
| 70 | |
| 71 | let val = compress(tagObj[property], options, matcher, readonlyMatcher); |
| 72 | const isLeaf = isLeafTag(val, options); |
| 73 | |
| 74 | if (Object.keys(val).length === 0 && options.alwaysCreateTextNode) { |
| 75 | val[options.textNodeName] = ""; |
| 76 | } |
| 77 | |
| 78 | if (tagObj[":@"]) { |
| 79 | assignAttributes(val, tagObj[":@"], readonlyMatcher, options); |
| 80 | } else if (Object.keys(val).length === 1 && val[options.textNodeName] !== undefined && !options.alwaysCreateTextNode) { |
| 81 | val = val[options.textNodeName]; |
| 82 | } else if (Object.keys(val).length === 0) { |
| 83 | if (options.alwaysCreateTextNode) val[options.textNodeName] = ""; |
| 84 | else val = ""; |
| 85 | } |
| 86 | |
| 87 | if (tagObj[METADATA_SYMBOL] !== undefined && typeof val === "object" && val !== null) { |
| 88 | val[METADATA_SYMBOL] = tagObj[METADATA_SYMBOL]; // copy over metadata |
| 89 | } |
| 90 | |
| 91 | |
| 92 | if (compressedObj[property] !== undefined && Object.prototype.hasOwnProperty.call(compressedObj, property)) { |
| 93 | if (!Array.isArray(compressedObj[property])) { |
| 94 | compressedObj[property] = [compressedObj[property]]; |
| 95 | } |
| 96 | compressedObj[property].push(val); |
| 97 | } else { |
| 98 | //TODO: if a node is not an array, then check if it should be an array |
| 99 | //also determine if it is a leaf node |
| 100 | |
| 101 | // Pass jPath string or readonlyMatcher based on options.jPath setting |
| 102 | const jPathOrMatcher = options.jPath ? readonlyMatcher.toString() : readonlyMatcher; |
| 103 | if (options.isArray(property, jPathOrMatcher, isLeaf)) { |
| 104 | compressedObj[property] = [val]; |
| 105 | } else { |
no test coverage detected