This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from dollarshaveclub/nodejs-text-encoding
Use TextEncoder and TextDecoder from node's util package
- Loading branch information
Showing
6 changed files
with
85 additions
and
86 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
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,40 @@ | ||
const textEncoding = require('../text-encoder.js') | ||
|
||
describe('text-encoder', () => { | ||
test('text decoder defaults to utf-8', () => { | ||
const decoder = new textEncoding.TextDecoder() | ||
expect(decoder.encoding).toEqual('utf-8') | ||
}) | ||
|
||
test('able to fetch encoding from text decoder', () => { | ||
const decoder = new textEncoding.TextDecoder('utf-8') | ||
expect(decoder.encoding).toEqual('utf-8') | ||
}) | ||
|
||
test('able to obtain ignoreBom from text decoder', () => { | ||
const decoder = new textEncoding.TextDecoder('utf-8', { ignoreBOM: false }) | ||
expect(decoder.encoding).toEqual('utf-8') | ||
expect(decoder.ignoreBOM).toEqual(false) | ||
|
||
const decoder2 = new textEncoding.TextDecoder('utf-8', { ignoreBOM: true }) | ||
expect(decoder2.encoding).toEqual('utf-8') | ||
expect(decoder2.ignoreBOM).toEqual(true) | ||
}) | ||
|
||
test('able to obtain fatal from text decoder', () => { | ||
const decoder = new textEncoding.TextDecoder('utf-8', { fatal: false }) | ||
expect(decoder.encoding).toEqual('utf-8') | ||
expect(decoder.fatal).toEqual(false) | ||
|
||
const decoder2 = new textEncoding.TextDecoder('utf-8', { fatal: true }) | ||
expect(decoder2.encoding).toEqual('utf-8') | ||
expect(decoder2.fatal).toEqual(true) | ||
}) | ||
|
||
test('text decoder throws on anything other than utf-8', () => { | ||
expect(() => { | ||
const decoder = new textEncoding.TextDecoder('ascii', { fatal: false }) | ||
console.log(decoder) // prevent linting error due to decoder not being used | ||
}).toThrow(new ReferenceError('TextDecoder only supports utf-8 encoding')) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,40 +1,14 @@ | ||
const textEncoding = require('text-encoding') | ||
const util = require('util') | ||
|
||
function CustomTextDecoder (label, options) { | ||
if (!(this instanceof CustomTextDecoder)) { | ||
throw TypeError('Called as a function. Did you forget \'new\'?') | ||
} | ||
this.decoder = new textEncoding.TextDecoder(label, options) | ||
return this | ||
} | ||
class TextDecoder extends util.TextDecoder { | ||
constructor () { | ||
if (arguments.length > 0 && arguments[0] !== 'utf-8') { | ||
throw new RangeError('TextDecoder only supports utf-8 encoding') | ||
} | ||
|
||
CustomTextDecoder.prototype.decode = function decode (input, options) { | ||
if (Object.prototype.toString.call(input) === '[object Uint8Array]') { | ||
const buffer = new ArrayBuffer(input.length) | ||
const view = new Uint8Array(buffer) | ||
view.set(input) | ||
return this.decoder.decode( | ||
view, | ||
options) | ||
super(...arguments) | ||
} | ||
|
||
return this.decoder.decode(input, options) | ||
} | ||
|
||
Object.defineProperty(CustomTextDecoder.prototype, 'encoding', { | ||
/** @this {CustomTextDecoder} */ | ||
get: function get () { return this.decoder.encoding }, | ||
}) | ||
|
||
Object.defineProperty(CustomTextDecoder.prototype, 'fatal', { | ||
/** @this {CustomTextDecoder} */ | ||
get: function get () { return this.decoder.fatal }, | ||
}) | ||
|
||
Object.defineProperty(CustomTextDecoder.prototype, 'ignoreBOM', { | ||
/** @this {CustomTextDecoder} */ | ||
get: function get () { return this.decoder.ignoreBOM }, | ||
}) | ||
|
||
module.exports.TextDecoder = CustomTextDecoder | ||
module.exports.TextEncoder = textEncoding.TextEncoder | ||
module.exports.TextDecoder = TextDecoder | ||
module.exports.TextEncoder = util.TextEncoder |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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