1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | export const fromCharCode = String.fromCharCode
|
8 | export const fromCodePoint = String.fromCodePoint
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const toLowerCase = s => s.toLowerCase()
|
15 |
|
16 | const trimLeftRegex = /^\s*/g
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | export const trimLeft = s => s.replace(trimLeftRegex, '')
|
23 |
|
24 | const fromCamelCaseRegex = /([A-Z])/g
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export const fromCamelCase = (s, separator) => trimLeft(s.replace(fromCamelCaseRegex, match => `${separator}${toLowerCase(match)}`))
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | export const utf8ByteLength = str => unescape(encodeURIComponent(str)).length
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | export const _encodeUtf8Polyfill = str => {
|
45 | const encodedString = unescape(encodeURIComponent(str))
|
46 | const len = encodedString.length
|
47 | const buf = new Uint8Array(len)
|
48 | for (let i = 0; i < len; i++) {
|
49 | buf[i] = (encodedString.codePointAt(i))
|
50 | }
|
51 | return buf
|
52 | }
|
53 |
|
54 |
|
55 | export const utf8TextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder() : null)
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | export const _encodeUtf8Native = str => utf8TextEncoder.encode(str)
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | export const encodeUtf8 = utf8TextEncoder ? _encodeUtf8Native : _encodeUtf8Polyfill
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | export const _decodeUtf8Polyfill = buf => {
|
75 | let remainingLen = buf.length
|
76 | let encodedString = ''
|
77 | let bufPos = 0
|
78 | while (remainingLen > 0) {
|
79 | const nextLen = remainingLen < 10000 ? remainingLen : 10000
|
80 | const bytes = buf.subarray(bufPos, bufPos + nextLen)
|
81 | bufPos += nextLen
|
82 |
|
83 | encodedString += String.fromCodePoint.apply(null, (bytes))
|
84 | remainingLen -= nextLen
|
85 | }
|
86 | return decodeURIComponent(escape(encodedString))
|
87 | }
|
88 |
|
89 |
|
90 | export let utf8TextDecoder = typeof TextDecoder === 'undefined' ? null : new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
|
91 |
|
92 |
|
93 | if (utf8TextDecoder && utf8TextDecoder.decode(new Uint8Array()).length === 1) {
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | utf8TextDecoder = null
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | export const _decodeUtf8Native = buf => (utf8TextDecoder).decode(buf)
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | export const decodeUtf8 = utf8TextDecoder ? _decodeUtf8Native : _decodeUtf8Polyfill
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | export const splice = (str, index, remove, insert = '') => str.slice(0, index) + insert + str.slice(index + remove)
|