forked from samthor/fast-text-encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
148 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters