Skip to content

Commit 1d7afa1

Browse files
committed
Add TextEncoder/TextDecoder declaration
1 parent 172ec13 commit 1d7afa1

File tree

6 files changed

+66
-14
lines changed

6 files changed

+66
-14
lines changed

packages/util-utf8-browser/__tests__/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ beforeEach(() => {
3131

3232
describe('fromUtf8', () => {
3333
it('should use the Encoding API if available', () => {
34-
TextEncoder = jest.fn() as any;
34+
(global as any).TextEncoder = jest.fn() as any;
3535

3636
fromUtf8('foo');
3737

@@ -51,7 +51,7 @@ describe('fromUtf8', () => {
5151

5252
describe('toUtf8', () => {
5353
it('should use the Encoding API if available', () => {
54-
TextDecoder = jest.fn() as any;
54+
(global as any).TextDecoder = jest.fn() as any;
5555

5656
toUtf8(new Uint8Array(0));
5757

packages/util-utf8-browser/__tests__/whatwgEncodingApi.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import {fromUtf8, toUtf8} from '../lib/whatwgEncodingApi';
1+
import {
2+
fromUtf8,
3+
toUtf8,
4+
} from '../lib/whatwgEncodingApi';
25

36
beforeEach(() => {
47
const textDecoderInstance = {
@@ -8,10 +11,19 @@ beforeEach(() => {
811
encode: jest.fn(() => new Uint8Array(0)),
912
};
1013

11-
TextDecoder = jest.fn(() => textDecoderInstance) as any;
12-
TextEncoder = jest.fn(() => textEncoderInstance) as any;
14+
(global as any).TextDecoder = jest.fn(() => textDecoderInstance) as any;
15+
(global as any).TextEncoder = jest.fn(() => textEncoderInstance) as any;
1316
});
1417

18+
interface TextDecoderCtor {
19+
new (): any;
20+
}
21+
interface TextEncoderCtor {
22+
new (): any;
23+
}
24+
declare const TextDecoder: TextDecoderCtor;
25+
declare const TextEncoder: TextEncoderCtor;
26+
1527
describe('WHATWG encoding spec compliant environment UTF-8 handling', () => {
1628
it('should use the global TextDecoder to decode UTF-8', () => {
1729
const decoder = new TextDecoder();

packages/util-utf8-browser/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import {
77
toUtf8 as textEncoderToUtf8,
88
} from './lib/whatwgEncodingApi';
99

10+
declare const TextDecoder: Function|undefined;
11+
declare const TextEncoder: Function|undefined;
12+
1013
export function fromUtf8(input: string): Uint8Array {
1114
if (typeof TextEncoder === 'function') {
1215
return textEncoderFromUtf8(input);

packages/util-utf8-browser/lib/whatwgEncodingApi.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,48 @@
1+
/**
2+
* A declaration of the global TextEncoder and TextDecoder constructors.
3+
*
4+
* @see https://encoding.spec.whatwg.org/
5+
*/
6+
namespace Encoding {
7+
interface TextDecoderOptions {
8+
fatal?: boolean;
9+
ignoreBOM?: boolean;
10+
}
11+
12+
interface TextDecodeOptions {
13+
stream?: boolean;
14+
}
15+
16+
interface TextDecoder {
17+
readonly encoding: string;
18+
readonly fatal: boolean;
19+
readonly ignoreBOM: boolean;
20+
decode(
21+
input?: ArrayBuffer|ArrayBufferView,
22+
options?: TextDecodeOptions
23+
): string;
24+
}
25+
26+
export interface TextDecoderConstructor {
27+
new (label?: string, options?: TextDecoderOptions): TextDecoder;
28+
}
29+
30+
interface TextEncoder {
31+
readonly encoding: 'utf-8';
32+
encode(input?: string): Uint8Array;
33+
}
34+
35+
export interface TextEncoderConstructor {
36+
new (): TextEncoder;
37+
}
38+
}
39+
40+
declare const TextDecoder: Encoding.TextDecoderConstructor;
41+
42+
declare const TextEncoder: Encoding.TextEncoderConstructor;
43+
144
export function fromUtf8(input: string): Uint8Array {
2-
return new TextEncoder('utf-8').encode(input);
45+
return new TextEncoder().encode(input);
346
}
447

548
export function toUtf8(input: Uint8Array): string {

packages/util-utf8-browser/package.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,7 @@
1414
"devDependencies": {
1515
"@types/jest": "^19.2.2",
1616
"@types/node": "^7.0.12",
17-
"@types/text-encoding": "0.0.30",
1817
"jest": "^19.0.2",
1918
"typescript": "^2.3"
20-
},
21-
"jest": {
22-
"globals": {
23-
"TextDecoder": true,
24-
"TextEncoder": true
25-
}
2619
}
2720
}

packages/util-utf8-browser/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"module": "commonjs",
55
"declaration": true,
66
"sourceMap": true,
7-
"strict": true
7+
"strict": true,
8+
"stripInternal": true
89
}
910
}

0 commit comments

Comments
 (0)