Created
May 24, 2014 19:09
-
-
Save Natinux/4e366059cd9e778ffd91 to your computer and use it in GitHub Desktop.
ex2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE HTML> | |
| <html> | |
| <head> | |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
| <title>Jasmine Spec Runner v2.0.0</title> | |
| <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.0.0/jasmine_favicon.png"> | |
| <link rel="stylesheet" type="text/css" href="lib/jasmine-2.0.0/jasmine.css"> | |
| <script type="text/javascript" src="lib/jasmine-2.0.0/jasmine.js"></script> | |
| <script type="text/javascript" src="lib/jasmine-2.0.0/jasmine-html.js"></script> | |
| <script type="text/javascript" src="lib/jasmine-2.0.0/boot.js"></script> | |
| <script type="text/javascript"> | |
| /* | |
| This function should receive an object deep N levels, with the following rules: | |
| - All levels can only be either JS primitives, or other literal objects. | |
| - Objects can contain references to each other | |
| This function should return a serialized version of this object as a string. | |
| The only limitation of this function is that it is not allowed to modify original object | |
| */ | |
| function serialize(obj) { | |
| seen = []; | |
| paths = {}; | |
| return runner(obj, '$'); | |
| } | |
| var seen = []; // since the objects are sealed | |
| var paths = {}; // track objects paths. | |
| /* | |
| * Run through the object to return a json representation | |
| * */ | |
| function runner(obj, pos) { | |
| if (typeof obj === 'object') { | |
| var name = getObjName(obj); | |
| if (seen.indexOf(name) > -1) { | |
| return JSON.stringify({ | |
| '#ref': getObjPath(obj) | |
| }); | |
| } else { | |
| // a new object | |
| seen.push(name); | |
| paths[name] = pos; | |
| } | |
| var output = []; | |
| for (var p in obj) { | |
| if (obj.hasOwnProperty(p)) { | |
| var val = runner(obj[p], pos + '.' + p); | |
| if (typeof val === 'string') { | |
| output.push('"' + p + '"' + ":" + val); | |
| } | |
| } | |
| } | |
| return "{" + output.join(",") + "}"; | |
| } | |
| return JSON.stringify(obj); | |
| } | |
| /* | |
| * export object name | |
| * */ | |
| function getObjName(obj) { | |
| for (var i in obj) { | |
| if (obj.hasOwnProperty(i) && typeof obj[i] === 'string' && i == obj[i]) { | |
| return i; | |
| } | |
| } | |
| } | |
| /* | |
| * Get object path by the tracker | |
| * */ | |
| function getObjPath(obj) { | |
| var name = getObjName(obj); | |
| return (paths || window.paths)[name]; | |
| } | |
| /* | |
| * Iterate over an object searching for references. | |
| * */ | |
| function referenceSpotter(obj, root) { | |
| root = root || obj; | |
| for (var property in obj) { | |
| if (obj.hasOwnProperty(property)) { | |
| if (typeof obj[property] == "object") { | |
| var isReference = false; | |
| for (var cp in obj[property]) { | |
| if (obj[property].hasOwnProperty(cp) && cp === '#ref') { | |
| isReference = true; | |
| obj[property] = getByPath(obj[property][cp], root); | |
| } | |
| } | |
| !isReference && referenceSpotter(obj[property], root); | |
| } | |
| } | |
| } | |
| return root; | |
| } | |
| /* | |
| * Get an object by his JSON path from object | |
| * */ | |
| function getByPath(path, src) { | |
| var pos = path.split('.'); | |
| if (pos.length == 1 && pos == '$') { | |
| // root | |
| return src; | |
| } else if (pos.length > 1 && pos.shift() == '$') { | |
| var tmp = src; | |
| while (pos.length) { | |
| tmp = tmp[pos.shift()]; | |
| } | |
| return tmp; | |
| } | |
| // no path | |
| } | |
| /* | |
| This function should be able to parese out the serialized string from the serialize function, | |
| and use it to reconstruct the object: | |
| 1. result object should have all the original object's properties and sub-properties | |
| 2. result object should have all it's internal ojbect relationsheeps maintained | |
| 3. result object (and it's decendants) must be completely new objects | |
| 4. result object must be identical - no extra properties can be added to it | |
| */ | |
| function deserialize(str) { | |
| return referenceSpotter(JSON.parse(str)); | |
| } | |
| </script> | |
| <!-- include source files here... --> | |
| <script type="text/javascript" src="spec/DeserializeSpec.js"></script> | |
| </head> | |
| <body> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment