Code coverage report for src/main.js

Statements: 85.38% (181 / 212)      Branches: 68.27% (71 / 104)      Functions: 80.77% (21 / 26)      Lines: 89.34% (176 / 197)     

All files » src/ » main.js
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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374  1                                         2       2             2     2 2     2       2                     2 2         2 2 4 4 4 4 4 4 3   4 4       2 2     4 2 2           9     9     10 7 2     5     5 5 8 5 5 8 8 8 8 8 8 8 4   8 8       5 5   3     25 2   23 13   10 2     10                             2 28                             2 28 28 28       2 28       2   2           2 28 28 28 28 28   28   28     2             2 48 48   48 35       13 13       13 13       13 13 13 13             2 2 2 2 4 4 3   1     1 4 4 4   2     2             2   9 9 9 9 9 2 7     7     5 5 5 5 8 4   4     4 8 8 8 8 8 8 8   5     12 12 12 12 12 12   71 71 2 2 2 2 2       2     12 9 12 12         9 9 9 9 9 9         9 19 19 14 8 1 1   7 7   9 9 9 9       2 2 20       2 103     103 103 75   75 75     2  
 
angular.module('rison',[])
	/**
	 * The $rison class/service provides a simple API for encoding and 
	 * decoding javascript objects into a compact and URI friendly format
	 * @example
	 * angular.module('foo',['rison'])
	 *     .run(['$rison',function($rison){
	 *         var object = {
	 *             foo:'bar'
	 *         };
	 *         var string = $rison.stringify(object);
	 *         // object === $rison.parse(string) will evalutate to true
	 * }]);
	 *
	 * @version v<%=pkg.version%>
	 * @name $rison
	 * @class
	 */
	.factory('$rison',[
 
		function(){
			var publicMembers = {},
				privateMembers = {};
 
 
			privateMembers.not_idchar  = " '!:(),*@$";
 
 
			/**
			 * characters that are illegal as the start of an id
			 * this is so ids can't look like numbers.
			 */
			privateMembers.not_idstart = "-0123456789";
 
 
			(function () {
			    var idrx = '[^' + privateMembers.not_idstart + privateMembers.not_idchar + 
			               '][^' + privateMembers.not_idchar + ']*';
 
			    privateMembers.id_ok = new RegExp('^' + idrx + '$');
 
			    // regexp to find the end of an id when parsing
			    // g flag on the regexp is necessary for iterative regexp.exec()
			    privateMembers.next_id = new RegExp(idrx, 'g');
			})();
 
 
			//
			//  based on json.js 2006-04-28 from json.org
			//  license: http://www.json.org/license.html
			//
			//  hacked by nix for use in uris.
			//
 
			(function () {
			    var sq = { // url-ok but quoted in strings
			               "'": true,  '!': true
			    },
			    s = {
			            array: function (x) {
			                var a = ['!('], b, f, i, l = x.length, v;
			                for (i = 0; i < l; i += 1) {
			                    v = x[i];
			                    f = s[typeof v];
			                    Eif (f) {
			                        v = f(v);
			                        Eif (typeof v == 'string') {
			                            if (b) {
			                                a[a.length] = ',';
			                            }
			                            a[a.length] = v;
			                            b = true;
			                        }
			                    }
			                }
			                a[a.length] = ')';
			                return a.join('');
			            },
			            'boolean': function (x) {
			                if (x)
			                    return '!t';
			                return '!f';
			            },
			            'null': function (x) {
			                return "!n";
			            },
			            number: function (x) {
			                Iif (!isFinite(x))
			                    return '!n';
			                // strip '+' out of exponent, '-' is ok though
			                return String(x).replace(/\+/,'');
			            },
			            object: function (x) {
			                if (x) {
			                    if (x instanceof Array) {
			                        return s.array(x);
			                    }
			                    // WILL: will this work on non-Firefox browsers?
			                    Iif (typeof x.__prototype__ === 'object' && typeof x.__prototype__.encode_rison !== 'undefined')
			                        return x.encode_rison();
 
			                    var a = ['('], b, f, i, v, ki, ks=[];
			                    for (i in x)
			                        ks[ks.length] = i;
			                    ks.sort();
			                    for (ki = 0; ki < ks.length; ki++) {
			                        i = ks[ki];
			                        v = x[i];
			                        f = s[typeof v];
			                        Eif (f) {
			                            v = f(v);
			                            Eif (typeof v == 'string') {
			                                if (b) {
			                                    a[a.length] = ',';
			                                }
			                                a.push(s.string(i), ':', v);
			                                b = true;
			                            }
			                        }
			                    }
			                    a[a.length] = ')';
			                    return a.join('');
			                }
			                return '!n';
			            },
			            string: function (x) {
			                if (x === '')
			                    return "''";
 
			                if (privateMembers.id_ok.test(x))
			                    return x;
 
			                x = x.replace(/(['!])/g, function(a, b) {
			                    Eif (sq[b]) return '!'+b;
			                    return b;
			                });
			                return "'" + x + "'";
			            },
			            undefined: function (x) {
			                throw new Error("rison can't encode the undefined value");
			            }
			        };
 
			    
			    /**
			     * This method Rison-encodes a javascript structure.
			     * @public
			     * @name $rison#stringify
			     * @param  {Object} object The object to encode
			     * @return {String}        Returns a Rison-encoded String
			     */
			    publicMembers.stringify = function (object) {
			        return s[typeof object](object);
			    };
 
			})();
 
 
 
 
			/**
			 * This method parses a rison string into a javascript object or primitive
			 * @public
			 * @name $rison#parse
			 * @param  {String} rison The string that will be parsed
			 * @return {Object|*}   An objectual, or primitive, representation of the rison string
			 */
			publicMembers.parse = function(r) {
			    var errcb = function(e) { throw Error('rison decoder error: ' + e); };
			    var p = new privateMembers.parser(errcb);
			    return p.parse(r);
			};
 
			
			privateMembers.parser = function (errcb) {
			    this.errorHandler = errcb;
			};
 
			
			privateMembers.parser.WHITESPACE = "";
 
			privateMembers.parser.prototype.setOptions = function (options) {
			    if (options.errorHandler)
			        this.errorHandler = options.errorHandler;
			};
 
 
			privateMembers.parser.prototype.parse = function (str) {
			    this.string = str;
			    this.index = 0;
			    this.message = null;
			    var value = this.readValue();
			    Iif (!this.message && this.next())
			        value = this.error("unable to parse string as rison: '" + publicMembers.encode(str) + "'");
			    Iif (this.message && this.errorHandler)
			        this.errorHandler(this.message, this.index);
			    return value;
			};
 
			privateMembers.parser.prototype.error = function (message) {
			    if (typeof(console) != 'undefined')
			        console.error('Rison parser error: ', message);
			    this.message = message;
			    return undefined;
			};
			    
			privateMembers.parser.prototype.readValue = function () {
			    var c = this.next();
			    var fn = c && this.table[c];
 
			    if (fn)
			        return fn.apply(this);
 
			    // fell through table, parse as an id
 
			    var s = this.string;
			    var i = this.index-1;
 
			    // Regexp.lastIndex may not work right in IE before 5.5?
			    // g flag on the regexp is also necessary
			    privateMembers.next_id.lastIndex = i;
			    var m = privateMembers.next_id.exec(s);
 
			    // console.log('matched id', i, r.lastIndex);
 
			    Eif (m.length > 0) {
			        var id = m[0];
			        this.index = i+id.length;
			        return id;  // a string
			    }
 
			    if (c) return this.error("invalid character: '" + c + "'");
			    return this.error("empty expression");
			};
 
			privateMembers.parser.parse_array = function (parser) {
			    var ar = [];
			    var c;
			    while ((c = parser.next()) != ')') {
			        Iif (!c) return parser.error("unmatched '!('");
			        if (ar.length) {
			            Iif (c != ',')
			                parser.error("missing ','");
			        } else Iif (c == ',') {
			            return parser.error("extra ','");
			        } else
			            --parser.index;
			        var n = parser.readValue();
			        Iif (typeof n == "undefined") return undefined;
			        ar.push(n);
			    }
			    return ar;
			};
 
			privateMembers.parser.bangs = {
			    t: true,
			    f: false,
			    n: null,
			    '(': privateMembers.parser.parse_array
			};
 
			privateMembers.parser.prototype.table = {
			    '!': function () {
			        var s = this.string;
			        var c = s.charAt(this.index++);
			        Iif (!c) return this.error('"!" at end of input');
			        var x = privateMembers.parser.bangs[c];
			        if (typeof(x) == 'function') {
			            return x.call(null, this);
			        } else Iif (typeof(x) == 'undefined') {
			            return this.error('unknown literal: "!' + c + '"');
			        }
			        return x;
			    },
			    '(': function () {
			        var o = {};
			        var c;
			        var count = 0;
			        while ((c = this.next()) != ')') {
			            if (count) {
			                Iif (c != ',')
			                    this.error("missing ','");
			            } else Iif (c == ',') {
			                return this.error("extra ','");
			            } else
			                --this.index;
			            var k = this.readValue();
			            Iif (typeof k == "undefined") return undefined;
			            Iif (this.next() != ':') return this.error("missing ':'");
			            var v = this.readValue();
			            Iif (typeof v == "undefined") return undefined;
			            o[k] = v;
			            count++;
			        }
			        return o;
			    },
			    "'": function () {
			        var s = this.string;
			        var i = this.index;
			        var start = i;
			        var segments = [];
			        var c;
			        while ((c = s.charAt(i++)) != "'") {
			            //if (i == s.length) return this.error('unmatched "\'"');
			            Iif (!c) return this.error('unmatched "\'"');
			            if (c == '!') {
			                Eif (start < i-1)
			                    segments.push(s.slice(start, i-1));
			                c = s.charAt(i++);
			                Eif ("!'".indexOf(c) >= 0) {
			                    segments.push(c);
			                } else {
			                    return this.error('invalid string escape: "!'+c+'"');
			                }
			                start = i;
			            }
			        }
			        if (start < i-1)
			            segments.push(s.slice(start, i-1));
			        this.index = i;
			        return segments.length == 1 ? segments[0] : segments.join('');
			    },
			    // Also any digit.  The statement that follows this table
			    // definition fills in the digits.
			    '-': function () {
			        var s = this.string;
			        var i = this.index;
			        var start = i-1;
			        var state = 'int';
			        var permittedSigns = '-';
			        var transitions = {
			            'int+.': 'frac',
			            'int+e': 'exp',
			            'frac+e': 'exp'
			        };
			        do {
			            var c = s.charAt(i++);
			            if (!c) break;
			            if ('0' <= c && c <= '9') continue;
			            if (permittedSigns.indexOf(c) >= 0) {
			                permittedSigns = '';
			                continue;
			            }
			            state = transitions[state+'+'+c.toLowerCase()];
			            if (state == 'exp') permittedSigns = '-';
			        } while (state);
			        this.index = --i;
			        s = s.slice(start, i);
			        Iif (s == '-') return this.error("invalid number");
			        return Number(s);
			    }
			};
			// copy table['-'] to each of table[i] | i <- '0'..'9':
			(function (table) {
			    for (var i = 0; i <= 9; i++)
			        table[String(i)] = table['-'];
			})(privateMembers.parser.prototype.table);
 
			// return the next non-whitespace character, or undefined
			privateMembers.parser.prototype.next = function () {
			    var s = this.string,
			        i = this.index,
			        c;
			    do {
			        if (i == s.length) return undefined;
			        c = s.charAt(i++);
			    } while (privateMembers.parser.WHITESPACE.indexOf(c) >= 0);
			    this.index = i;
			    return c;
			};
 
		return publicMembers;
}]);