Skip to content

Commit 2768123

Browse files
Fix encode() behavior when called with falsy arguments. Resolves inexorabletash#58
1 parent db1ed7c commit 2768123

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/encoding.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ if (typeof module !== "undefined" && module.exports) {
12681268
* @return {!Uint8Array} Encoded bytes, as a Uint8Array.
12691269
*/
12701270
TextEncoder.prototype.encode = function encode(opt_string, options) {
1271-
opt_string = opt_string ? String(opt_string) : '';
1271+
opt_string = opt_string === undefined ? '' : String(opt_string);
12721272
options = ToDictionary(options);
12731273

12741274
// NOTE: This option is nonstandard. None of the encodings

test/test-misc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,9 @@ test(function() {
379379

380380
}, 'NONSTANDARD - ' + encoding + ' (encoding)');
381381
});
382+
383+
test(function() {
384+
var encoder = new TextEncoder();
385+
assert_array_equals([].slice.call(encoder.encode(false)), [102, 97, 108, 115, 101]);
386+
assert_array_equals([].slice.call(encoder.encode(0)), [48]);
387+
}, 'encode() called with falsy arguments (polyfill bindings)');

0 commit comments

Comments
 (0)