arnaucube
arnaucube/socketioMEANseedPublic Self-hosted repository
socketioMEANseed/server/node_modules/wtf-8/wtf-8.js
server/node_modules/wtf-8/wtf-8.jsRaw
  1/*! https://mths.be/wtf8 v1.0.0 by @mathias */
  2;(function(root) {
  3
  4	// Detect free variables `exports`
  5	var freeExports = typeof exports == 'object' && exports;
  6
  7	// Detect free variable `module`
  8	var freeModule = typeof module == 'object' && module &&
  9		module.exports == freeExports && module;
 10
 11	// Detect free variable `global`, from Node.js or Browserified code,
 12	// and use it as `root`
 13	var freeGlobal = typeof global == 'object' && global;
 14	if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
 15		root = freeGlobal;
 16	}
 17
 18	/*--------------------------------------------------------------------------*/
 19
 20	var stringFromCharCode = String.fromCharCode;
 21
 22	// Taken from https://mths.be/punycode
 23	function ucs2decode(string) {
 24		var output = [];
 25		var counter = 0;
 26		var length = string.length;
 27		var value;
 28		var extra;
 29		while (counter < length) {
 30			value = string.charCodeAt(counter++);
 31			if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
 32				// high surrogate, and there is a next character
 33				extra = string.charCodeAt(counter++);
 34				if ((extra & 0xFC00) == 0xDC00) { // low surrogate
 35					output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
 36				} else {
 37					// unmatched surrogate; only append this code unit, in case the next
 38					// code unit is the high surrogate of a surrogate pair
 39					output.push(value);
 40					counter--;
 41				}
 42			} else {
 43				output.push(value);
 44			}
 45		}
 46		return output;
 47	}
 48
 49	// Taken from https://mths.be/punycode
 50	function ucs2encode(array) {
 51		var length = array.length;
 52		var index = -1;
 53		var value;
 54		var output = '';
 55		while (++index < length) {
 56			value = array[index];
 57			if (value > 0xFFFF) {
 58				value -= 0x10000;
 59				output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);
 60				value = 0xDC00 | value & 0x3FF;
 61			}
 62			output += stringFromCharCode(value);
 63		}
 64		return output;
 65	}
 66
 67	/*--------------------------------------------------------------------------*/
 68
 69	function createByte(codePoint, shift) {
 70		return stringFromCharCode(((codePoint >> shift) & 0x3F) | 0x80);
 71	}
 72
 73	function encodeCodePoint(codePoint) {
 74		if ((codePoint & 0xFFFFFF80) == 0) { // 1-byte sequence
 75			return stringFromCharCode(codePoint);
 76		}
 77		var symbol = '';
 78		if ((codePoint & 0xFFFFF800) == 0) { // 2-byte sequence
 79			symbol = stringFromCharCode(((codePoint >> 6) & 0x1F) | 0xC0);
 80		}
 81		else if ((codePoint & 0xFFFF0000) == 0) { // 3-byte sequence
 82			symbol = stringFromCharCode(((codePoint >> 12) & 0x0F) | 0xE0);
 83			symbol += createByte(codePoint, 6);
 84		}
 85		else if ((codePoint & 0xFFE00000) == 0) { // 4-byte sequence
 86			symbol = stringFromCharCode(((codePoint >> 18) & 0x07) | 0xF0);
 87			symbol += createByte(codePoint, 12);
 88			symbol += createByte(codePoint, 6);
 89		}
 90		symbol += stringFromCharCode((codePoint & 0x3F) | 0x80);
 91		return symbol;
 92	}
 93
 94	function wtf8encode(string) {
 95		var codePoints = ucs2decode(string);
 96		var length = codePoints.length;
 97		var index = -1;
 98		var codePoint;
 99		var byteString = '';
100		while (++index < length) {
101			codePoint = codePoints[index];
102			byteString += encodeCodePoint(codePoint);
103		}
104		return byteString;
105	}
106
107	/*--------------------------------------------------------------------------*/
108
109	function readContinuationByte() {
110		if (byteIndex >= byteCount) {
111			throw Error('Invalid byte index');
112		}
113
114		var continuationByte = byteArray[byteIndex] & 0xFF;
115		byteIndex++;
116
117		if ((continuationByte & 0xC0) == 0x80) {
118			return continuationByte & 0x3F;
119		}
120
121		// If we end up here, it’s not a continuation byte.
122		throw Error('Invalid continuation byte');
123	}
124
125	function decodeSymbol() {
126		var byte1;
127		var byte2;
128		var byte3;
129		var byte4;
130		var codePoint;
131
132		if (byteIndex > byteCount) {
133			throw Error('Invalid byte index');
134		}
135
136		if (byteIndex == byteCount) {
137			return false;
138		}
139
140		// Read the first byte.
141		byte1 = byteArray[byteIndex] & 0xFF;
142		byteIndex++;
143
144		// 1-byte sequence (no continuation bytes)
145		if ((byte1 & 0x80) == 0) {
146			return byte1;
147		}
148
149		// 2-byte sequence
150		if ((byte1 & 0xE0) == 0xC0) {
151			var byte2 = readContinuationByte();
152			codePoint = ((byte1 & 0x1F) << 6) | byte2;
153			if (codePoint >= 0x80) {
154				return codePoint;
155			} else {
156				throw Error('Invalid continuation byte');
157			}
158		}
159
160		// 3-byte sequence (may include unpaired surrogates)
161		if ((byte1 & 0xF0) == 0xE0) {
162			byte2 = readContinuationByte();
163			byte3 = readContinuationByte();
164			codePoint = ((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3;
165			if (codePoint >= 0x0800) {
166				return codePoint;
167			} else {
168				throw Error('Invalid continuation byte');
169			}
170		}
171
172		// 4-byte sequence
173		if ((byte1 & 0xF8) == 0xF0) {
174			byte2 = readContinuationByte();
175			byte3 = readContinuationByte();
176			byte4 = readContinuationByte();
177			codePoint = ((byte1 & 0x0F) << 0x12) | (byte2 << 0x0C) |
178				(byte3 << 0x06) | byte4;
179			if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) {
180				return codePoint;
181			}
182		}
183
184		throw Error('Invalid WTF-8 detected');
185	}
186
187	var byteArray;
188	var byteCount;
189	var byteIndex;
190	function wtf8decode(byteString) {
191		byteArray = ucs2decode(byteString);
192		byteCount = byteArray.length;
193		byteIndex = 0;
194		var codePoints = [];
195		var tmp;
196		while ((tmp = decodeSymbol()) !== false) {
197			codePoints.push(tmp);
198		}
199		return ucs2encode(codePoints);
200	}
201
202	/*--------------------------------------------------------------------------*/
203
204	var wtf8 = {
205		'version': '1.0.0',
206		'encode': wtf8encode,
207		'decode': wtf8decode
208	};
209
210	// Some AMD build optimizers, like r.js, check for specific condition patterns
211	// like the following:
212	if (
213		typeof define == 'function' &&
214		typeof define.amd == 'object' &&
215		define.amd
216	) {
217		define(function() {
218			return wtf8;
219		});
220	}	else if (freeExports && !freeExports.nodeType) {
221		if (freeModule) { // in Node.js or RingoJS v0.8.0+
222			freeModule.exports = wtf8;
223		} else { // in Narwhal or RingoJS v0.7.0-
224			var object = {};
225			var hasOwnProperty = object.hasOwnProperty;
226			for (var key in wtf8) {
227				hasOwnProperty.call(wtf8, key) && (freeExports[key] = wtf8[key]);
228			}
229		}
230	} else { // in Rhino or a web browser
231		root.wtf8 = wtf8;
232	}
233
234}(this));