Code coverage report for regenerate/regenerate.js

Statements: 98.73% (312 / 316)      Branches: 95.6% (152 / 159)      Functions: 98.31% (58 / 59)      Lines: 98.73% (312 / 316)     

All files » regenerate/ » regenerate.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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618  1     1     1         1 1 1         1 1 1 6041163     1 2 2 26 24     2     1 1 1195   1 1142     1 36     1 22     1 7 7 7 58   7     1 6023 6023 6023 15084454       1 60 60 5951 5900         1 6035212 6029312   5900       1   30 70470242     30 30 30 2359918 2359915 2359915     30         1 1 11878 11878         1 11878     1       1   29 3     26 26     1 4 1       3 1     2 2   7 2 2     5 5 5   2     1 103 103 102 179 26     76     1 18 18 18 18 18 84 84 64     17     1 1 1 1 1 1 12 12 5     1     1 1102 1   1101 1076 1076   25 7 7   18 15 1071   15   3     1 23 1   22 2 2 11 6     2   20 9   11 5   6 5 12   5   1           1 1 6029321     1 6029321     1 1 11906 11906           28 11878   29     11849                   11906       1 93 93 93 93     11 11   82       1 5932 5932 5932 5932   5932   5932 5932 6361180 6361143 6361143 6361143   37 16 16 21 1 1   20 20   37 37 37     5932 16 16 5916 3 3   5913 5913     5932 4   5928           1 1     34 34 34 34   34 6361225           5130 6356095   326770 6029325   6029321 6029321           4         30   30 5891 5891     30     30 4   30 29   30 9       30 3   30           1 16 1       15 1     14   14     1 7     1 3 1       2 1     1     1 9 1       8 1     7     7 101     7     1 4         1 3 1       2 1     1 1   4 1 1     3 3 3   1         1 13 13     1 1   21 2   21 21     5 2   5 5     6       6     2 2 2 2 1026 514     2 2     1 1     1 1     5           1 1     1 1     10 10       1   1 14   1 13 2   13     1                                 1       1               1 1 1                  
/*! http://mths.be/regenerate v0.5.3 by @mathias | MIT license */
;(function(root) {
 
	// Detect free variables `exports`
	var freeExports = typeof exports == 'object' && exports;
 
	// Detect free variable `module`
	var freeModule = typeof module == 'object' && module &&
		module.exports == freeExports && module;
 
	// Detect free variable `global`, from Node.js or Browserified code,
	// and use it as `root`
	var freeGlobal = typeof global == 'object' && global;
	Eif (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
		root = freeGlobal;
	}
 
	/*--------------------------------------------------------------------------*/
 
	var object = {};
	var hasOwnProperty = object.hasOwnProperty;
	var hasKey = function(object, key) {
		return hasOwnProperty.call(object, key);
	};
 
	var extend = function(destination, source) {
		var key;
		for (key in source) {
			if (hasOwnProperty.call(source, key)) {
				destination[key] = source[key];
			}
		}
		return destination;
	};
 
	var toString = object.toString;
	var isArray = function(value) {
		return toString.call(value) == '[object Array]';
	};
	var isNumber = function(value) {
		return typeof value == 'number' ||
			toString.call(value) == '[object Number]';
	};
	var isString = function(value) {
		return typeof value == 'string' ||
			toString.call(value) == '[object String]';
	};
	var isFunction = function(value) {
		return typeof value == 'function';
	};
 
	var map = function(array, callback) {
		var index = -1;
		var length = array.length;
		while (++index < length) {
			array[index] = callback(array[index]);
		}
		return array;
	};
 
	var forEach = function(array, callback) {
		var index = -1;
		var length = array.length;
		while (++index < length) {
			callback(array[index], index);
		}
	};
 
	var forOwn = function(object, callback) {
		var key;
		for (key in object) {
			if (hasKey(object, key)) {
				callback(key, object[key]);
			}
		}
	};
 
	var append = function(object, key, value) {
		if (hasKey(object, key)) {
			object[key].push(value);
		} else {
			object[key] = [value];
		}
	};
 
	var sortUniqueNumbers = function(array) {
		// Sort numerically in ascending order
		array = array.sort(function(a, b) {
			return a - b;
		});
		// Remove duplicates
		var previous;
		var result = [];
		forEach(array, function(item, index) {
			if (previous != item) {
				result.push(item);
				previous = item;
			}
		});
		return result;
	};
 
	// This assumes that `number` is a positive integer that `toString()`s nicely
	// (which is the case for all code point values).
	var zeroes = '0000';
	var pad = function(number, totalCharacters) {
		var string = String(number);
		return string.length < totalCharacters
			? (zeroes + string).slice(-totalCharacters)
			: string;
	};
 
	var hex = function(number) {
		return Number(number).toString(16).toUpperCase();
	};
 
	var slice = [].slice;
 
	/*--------------------------------------------------------------------------*/
 
	var range = function(start, stop) {
		// inclusive, e.g. `range(1, 3)` → `[1, 2, 3]`
		if (stop < start) {
			throw Error('A range\u2019s `stop` value must be greater than or equal ' +
				'to the `start` value.');
		}
		for (var result = []; start <= stop; result.push(start++));
		return result;
	};
 
	var ranges = function(codePointRanges) {
		if (!isArray(codePointRanges)) {
			throw TypeError('ranges(): The `codePointRanges` argument must be an ' +
				'array.');
		}
 
		if (!codePointRanges.length) {
			return [];
		}
 
		var codePoints = [];
		forEach(codePointRanges, function(codePointRange) {
			// If it’s a single code point (not a range)
			if (!isArray(codePointRange)) {
				codePoints.push(codePointRange);
				return;
			}
			// If it’s a range (not a single code point)
			var start = codePointRange[0];
			var stop = codePointRange[1];
			codePoints = codePoints.concat(range(start, stop));
		});
		return codePoints;
	};
 
	var contains = function(array, value) {
		var index = -1;
		var length = array.length;
		while (++index < length) {
			if (array[index] == value) {
				return true;
			}
		}
		return false;
	};
 
	var difference = function(a, b) {
		var index = -1;
		var length = a.length;
		var result = [];
		var value;
		while (++index < length) {
			value = a[index];
			if (!contains(b, value)) {
				result.push(value);
			}
		}
		return result;
	};
 
	var intersection = function(a, b) {
		var index = -1;
		var length = a.length;
		var result = [];
		var value;
		while (++index < length) {
			value = a[index];
			if (contains(b, value)) {
				result.push(value);
			}
		}
		return result;
	};
 
	var add = function(destination, value) {
		if (!isArray(destination)) {
			throw TypeError('add(): The `destination` argument must be an array.');
		}
		if (isNumber(value)) {
			destination.push(Number(value));
			return destination;
		}
		if (isString(value)) {
			destination.push(symbolToCodePoint(value));
			return destination;
		}
		if (isArray(value)) {
			forEach(value, function(item) {
				destination = add(destination, item);
			});
			return destination;
		}
		return destination;
	};
 
	var remove = function(destination, value) {
		if (!isArray(destination)) {
			throw TypeError('remove(): The `destination` argument must be an array.');
		}
		if (isFunction(value)) {
			var array = [];
			forEach(destination, function(item) {
				if (!value(item)) {
					array.push(item);
				}
			});
			return array;
		}
		if (isNumber(value)) {
			return difference(destination, [value]);
		}
		if (isString(value)) {
			return difference(destination, [symbolToCodePoint(value)]);
		}
		if (isArray(value)) {
			forEach(value, function(item) {
				destination = remove(destination, item);
			});
			return destination;
		}
		return destination;
	};
 
	/*--------------------------------------------------------------------------*/
 
	// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
	var floor = Math.floor;
	var highSurrogate = function(codePoint) {
		return parseInt(floor((codePoint - 0x10000) / 0x400) + 0xD800, 10);
	};
 
	var lowSurrogate = function(codePoint) {
		return parseInt((codePoint - 0x10000) % 0x400 + 0xDC00, 10);
	};
 
	var stringFromCharCode = String.fromCharCode;
	var codePointToString = function(codePoint) {
		var string;
		if (
			(codePoint >= 0x41 && codePoint <= 0x5A) ||
			(codePoint >= 0x61 && codePoint <= 0x7A) ||
			(codePoint >= 0x30 && codePoint <= 0x39)
		) {
			// [a-zA-Z0-9]
			string = stringFromCharCode(codePoint);
		} else if (codePoint <= 0xFF) {
			// http://mathiasbynens.be/notes/javascript-escapes#hexadecimal
			string = '\\x' + pad(hex(codePoint), 2);
		} else { // if (codePoint <= 0xFFFF)
			// http://mathiasbynens.be/notes/javascript-escapes#unicode
			string = '\\u' + pad(hex(codePoint), 4);
		}
 
		// There’s no need to account for astral symbols / surrogate pairs here,
		// since `codePointToString` is private and only used for BMP code points.
		// But if that’s what you need, just add an `else` block with this code:
		//
		//     string = '\\u' + pad(hex(highSurrogate(codePoint)), 4)
		//     	+ '\\u' + pad(hex(lowSurrogate(codePoint)), 4);
 
		return string;
	};
 
	// Based on `punycode.ucs2.decode`: http://mths.be/punycode
	var symbolToCodePoint = function(symbol) {
		var length = symbol.length;
		var value = symbol.charCodeAt(0);
		var extra;
		if ((value & 0xF800) == 0xD800 && length > 1) {
			// `value` is a high surrogate, and there is a next character — assume
			// it’s a low surrogate (else it’s invalid use of Regenerate anyway).
			extra = symbol.charCodeAt(1);
			return ((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000;
		} else {
			return value;
		}
	};
 
	var createBMPCharacterClasses = function(codePoints) {
		var tmp = '';
		var start = codePoints[0];
		var end = start;
		var predict = start + 1;
 
		codePoints = codePoints.slice(1);
 
		var counter = 0;
		forEach(codePoints, function(code) {
			if (predict == code) {
				end = code;
				predict = code + 1;
				return;
			}
			if (start == end) {
				tmp += codePointToString(start);
				counter += 1;
			} else if (end == start + 1) {
				tmp += codePointToString(start) + codePointToString(end);
				counter += 2;
			} else {
				tmp += codePointToString(start) + '-' + codePointToString(end);
				counter += 2;
			}
			start = code;
			end = code;
			predict = code + 1;
		});
 
		if (start == end) {
			tmp += codePointToString(start);
			counter += 1;
		} else if (end == start + 1) {
			tmp += codePointToString(start) + codePointToString(end);
			counter += 2;
		} else {
			tmp += codePointToString(start) + '-' + codePointToString(end);
			counter += 2;
		}
 
		if (counter == 1) {
			return tmp;
		} else {
			return '[' + tmp + ']';
		}
	};
 
	// In Regenerate output, `\0` will never be preceded by `\` because we sort
	// by code point value, so let’s keep this regular expression simple:
	var regexNull = /\\x00([^0123456789]|$)/g;
	var createCharacterClasses = function(codePoints) {
		// At this point, it’s safe to assume `codePoints` is a sorted array of
		// numeric code point values.
		var bmp = [];
		var astralMap = {};
		var surrogates = [];
		var hasAstral = false;
 
		forEach(codePoints, function(codePoint) {
			if (codePoint >= 0xD800 && codePoint <= 0xDBFF) {
				// If a high surrogate is followed by a low surrogate, the two code
				// units should be matched together, so that the regex always matches a
				// full code point. For this reason, separate code points that are
				// (unmatched) high surrogates are tracked separately, so they can be
				// moved to the end if astral symbols are to be matched as well.
				surrogates.push(codePoint);
			} else if (codePoint >= 0x0000 && codePoint <= 0xFFFF) {
				// non-surrogate BMP code point
				bmp.push(codePoint);
			} else if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) {
				// astral code point
				hasAstral = true;
				append(
					astralMap,
					highSurrogate(codePoint),
					lowSurrogate(codePoint)
				);
			} else {
				throw RangeError('Invalid code point value. Code points range from ' +
					'U+000000 to U+10FFFF.');
			}
		});
 
		var astralMapByLowRanges = {};
 
		forOwn(astralMap, function(highSurrogate, lowSurrogate) {
			var bmpRange = createBMPCharacterClasses(lowSurrogate);
			append(astralMapByLowRanges, bmpRange, +highSurrogate);
		});
 
		var tmp = [];
		// If we’re not dealing with any astral symbols, there’s no need to move
		// individual code points that are high surrogates to the end of the regex.
		if (!hasAstral && surrogates.length) {
			bmp = sortUniqueNumbers(bmp.concat(surrogates));
		}
		if (bmp.length) {
			tmp.push(createBMPCharacterClasses(bmp));
		}
		forOwn(astralMapByLowRanges, function(lowSurrogate, highSurrogate) {
			tmp.push(createBMPCharacterClasses(highSurrogate) + lowSurrogate);
		});
		// Individual code points that are high surrogates must go at the end
		// if astral symbols are to be matched as well.
		if (hasAstral && surrogates.length) {
			tmp.push(createBMPCharacterClasses(surrogates));
		}
		return tmp
			.join('|')
			// Use `\0` instead of `\x00` where possible
			.replace(regexNull, '\\0$1');
	};
 
	var fromCodePoints = function(codePoints) {
		if (!isArray(codePoints)) {
			throw TypeError('fromCodePoints(): The `codePoints` argument must be ' +
				'an array.');
		}
 
		if (!codePoints.length) {
			return '';
		}
 
		codePoints = sortUniqueNumbers(codePoints);
 
		return createCharacterClasses(codePoints);
	};
 
	var fromCodePointRange = function(start, end) {
		return createCharacterClasses(range(start, end));
	};
 
	var fromCodePointRanges = function(codePointRanges) {
		if (!isArray(codePointRanges)) {
			throw TypeError('fromCodePointRanges(): The `ranges` argument must be ' +
				'an array.');
		}
 
		if (!codePointRanges.length) {
			return '';
		}
 
		return createCharacterClasses(ranges(codePointRanges));
	};
 
	var fromSymbols = function(symbols) {
		if (!isArray(symbols)) {
			throw TypeError('fromSymbols(): The `symbols` argument must be an ' +
				'array.');
		}
 
		if (!symbols.length) {
			return '';
		}
 
		var codePoints = map(symbols, symbolToCodePoint);
 
		// Sort code points numerically
		codePoints = codePoints.sort(function(a, b) {
			return a - b;
		});
 
		return createCharacterClasses(codePoints);
	};
 
	var fromSymbolRange = function(start, end) {
		return createCharacterClasses(
			range(symbolToCodePoint(start), symbolToCodePoint(end))
		);
	};
 
	var fromSymbolRanges = function(symbolRanges) {
		if (!isArray(symbolRanges)) {
			throw TypeError('fromSymbolRanges(): The `ranges` argument must be an ' +
				'array.');
		}
 
		if (!symbolRanges.length) {
			return '';
		}
 
		var codePoints = [];
		forEach(symbolRanges, function(symbolRange) {
			// If it’s a single symbol (not a range)
			if (!isArray(symbolRange)) {
				codePoints.push(symbolToCodePoint(symbolRange));
				return;
			}
			// If it’s a range (not a single code point)
			var start = symbolToCodePoint(symbolRange[0]);
			var stop = symbolToCodePoint(symbolRange[1]);
			codePoints = codePoints.concat(range(start, stop));
		});
		return createCharacterClasses(codePoints);
	};
 
	/*--------------------------------------------------------------------------*/
 
	var Set = function(value) {
		this.__codePoints__ = [];
		return this;
	};
 
	var proto = Set.prototype;
	extend(proto, {
		'add': function(value) {
			if (arguments.length > 1) {
				value = slice.call(arguments);
			}
			this.__codePoints__ = add(this.__codePoints__, value);
			return this;
		},
		'remove': function(value) {
			if (arguments.length > 1) {
				value = slice.call(arguments);
			}
			this.__codePoints__ = remove(this.__codePoints__, value);
			return this;
		},
		'addRange': function(start, end) {
			this.__codePoints__ = add(this.__codePoints__, range(
				isNumber(start) ? start : symbolToCodePoint(start),
				isNumber(end) ? end : symbolToCodePoint(end)
			));
			return this;
		},
		'removeRange': function(start, end) {
			var startCodePoint = isNumber(start) ? start : symbolToCodePoint(start);
			var endCodePoint = isNumber(end) ? end : symbolToCodePoint(end);
			var array = [];
			forEach(this.__codePoints__, function(codePoint) {
				if (codePoint < startCodePoint || codePoint > endCodePoint) {
					array.push(codePoint);
				}
			});
			this.__codePoints__ = array;
			return this;
		},
		'difference': function(array) {
			this.__codePoints__ = difference(this.__codePoints__, array);
			return this;
		},
		'intersection': function(array) {
			this.__codePoints__ = intersection(this.__codePoints__, array);
			return this;
		},
		'contains': function(codePoint) {
			return contains(
				this.__codePoints__,
				isNumber(codePoint) ? codePoint : symbolToCodePoint(codePoint)
			);
		},
		'toString': function() {
			this.__codePoints__ = sortUniqueNumbers(this.__codePoints__);
			return createCharacterClasses(this.__codePoints__);
		},
		'toRegExp': function() {
			this.__codePoints__ = sortUniqueNumbers(this.__codePoints__);
			return RegExp(createCharacterClasses(this.__codePoints__));
		},
		'valueOf': function() { // has alias `toArray`
			this.__codePoints__ = sortUniqueNumbers(this.__codePoints__);
			return this.__codePoints__;
		}
	});
 
	proto.toArray = proto.valueOf;
 
	var set = function(value) {
		if (value instanceof Set) {
			// this is already a set; don’t wrap it again
			return value;
		} else if (arguments.length > 1) {
			value = slice.call(arguments);
		}
		return (new Set).add(value);
	};
 
	extend(set, {
		'version': '0.5.3',
		'fromCodePoints': fromCodePoints,
		'fromCodePointRange': fromCodePointRange,
		'fromCodePointRanges': fromCodePointRanges,
		'fromSymbols': fromSymbols,
		'fromSymbolRange': fromSymbolRange,
		'fromSymbolRanges': fromSymbolRanges,
		'range': range,
		'ranges': ranges,
		'contains': contains,
		'difference': difference,
		'intersection': intersection,
		'add': add,
		'remove': remove
	});
 
	var regenerate = set;
 
	// Some AMD build optimizers, like r.js, check for specific condition patterns
	// like the following:
	Iif (
		typeof define == 'function' &&
		typeof define.amd == 'object' &&
		define.amd
	) {
		define(function() {
			return regenerate;
		});
	}	else Eif (freeExports && !freeExports.nodeType) {
		Eif (freeModule) { // in Node.js or RingoJS v0.8.0+
			freeModule.exports = regenerate;
		} else { // in Narwhal or RingoJS v0.7.0-
			freeExports.regenerate = regenerate;
		}
	} else { // in Rhino or a web browser
		root.regenerate = regenerate;
	}
 
}(this));