1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| LOCALSTORAGE = (function() { "use strict"; var ignore = [Boolean, Date, Number, RegExp, String]; function primitive(item) { if (typeof item === 'object') { if (item === null) { return true; } for (var i = 0; i < ignore.length; i++) { if (item instanceof ignore[i]) { return true; } } return false; } else { return true; } } function infant(value) { return Array.isArray(value)? [] : {}; } function decycleIntoForest(object, replacer) { if (typeof replacer!== 'function') { replacer = function(x) { return x; }; } object = replacer(object); if (primitive(object)) return object; var objects = [object]; var forest = [infant(object)]; var bucket = new WeakMap(); bucket.set(object, 0); function addToBucket(obj) { var result = objects.length; objects.push(obj); bucket.set(obj, result); return result; } function isInBucket(obj) { return bucket.has(obj); } function processNode(source, target) { Object.keys(source).forEach(function(key) { var value = replacer(source[key]); if (primitive(value)) { target[key] = { value: value }; } else { var ptr; if (isInBucket(value)) { ptr = bucket.get(value); } else { ptr = addToBucket(value); var newTree = infant(value); forest.push(newTree); processNode(value, newTree); } target[key] = { pointer: ptr }; } }); } processNode(object, forest[0]); return forest; } function deForestIntoCycle(forest) { var objects = []; var objectRequested = []; var todo = []; function processTree(idx) { if (idx in objects) return objects[idx]; if (objectRequested[idx]) return null; objectRequested[idx] = true; var tree = forest[idx]; var node = Array.isArray(tree)? [] : {}; for (var key in tree) { var o = tree[key]; if ('pointer' in o) { var ptr = o.pointer; var value = processTree(ptr); if (value === null) { todo.push({ node: node, key: key, idx: ptr }); } else { node[key] = value; } } else { if ('value' in o) { node[key] = o.value; } else { throw new Error('unexpected'); } } } objects[idx] = node; return node; } var result = processTree(0); for (var i = 0; i < todo.length; i++) { var item = todo[i]; item.node[item.key] = objects[item.idx]; } return result; } var console = { log: function(x) { var the = document.getElementById('the'); the.textContent = the.textContent + '\n' + x; }, delimiter: function() { var the = document.getElementById('the'); the.textContent = the.textContent + '\n*******************************************'; } } function logCyclicObjectToConsole(root) { var cycleFree = decycleIntoForest(root); var shown = cycleFree.map(function(tree, idx) { return false; }); var indentIncrement = 4; function showItem(nodeSlot, indent, label) { var leadingSpaces =' '.repeat(indent); var leadingSpacesPlus =' '.repeat(indent + indentIncrement); if (shown[nodeSlot]) { console.log(leadingSpaces + label + ' ... see above (object #' + nodeSlot + ')'); } else { console.log(leadingSpaces + label + ' object#' + nodeSlot); var tree = cycleFree[nodeSlot]; shown[nodeSlot] = true; Object.keys(tree).forEach(function(key) { var entry = tree[key]; if ('value' in entry) { console.log(leadingSpacesPlus + key + ": " + entry.value); } else { if ('pointer' in entry) { showItem(entry.pointer, indent + indentIncrement, key); } } }); } } console.delimiter(); showItem(0, 0, 'root'); } function stringify(obj) { return JSON.stringify(decycleIntoForest(obj)); } function parse(str) { return deForestIntoCycle(JSON.parse(str)); } var CYCLICJSON = { decycleIntoForest: decycleIntoForest, deForestIntoCycle: deForestIntoCycle, logCyclicObjectToConsole: logCyclicObjectToConsole, stringify: stringify, parse: parse } function setObject(name, object) { var str = stringify(object); localStorage.setItem(name, str); } function getObject(name) { var str = localStorage.getItem(name); if (str === null) return null; return parse(str); } return { CYCLICJSON: CYCLICJSON, setObject: setObject, getObject: getObject } })();
|