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