Skip to content

Commit e4ec606

Browse files
authored
Merge pull request #11 from kevinushey/feature/uint8array-subarray
support Uint8Array sub-array; tweak utilities
2 parents 0423145 + 8d34015 commit e4ec606

7 files changed

+188
-12
lines changed

compile.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env sh
2+
3+
google-closure-compiler \
4+
--compilation_level ADVANCED \
5+
--js_output_file text.min.js \
6+
--language_out ECMASCRIPT5 \
7+
--warning_level VERBOSE \
8+
--create_source_map %outname%.map \
9+
text.js

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
"license": "Apache-2.0",
99
"devDependencies": {
1010
"chai": "^4.2.0",
11+
"google-closure-compiler": "^20200406.0.0",
1112
"mocha": "^7.1.0"
1213
},
1314
"scripts": {
15+
"compile": "./compile.sh",
1416
"test": "mocha"
1517
}
1618
}

suite.js

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ function tests(isNative, TextEncoder, TextDecoder) {
3838
}
3939
});
4040

41+
test('subarray', () => {
42+
const buffer = new Uint8Array([104, 101, 108, 108, 111]);
43+
const array = buffer.subarray(0, 4);
44+
assert.equal(dec.decode(array), 'hell');
45+
});
46+
4147
test('null in middle', () => {
4248
const s = 'pad\x00pad';
4349
const buffer = new Uint8Array([112, 97, 100, 0, 112, 97, 100]);

text.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Object.defineProperty(FastTextEncoder.prototype, 'encoding', {value: 'utf-8'});
4646
* @param {{stream: boolean}=} options
4747
* @return {!Uint8Array}
4848
*/
49-
FastTextEncoder.prototype.encode = function(string, options={stream: false}) {
49+
FastTextEncoder.prototype['encode'] = function(string, options={stream: false}) {
5050
if (options.stream) {
5151
throw new Error(`Failed to encode: the 'stream' option is unsupported.`);
5252
}
@@ -135,19 +135,21 @@ Object.defineProperty(FastTextDecoder.prototype, 'ignoreBOM', {value: false});
135135
* @param {{stream: boolean}=} options
136136
* @return {string}
137137
*/
138-
FastTextDecoder.prototype.decode = function(buffer, options={stream: false}) {
138+
FastTextDecoder.prototype['decode'] = function(buffer, options={stream: false}) {
139139
if (options['stream']) {
140140
throw new Error(`Failed to decode: the 'stream' option is unsupported.`);
141141
}
142142

143+
// Accept Uint8Array's as-is.
144+
let bytes = buffer;
145+
143146
// Look for ArrayBufferView, which isn't a real type, but basically represents
144147
// all the valid TypedArray types plus DataView. They all have ".buffer" as
145148
// an instance of ArrayBuffer.
146-
if (buffer.buffer instanceof ArrayBuffer) {
147-
buffer = buffer.buffer;
149+
if (!(bytes instanceof Uint8Array) && bytes.buffer instanceof ArrayBuffer) {
150+
bytes = new Uint8Array(buffer.buffer);
148151
}
149152

150-
let bytes = new Uint8Array(buffer);
151153
let pos = 0;
152154
let pending = [];
153155
const chunks = [];

text.min.js

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

text.min.js.map

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)