From 99e3a7ede1e37bb54b299cbafe3bf23eea22444d Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Thu, 13 Oct 2011 14:37:03 +0200 Subject: Added previous examples, .ditz subdirectory and testing data as submodules. --- json_diff.js | 158 ++++++++++++++++++++++++++++------------------------------- 1 file changed, 74 insertions(+), 84 deletions(-) (limited to 'json_diff.js') diff --git a/json_diff.js b/json_diff.js index 27973aa..07068cb 100644 --- a/json_diff.js +++ b/json_diff.js @@ -1,86 +1,77 @@ -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -// IN THE SOFTWARE. -var jsonBoxA, jsonBoxB; - -var HashStore = { - load : function(callback) { - if (window.location.hash) { - try { - var hashObject = JSON.parse(decodeURIComponent(window.location.hash.slice(1))); - callback && callback(hashObject.d); - return; - } catch (e) { - console.log() - } - } - callback && callback(null); - }, - sync : function(object) { - var hashObject = { d : object }; - window.location.hash = "#" + encodeURIComponent(JSON.stringify(hashObject)); - } -}; - -function init() { - document.addEventListener("click", clickHandler, false); +#!/usr/bin/rhino +/* +This is for now just reimplementation of tlrobinson's json-diff (http://tlrobinson.net/projects/javascript-fun/jsondiff/). - jsonBoxA = document.getElementById("jsonA"); - jsonBoxB = document.getElementById("jsonB"); - - HashStore.load(function(data) { - if (data) { - jsonBoxA.value = data.a; - jsonBoxB.value = data.b; - } - }); - - startCompare(); -} - -function swapBoxes() { - var tmp = jsonBoxA.value; - jsonBoxA.value = jsonBoxB.value; - jsonBoxB.value = tmp; -} +However, later I would like to incorporate algorightm from +https://github.com/cmsd2/xdiff (which is an implementation in Ruby of +http://pages.cs.wisc.edu/~yuanwang/xdiff.html) + */ -function clearBoxes() { - jsonBoxA.value = ""; - jsonBoxB.value = ""; -} - -function startCompare() { - var aValue = jsonBoxA.value; - var bValue = jsonBoxB.value; - - var objA, objB; - try { - objA = JSON.parse(aValue); - jsonBoxA.style.backgroundColor = ""; - } catch(e) { - jsonBoxA.style.backgroundColor = "rgba(255,0,0,0.5)"; - } - try { - objB = JSON.parse(bValue); - jsonBoxB.style.backgroundColor = ""; - } catch(e) { - jsonBoxB.style.backgroundColor = "rgba(255,0,0,0.5)"; - } - - HashStore.sync({ - a : aValue, - b : bValue - }); - - results = document.getElementById("results"); - removeAllChildren(results); +var jsonBoxA, jsonBoxB; - compareTree(objA, objB, "root", results); -} +//var HashStore = { +// load : function(callback) { +// if (window.location.hash) { +// try { +// var hashObject = JSON.parse(decodeURIComponent(window.location.hash.slice(1))); +// callback && callback(hashObject.d); +// return; +// } catch (e) { +// console.log() +// } +// } +// callback && callback(null); +// }, +// sync : function(object) { +// var hashObject = { d : object }; +// window.location.hash = "#" + encodeURIComponent(JSON.stringify(hashObject)); +// } +//}; + +//function init() { +// document.addEventListener("click", clickHandler, false); + +// jsonBoxA = document.getElementById("jsonA"); +// jsonBoxB = document.getElementById("jsonB"); + +// HashStore.load(function(data) { +// if (data) { +// jsonBoxA.value = data.a; +// jsonBoxB.value = data.b; +// } +// }); + +// startCompare(); +//} + +//function startCompare() { +// var aValue = jsonBoxA.value; +// var bValue = jsonBoxB.value; + +// var objA, objB; +// try { +// objA = JSON.parse(aValue); +// jsonBoxA.style.backgroundColor = ""; +// } catch(e) { +// jsonBoxA.style.backgroundColor = "rgba(255,0,0,0.5)"; +// } +// try { +// objB = JSON.parse(bValue); +// jsonBoxB.style.backgroundColor = ""; +// } catch(e) { +// jsonBoxB.style.backgroundColor = "rgba(255,0,0,0.5)"; +// } + +// HashStore.sync({ +// a : aValue, +// b : bValue +// }); + +// results = document.getElementById("results"); +// removeAllChildren(results); + +// compareTree(objA, objB, "root", results); +//} function compareTree(a, b, name, results) { var typeA = typeofReal(a); @@ -161,11 +152,8 @@ function removeAllChildren(node) { } } -function isArray(value) { - return value && typeof value === "object" && value.constructor === Array; -} function typeofReal(value) { - return isArray(value) ? "array" : typeof value; + return Array.isArray(value) ? "array" : typeof value; } function clickHandler(e) { @@ -177,3 +165,5 @@ function clickHandler(e) { e.target.setAttribute("closed", "yes"); } } + +/* vim: set ts=2 et sw=2 tw=80: */ -- cgit