UNPKG

2.01 kBJavaScriptView Raw
1
2/* litejs.com/MIT-LICENSE.txt */
3
4
5
6!function(exports) {
7 var Str = exports.String || exports
8 , Str$ = Str.prototype || exports
9 , fromCharCode = String.fromCharCode
10
11 if (!Str$.startsWith) Str$.startsWith = function(str) {
12 return this.lastIndexOf(str, 0) === 0
13 }
14
15 if (!Str$.endsWith) Str$.endsWith = function(str) {
16 return this.indexOf(str, this.length - str.length) !== -1
17 }
18
19 if (!Str$.codePointAt) Str$.codePointAt = function(pos) {
20 var str = this
21 , code = str.charCodeAt(pos)
22
23 return code >= 0xD800 && code <= 0xDBFF &&
24 (str = str.charCodeAt(pos + 1)) >= 0xDC00 && str <= 0xDFFF ?
25 (code - 0xD800) * 0x400 + str - 0xDC00 + 0x10000 :
26 code === code ? code :
27 void 0
28 }
29
30 if (!Str.fromCodePoint) Str.fromCodePoint = function() {
31 var code
32 , arr = arguments
33 , len = arr.length
34 , pos = 0
35 , out = []
36 , str = ""
37
38 for (; pos < len; ) {
39 code = arr[pos++]
40 if (code <= 0xFFFF) {
41 out.push(code)
42 } else {
43 code -= 0x10000
44 out.push(
45 (code >> 10) + 0xD800,
46 (code % 0x400) + 0xDC00
47 )
48 }
49 if (len === pos || out.length > 8191) {
50 str += fromCharCode.apply(null, out)
51 out.length = 0
52 }
53 }
54
55 return str
56 }
57
58 if (!exports.TextEncoder) {
59 exports.TextEncoder = exports.TextDecoder = TextEncoder
60 }
61
62 // Only utf-8 TextEncoder is supported by spec
63 function TextEncoder(encoding) {
64 this.encoding = encoding || "utf-8"
65 }
66 TextEncoder.prototype = {
67 encode: function(str) {
68 var s = unescape(encodeURIComponent(str))
69 , len = s.length
70 , arr = new Uint8Array(len)
71 for (; len--; ) {
72 arr[len] = s.charCodeAt(len)
73 }
74 return arr
75 },
76 decode: function(arr) {
77 var i, out
78 , map = TextEncoder[this.encoding]
79 if (map) {
80 // Single-byte codec
81 out = []
82 for (i = arr.length; i--; ) {
83 out[i] = (
84 arr[i] > 127 ?
85 map.charCodeAt(arr[i] - 128) :
86 arr[i]
87 )
88 }
89 return fromCharCode.apply(null, out)
90 }
91 return decodeURIComponent(escape(fromCharCode.apply(null, arr)))
92 }
93 }
94}(this)
95