Skip to content

Commit b7db14e

Browse files
committed
fix: wrong validating function within parseTextDecoder()
1 parent 9d7ac1b commit b7db14e

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

src/lib/helper.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function parseTextDecoder(textDecoder?: TextDecoderFn): TextDecoderFn {
4343
? textDecoder
4444
: (typeof TextDecoder === 'function' ? TextDecoder : null)
4545

46-
validateEncoder(Decoder)
46+
validateDecoder(Decoder)
4747
return <TextDecoderFn> Decoder
4848
}
4949

@@ -55,6 +55,13 @@ export function validateEncoder(input: any): void {
5555
}
5656
}
5757

58+
/** Throw error if input be null */
59+
export function validateDecoder(input: any): void {
60+
if (input === null) {
61+
throw new TypeError(ErrorMsg.textDecoderUndefined)
62+
}
63+
}
64+
5865

5966
/** Whether string contains valid base64 characters */
6067
export function validB64Chars(input: string): boolean {

test/30_helper.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ import {
66
} from '@waiting/shared-core'
77
import * as assert from 'power-assert'
88
import rewire = require('rewire')
9+
import {
10+
TextDecoder as NodeTextDecoder,
11+
TextEncoder as NodeTextEncoder,
12+
} from 'util'
913

1014
import { ErrorMsg } from '../src/index'
1115
import {
1216
parseDecodeInputBase64,
1317
parseEncodeInputString,
18+
parseTextDecoder,
19+
parseTextEncoder,
1420
validateEncoder,
1521
} from '../src/lib/helper'
1622

@@ -74,4 +80,59 @@ describe(filename, () => {
7480
})
7581
})
7682

83+
84+
describe('parseTextEncoder() works', () => {
85+
it('with valid input', () => {
86+
const ret = parseTextEncoder(NodeTextEncoder)
87+
assert(ret === NodeTextEncoder)
88+
})
89+
90+
it('with invalid input', () => {
91+
try {
92+
// @ts-ignore
93+
const ret = parseTextEncoder(null)
94+
// node.js v11+ exports global TextEncoder
95+
if (typeof TextEncoder === 'function') {
96+
assert(true)
97+
}
98+
else {
99+
assert(false, 'Should throw error, but NOT')
100+
}
101+
}
102+
catch (ex) {
103+
assert(
104+
ex.message && ex.message.includes(ErrorMsg.textEncoderUndefined),
105+
ex.message,
106+
)
107+
}
108+
})
109+
})
110+
111+
describe('parseTextDecoder() works', () => {
112+
it('with valid input', () => {
113+
const ret = parseTextDecoder(NodeTextDecoder)
114+
assert(ret === NodeTextDecoder)
115+
})
116+
117+
it('with invalid input', () => {
118+
try {
119+
// @ts-ignore
120+
const ret = parseTextDecoder(null)
121+
// node.js v11+ exports global TextDecoder
122+
if (typeof TextDecoder === 'function') {
123+
assert(true)
124+
}
125+
else {
126+
assert(false, 'Should throw error, but NOT')
127+
}
128+
}
129+
catch (ex) {
130+
assert(
131+
ex.message && ex.message.includes(ErrorMsg.textDecoderUndefined),
132+
ex.message,
133+
)
134+
}
135+
})
136+
})
137+
77138
})

0 commit comments

Comments
 (0)