Skip to content

Commit

Permalink
added test and fixed null in decode
Browse files Browse the repository at this point in the history
  • Loading branch information
samthor committed Mar 6, 2020
1 parent d8685e5 commit 2a0f2f9
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 14 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"main": "text.min.js",
"repository": "https://github.com/samthor/fast-text-encoder.git",
"author": "Sam Thorogood <[email protected]>",
"license": "Apache-2"
"license": "Apache-2",
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^7.1.0"
}
}
10 changes: 0 additions & 10 deletions polyfill.js

This file was deleted.

93 changes: 93 additions & 0 deletions suite.js
Original file line number Diff line number Diff line change
@@ -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);
48 changes: 47 additions & 1 deletion test.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,52 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" href="node_modules/mocha/mocha.css" />
<script src="node_modules/mocha/mocha.js"></script>
<script src="node_modules/chai/chai.js"></script>
<script>
window.NativeTextDecoder = window.TextDecoder;
window.NativeTextEncoder = window.TextEncoder;
window.TextDecoder = null;
window.TextEncoder = null;
</script>
<script src="text.js"></script>
<script>

var assert = chai.assert;
mocha.setup({ ui: 'tdd' });

(function() {
var pageError = null;

window.addEventListener('error', function(event) {
pageError = event.filename + ':' + event.lineno + ' ' + event.message;
});

window.addEventListener('load', function() {
if (pageError) {
suite('page-script-errors', function() {
test('no script errors on page', function() {
assert.fail(null, null, pageError);
});
});
}
mocha.run();
});
})();

</script>
</head>
<body>
<div id="mocha"></div>
<script src="suite.js"></script>
</body>
</html>
<!--
<!DOCTYPE html>
<html>
<head>
<script defer src="https://cdn.rawgit.com/mathiasbynens/utf8.js/5566334e/utf8.js"></script>
<script type="module">
</script>
Expand Down Expand Up @@ -66,4 +112,4 @@
</script>
</head>
</html>
</html> -->
5 changes: 3 additions & 2 deletions text.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2a0f2f9

Please sign in to comment.