all files / cycle/ index.js

96.15% Statements 75/78
91.67% Branches 66/72
100% Functions 15/15
96.15% Lines 75/78
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244                                156×         21×   21×         21× 21×       16× 16×       20×     53×                                                   10×     10×   10×       53×       53×         32× 32× 11×           21×   21×   43×           21×     21×         50× 50× 16× 12× 12× 12×     34×                     50×   50× 21× 21×   49× 45× 40×     21×   29×                                                         10×         10×       10×   10×   10×                         21×   40× 40×     11×   29× 11×         10×            
/*
    Copyright, Feb 2016, AnyWhichWay
    
    MIT License (since some CDNs and users must have some type of license and MIT in pretty un-restrictive)
    
    Substantive portions based on:
    
    cycle.js
    2013-02-19 douglas crockford
 
    Public Domain.
    
 */
(function() {
	"use strict";
 
	var cycler = {};
	
	function isArray(value) {
		return Array.isArray(value) || value instanceof Array;
	}
 
	// AnyWhichWay, Feb 2016, isolates code for tagging objects with $class
	// during decycle. See resurrect for the converse.
	function augment(context, original, decycled) {
		var classname = original.constructor.name;
		// look in context if classname not available
		if (!classname || classname === "") { 
			Object.keys(context).some(function(name) {
				if (context[name] === original.constructor) {
					classname = name;
					return true;
				}
			});
		}
		// add the $class info to array or object
		Eif (classname && classname.length > 0) { 
			if (isArray(decycled)) {
				decycled.push({
					$class : classname
				});
				return decycled;
			}
			decycled.$class = classname;
			return decycled;
		}
	}
	
	function getContext(context) {
		return (context ? context : (typeof (window) !== "undefined" ? window : global));
	}
	
	function isDecyclable(value) {
		return typeof(value)==="object" && value
		&& !(value instanceof Boolean) && !(value instanceof Date)
		&& !(value instanceof Number) && !(value instanceof RegExp)
		&& !(value instanceof String);
	}
 
	cycler.decycle = function decycle(object, context) {
 
		// Make a deep copy of an object or array, assuring that there is at
		// most one instance of each object or array in the resulting structure. The
		// duplicate references (which might be forming cycles) are replaced
		// with an object of the form
		// {$ref: PATH}
		// where the PATH is a JSONPath string that locates the first occurance.
		// So,
		// var a = [];
		// a[0] = a;
		// return JSON.stringify(JSON.decycle(a));
		// produces the string '[{"$ref":"$"}]'.
		// Add a $class property to objects or element to arrays so that they
		// can be restored as their original kind.
 
		// JSONPath is used to locate the unique object. $ indicates the top
		// level of the object or array. [NUMBER] or [STRING] indicates a 
		// child member or property.
 
		// AnyWhichWay, Feb 2016, establish context
		context = getContext(context);
 
		// AnyWhichWay, Feb 201, replaced objects and paths arrays with Map
		var objects = new Map(); 
 
		return (function derez(value, path) {
 
			// The derez recurses through the object, producing the deep copy.
 
			var pathfound, // AnyWhichWay added Feb 2016
			nu = (isArray(value) ? [] : {}); 
 
			// AnyWhichWay, Feb 2016, converted test to function call
			if (isDecyclable(value)) { 
 
				// If the value is an object or array, look to see if we have
				// encountered it. If so, return a $ref/path object.
				// AnyWhichWay, Feb 2016 replaced array loops with Map get
				pathfound = objects.get(value); 
				if (pathfound) {
					return {
						$ref : pathfound
					};
				}
				// Otherwise, accumulate the unique value and its path.
				// AnyWhichWay, Feb 2016 replace array objects and paths with Map
				objects.set(value, path); 
 
				Object.keys(value).forEach(
						function(key) {
							nu[key] = derez(value[key], path
									+ "["
									+ (isArray(nu) ? key : JSON
											.stringify(key)) + "]");
						});
				// AnyWhichWay, Feb 2016 augment with $class
				return augment(context, value, nu); 
			}
			// otherwise, just return value
			return value;
		}(object, "$"));
	};
 
	function getConstructor(context, item) {
		// process objects and return possibly modified item
		var value;
		if (item && item.$class) {
			if (typeof (context[item.$class]) === "function") {
				value = context[item.$class];
				delete item.$class;
				return value;
			}
			return Object; // don't delete item.$class since it will be useful for debugging scope issues
		}
		if (isArray(item)
				&& item[item.length - 1].$class
				&& Object.keys(item[item.length - 1]).length === 1) {
			Eif (typeof (context[item[item.length - 1].$class]) === "function") {
				value = context[item[item.length - 1].$class];
				delete item[item.length - 1].$class;
				return value;
			}
			return Object;
		}
	}
	// AnyWhichWay, Feb 2016, isolates code for resurrecting objects as their
	// original type see augment for inverse
	// AnyWhichWay, Feb 2016, isolates code for resurrecting objects as their
	// original type see augment for inverse. Optimized May, 2016
	function resurrect(context, item) {
		var cons = getConstructor(context, item);
		// process objects and return possibly modified item
		if (cons) {
			var properties = {constructor: {enumerable:false,configurable:true,writable:true,value:cons}};
			Object.keys(item).forEach(function(key, i) {
				// hide class spec if it exists
				if(key==="$class") {
					properties[key] = {configurable:true,writable:true,value:item[key]};
				} else if (i !== item.length - 1 || !isArray(item)) {
					properties[key] = {configurable:true,writable:true,enumerable:true,value:item[key]};
				}
			});
			return Object.create(cons.prototype,properties);
		}
		return item;
	}
 
	cycler.retrocycle = function retrocycle($, context) {
 
		// Restore an object that was reduced by decycle. Members whose values
		// are objects of the form {$ref: PATH}
		// are replaced with references to the value found by the PATH. This
		// will restore cycles. The object will be mutated.
 
		// AnyWhichWay, Feb 2016
		// Objects containing $class member are converted to the class specified
		// Arrays with last member {$class: <some kind>} are converted to the
		// specified class of array
 
		// A dynamic Function is used to locate the values described by a PATH.
		// Root object is kept in a $ variable. A regular expression is used to
		// assure that the PATH is well formed. The regexp contain nested
		// * quantifiers. That has been known to have extremely bad performance
		// problems on some browsers for very long strings. A PATH should be
		// reasonably short. A PATH is allowed to belong to a very restricted
		// subset of Goessner's JSONPath.
 
		// So,
		// var s = '[{"$ref":"$"}]';
		// return JSON.retrocycle(JSON.parse(s));
		// produces an array containing a single element which is the array
		// itself.
		
		// AnyWhichWay, May 2016, just return if not object
		Iif(typeof($)!=="object" || !$) {
			return $;
		}
 
		// AnyWhichWay, Feb 2016, establish the context
		context = getContext(context);
 
		// AnyWhichWay, Feb 2016 do any required top-level conversion from
		// POJO's to $classs
		$ = resurrect(context, $);
 
		var px = /^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/;
 
		(function rez(value) {
 
			// Modified by AnyWhichWay, Feb 2016
			// The rez function walks recursively through the object looking for
			// $ref and $class properties or array values. When it finds a $ref value
			// that is a path, then it replaces the $ref object with a reference to the value
			// that is found by the path. When it finds a $class value that names a function in
			// the global scope, it assumes the function is a constructor and uses it to create an
			// object which replaces the JSON such that it is restored with the appropriate
			// class semantics and capability rather than just a general object. If no
			// constructor exists, a POJO is used.
 
			// AnyWhichWay, Feb 2016, replaced separate array and object loops with forEach
			Object.keys(value).forEach(
					function(name) {
						value[name] = resurrect(context, value[name]);
						if (value[name] && typeof value[name] === "object"
								&& typeof value[name].$ref === "string"
								&& px.test(value[name].$ref)) {
							value[name] = Function("dollar",
									"var $ = dollar; return " + value[name].$ref)($);
						} else if (value[name] && typeof value[name] === "object") {
							rez(value[name]);
						}
					});
 
		}($));
		return $;
	};
 
	Eif(typeof(module)!=="undefined") {
		module.exports = cycler;
	}
	Iif(typeof(window)!=="undefined") {
		window.Cycler = cycler;
	}
})();