@@ -32,6 +32,21 @@ function lazyBuffer() {
32
32
return Buffer ;
33
33
}
34
34
35
+ function validateEncoder ( obj ) {
36
+ if ( obj == null || obj [ kEncoder ] !== true )
37
+ throw new errors . TypeError ( 'ERR_INVALID_THIS' , 'TextEncoder' ) ;
38
+ }
39
+
40
+ function validateDecoder ( obj ) {
41
+ if ( obj == null || obj [ kDecoder ] !== true )
42
+ throw new errors . TypeError ( 'ERR_INVALID_THIS' , 'TextDecoder' ) ;
43
+ }
44
+
45
+ function validateArgument ( prop , expected , propName , expectedName ) {
46
+ if ( typeof prop !== expected )
47
+ throw new errors . Error ( 'ERR_INVALID_ARG_TYPE' , propName , expectedName ) ;
48
+ }
49
+
35
50
const CONVERTER_FLAGS_FLUSH = 0x1 ;
36
51
const CONVERTER_FLAGS_FATAL = 0x2 ;
37
52
const CONVERTER_FLAGS_IGNORE_BOM = 0x4 ;
@@ -288,20 +303,17 @@ class TextEncoder {
288
303
}
289
304
290
305
get encoding ( ) {
291
- if ( this == null || this [ kEncoder ] !== true )
292
- throw new errors . TypeError ( 'ERR_INVALID_THIS' , 'TextEncoder' ) ;
306
+ validateEncoder ( this ) ;
293
307
return 'utf-8' ;
294
308
}
295
309
296
310
encode ( input = '' ) {
297
- if ( this == null || this [ kEncoder ] !== true )
298
- throw new errors . TypeError ( 'ERR_INVALID_THIS' , 'TextEncoder' ) ;
311
+ validateEncoder ( this ) ;
299
312
return encodeUtf8String ( `${ input } ` ) ;
300
313
}
301
314
302
315
[ inspect ] ( depth , opts ) {
303
- if ( this == null || this [ kEncoder ] !== true )
304
- throw new errors . TypeError ( 'ERR_INVALID_THIS' , 'TextEncoder' ) ;
316
+ validateEncoder ( this ) ;
305
317
if ( typeof depth === 'number' && depth < 0 )
306
318
return opts . stylize ( '[Object]' , 'special' ) ;
307
319
var ctor = getConstructorOf ( this ) ;
@@ -329,8 +341,7 @@ const { hasConverter, TextDecoder } =
329
341
makeTextDecoderJS ( ) ;
330
342
331
343
function hasTextDecoder ( encoding = 'utf-8' ) {
332
- if ( typeof encoding !== 'string' )
333
- throw new errors . Error ( 'ERR_INVALID_ARG_TYPE' , 'encoding' , 'string' ) ;
344
+ validateArgument ( encoding , 'string' , 'encoding' , 'string' ) ;
334
345
return hasConverter ( getEncodingFromLabel ( encoding ) ) ;
335
346
}
336
347
@@ -344,8 +355,7 @@ function makeTextDecoderICU() {
344
355
class TextDecoder {
345
356
constructor ( encoding = 'utf-8' , options = { } ) {
346
357
encoding = `${ encoding } ` ;
347
- if ( typeof options !== 'object' )
348
- throw new errors . Error ( 'ERR_INVALID_ARG_TYPE' , 'options' , 'Object' ) ;
358
+ validateArgument ( options , 'object' , 'options' , 'Object' ) ;
349
359
350
360
const enc = getEncodingFromLabel ( encoding ) ;
351
361
if ( enc === undefined )
@@ -369,17 +379,14 @@ function makeTextDecoderICU() {
369
379
370
380
371
381
decode ( input = empty , options = { } ) {
372
- if ( this == null || this [ kDecoder ] !== true )
373
- throw new errors . TypeError ( 'ERR_INVALID_THIS' , 'TextDecoder' ) ;
382
+ validateDecoder ( this ) ;
374
383
if ( isArrayBuffer ( input ) ) {
375
384
input = lazyBuffer ( ) . from ( input ) ;
376
385
} else if ( ! isArrayBufferView ( input ) ) {
377
386
throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'input' ,
378
387
[ 'ArrayBuffer' , 'ArrayBufferView' ] ) ;
379
388
}
380
- if ( typeof options !== 'object' ) {
381
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'options' , 'Object' ) ;
382
- }
389
+ validateArgument ( options , 'object' , 'options' , 'Object' ) ;
383
390
384
391
var flags = 0 ;
385
392
if ( options !== null )
@@ -416,8 +423,7 @@ function makeTextDecoderJS() {
416
423
class TextDecoder {
417
424
constructor ( encoding = 'utf-8' , options = { } ) {
418
425
encoding = `${ encoding } ` ;
419
- if ( typeof options !== 'object' )
420
- throw new errors . Error ( 'ERR_INVALID_ARG_TYPE' , 'options' , 'Object' ) ;
426
+ validateArgument ( options , 'object' , 'options' , 'Object' ) ;
421
427
422
428
const enc = getEncodingFromLabel ( encoding ) ;
423
429
if ( enc === undefined || ! hasConverter ( enc ) )
@@ -440,8 +446,7 @@ function makeTextDecoderJS() {
440
446
}
441
447
442
448
decode ( input = empty , options = { } ) {
443
- if ( this == null || this [ kDecoder ] !== true )
444
- throw new errors . TypeError ( 'ERR_INVALID_THIS' , 'TextDecoder' ) ;
449
+ validateDecoder ( this ) ;
445
450
if ( isArrayBuffer ( input ) ) {
446
451
input = lazyBuffer ( ) . from ( input ) ;
447
452
} else if ( isArrayBufferView ( input ) ) {
@@ -451,9 +456,7 @@ function makeTextDecoderJS() {
451
456
throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'input' ,
452
457
[ 'ArrayBuffer' , 'ArrayBufferView' ] ) ;
453
458
}
454
- if ( typeof options !== 'object' ) {
455
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'options' , 'Object' ) ;
456
- }
459
+ validateArgument ( options , 'object' , 'options' , 'Object' ) ;
457
460
458
461
if ( this [ kFlags ] & CONVERTER_FLAGS_FLUSH ) {
459
462
this [ kBOMSeen ] = false ;
@@ -496,27 +499,23 @@ function makeTextDecoderJS() {
496
499
TextDecoder . prototype ,
497
500
Object . getOwnPropertyDescriptors ( {
498
501
get encoding ( ) {
499
- if ( this == null || this [ kDecoder ] !== true )
500
- throw new errors . TypeError ( 'ERR_INVALID_THIS' , 'TextDecoder' ) ;
502
+ validateDecoder ( this ) ;
501
503
return this [ kEncoding ] ;
502
504
} ,
503
505
504
506
get fatal ( ) {
505
- if ( this == null || this [ kDecoder ] !== true )
506
- throw new errors . TypeError ( 'ERR_INVALID_THIS' , 'TextDecoder' ) ;
507
+ validateDecoder ( this ) ;
507
508
return ( this [ kFlags ] & CONVERTER_FLAGS_FATAL ) === CONVERTER_FLAGS_FATAL ;
508
509
} ,
509
510
510
511
get ignoreBOM ( ) {
511
- if ( this == null || this [ kDecoder ] !== true )
512
- throw new errors . TypeError ( 'ERR_INVALID_THIS' , 'TextDecoder' ) ;
512
+ validateDecoder ( this ) ;
513
513
return ( this [ kFlags ] & CONVERTER_FLAGS_IGNORE_BOM ) ===
514
514
CONVERTER_FLAGS_IGNORE_BOM ;
515
515
} ,
516
516
517
517
[ inspect ] ( depth , opts ) {
518
- if ( this == null || this [ kDecoder ] !== true )
519
- throw new errors . TypeError ( 'ERR_INVALID_THIS' , 'TextDecoder' ) ;
518
+ validateDecoder ( this ) ;
520
519
if ( typeof depth === 'number' && depth < 0 )
521
520
return opts . stylize ( '[Object]' , 'special' ) ;
522
521
var ctor = getConstructorOf ( this ) ;
0 commit comments