File tree Expand file tree Collapse file tree 6 files changed +66
-14
lines changed
packages/util-utf8-browser Expand file tree Collapse file tree 6 files changed +66
-14
lines changed Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ beforeEach(() => {
31
31
32
32
describe ( 'fromUtf8' , ( ) => {
33
33
it ( 'should use the Encoding API if available' , ( ) => {
34
- TextEncoder = jest . fn ( ) as any ;
34
+ ( global as any ) . TextEncoder = jest . fn ( ) as any ;
35
35
36
36
fromUtf8 ( 'foo' ) ;
37
37
@@ -51,7 +51,7 @@ describe('fromUtf8', () => {
51
51
52
52
describe ( 'toUtf8' , ( ) => {
53
53
it ( 'should use the Encoding API if available' , ( ) => {
54
- TextDecoder = jest . fn ( ) as any ;
54
+ ( global as any ) . TextDecoder = jest . fn ( ) as any ;
55
55
56
56
toUtf8 ( new Uint8Array ( 0 ) ) ;
57
57
Original file line number Diff line number Diff line change 1
- import { fromUtf8 , toUtf8 } from '../lib/whatwgEncodingApi' ;
1
+ import {
2
+ fromUtf8 ,
3
+ toUtf8 ,
4
+ } from '../lib/whatwgEncodingApi' ;
2
5
3
6
beforeEach ( ( ) => {
4
7
const textDecoderInstance = {
@@ -8,10 +11,19 @@ beforeEach(() => {
8
11
encode : jest . fn ( ( ) => new Uint8Array ( 0 ) ) ,
9
12
} ;
10
13
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 ;
13
16
} ) ;
14
17
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
+
15
27
describe ( 'WHATWG encoding spec compliant environment UTF-8 handling' , ( ) => {
16
28
it ( 'should use the global TextDecoder to decode UTF-8' , ( ) => {
17
29
const decoder = new TextDecoder ( ) ;
Original file line number Diff line number Diff line change 7
7
toUtf8 as textEncoderToUtf8 ,
8
8
} from './lib/whatwgEncodingApi' ;
9
9
10
+ declare const TextDecoder : Function | undefined ;
11
+ declare const TextEncoder : Function | undefined ;
12
+
10
13
export function fromUtf8 ( input : string ) : Uint8Array {
11
14
if ( typeof TextEncoder === 'function' ) {
12
15
return textEncoderFromUtf8 ( input ) ;
Original file line number Diff line number Diff line change
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
+
1
44
export function fromUtf8 ( input : string ) : Uint8Array {
2
- return new TextEncoder ( 'utf-8' ) . encode ( input ) ;
45
+ return new TextEncoder ( ) . encode ( input ) ;
3
46
}
4
47
5
48
export function toUtf8 ( input : Uint8Array ) : string {
Original file line number Diff line number Diff line change 14
14
"devDependencies" : {
15
15
"@types/jest" : " ^19.2.2" ,
16
16
"@types/node" : " ^7.0.12" ,
17
- "@types/text-encoding" : " 0.0.30" ,
18
17
"jest" : " ^19.0.2" ,
19
18
"typescript" : " ^2.3"
20
- },
21
- "jest" : {
22
- "globals" : {
23
- "TextDecoder" : true ,
24
- "TextEncoder" : true
25
- }
26
19
}
27
20
}
Original file line number Diff line number Diff line change 4
4
"module" : " commonjs" ,
5
5
"declaration" : true ,
6
6
"sourceMap" : true ,
7
- "strict" : true
7
+ "strict" : true ,
8
+ "stripInternal" : true
8
9
}
9
10
}
You can’t perform that action at this time.
0 commit comments