diff --git a/package.json b/package.json index 3a48e48..b767ed7 100644 --- a/package.json +++ b/package.json @@ -5,5 +5,9 @@ "main": "text.min.js", "repository": "https://github.com/samthor/fast-text-encoder.git", "author": "Sam Thorogood ", - "license": "Apache-2" + "license": "Apache-2", + "devDependencies": { + "chai": "^4.2.0", + "mocha": "^7.1.0" + } } diff --git a/polyfill.js b/polyfill.js deleted file mode 100644 index 4063ea4..0000000 --- a/polyfill.js +++ /dev/null @@ -1,10 +0,0 @@ -window.module = { - exports: {}, -}; - -const hack = new Proxy({}, { - get() { - return []; - }, -}); -window.require = () => ({'encoding-indexes': hack}); diff --git a/suite.js b/suite.js new file mode 100644 index 0000000..e2a8b34 --- /dev/null +++ b/suite.js @@ -0,0 +1,93 @@ +function tests(isNative, TextEncoder, TextDecoder) { + const dec = new TextDecoder(); + const enc = new TextEncoder('utf-8'); + + suite(isNative ? 'native' : 'polyfill', () => { + + suite('decoder', () => { + + test('basic', () => { + const buffer = new Uint8Array([104, 101, 108, 108, 111]); + assert.equal(dec.decode(buffer), 'hello'); + }); + + test('constructor', () => { + assert.throws(() => { + new TextDecoder('invalid'); + }, RangeError); + + if (!isNative) { + assert.throws(() => { + new TextDecoder('utf-8', {fatal: true}); + }, Error, 'unsupported', 'fatal is unsupported'); + } + }); + + test('null in middle', () => { + const s = 'pad\x00pad'; + const buffer = new Uint8Array([112, 97, 100, 0, 112, 97, 100]); + assert.deepEqual(dec.decode(buffer), s); + }); + + test('null at ends', () => { + const s = '\x00\x00?\x00\x00'; + const buffer = new Uint8Array([0, 0, 63, 0, 0]); + assert.deepEqual(dec.decode(buffer), s); + }); + + }); + + suite('encoder', () => { + + test('basic', () => { + const buffer = new Uint8Array([104, 101, 108, 108, 111]); + assert.deepEqual(enc.encode('hello'), buffer); + }); + + test('constructor', () => { + const enc2 = new TextEncoder('literally anything can go here'); + const enc3 = new TextEncoder(new Object()); + + // Despite having no difference in functionality, these should not be the + // same object. + assert.notEqual(enc, enc2); + assert.notEqual(enc, enc3); + }); + + test('ie11 .slice', () => { + const originalSlice = Uint8Array.prototype.slice; + try { + Uint8Array.prototype.slice = null; + assert.isNull(Uint8Array.prototype.slice); + + // Confirms that the method works even without .slice. + const buffer = new Uint8Array([194, 161]); + assert.deepEqual(enc.encode('ยก'), buffer); + + } finally { + Uint8Array.prototype.slice = originalSlice; + } + }); + + test('null in middle', () => { + const s = 'pad\x00pad'; + const buffer = new Uint8Array([112, 97, 100, 0, 112, 97, 100]); + assert.deepEqual(enc.encode(s), buffer); + }); + + test('null at ends', () => { + const s = '\x00\x00?\x00\x00'; + const buffer = new Uint8Array([0, 0, 63, 0, 0]); + assert.deepEqual(enc.encode(s), buffer); + }); + + }); + + }); + +} + +if (window.NativeTextEncoder && window.NativeTextDecoder) { + tests(true, NativeTextEncoder, NativeTextDecoder); +} +tests(false, TextEncoder, TextDecoder); diff --git a/test.html b/test.html index a80122f..f8b9c9b 100644 --- a/test.html +++ b/test.html @@ -1,6 +1,52 @@ + + + + + + + + + +
+ + + + diff --git a/text.js b/text.js index 28df59c..e8545c2 100644 --- a/text.js +++ b/text.js @@ -107,7 +107,7 @@ FastTextEncoder.prototype.encode = function(string, options={stream: false}) { // Use subarray if slice isn't supported (IE11). This will use more memory // because the original array still exists. - return output.slice ? output.slice(0, at) : output.subarray(0, at); + return target.slice ? target.slice(0, at) : target.subarray(0, at); } /** @@ -156,7 +156,8 @@ FastTextDecoder.prototype.decode = function(buffer, options={stream: false}) { while (pos < len) { const byte1 = bytes[pos++]; if (byte1 === 0) { - break; // NULL + out.push(0); + continue; } if ((byte1 & 0x80) === 0) { // 1-byte