Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/write-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function getWriteType(options) {
var bin = binarraybuffer ? bin_arraybuffer : bin_buffer;
var usemap = HAS_MAP && options && options.usemap;
var map = usemap ? map_to_map : obj_to_map;
var strip_undef = options && options.strip_undef;
Copy link

@AArnott AArnott Jun 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about calling it omit_undefined_properties?
undef is needlessly terse, and by spelling out the "properties" part, we're clear that it doesn't remove them from array elements.

Suggested change
var strip_undef = options && options.strip_undef;
var strip_undef = options && options.omit_undefined_properties;


var writeType = {
"boolean": bool,
Expand Down Expand Up @@ -229,6 +230,12 @@ function getWriteType(options) {
// map 32 -- 0xdf
function obj_to_map(encoder, value) {
var keys = Object.keys(value);

if (strip_undef) {
// skip encoding keys if their value is undefined
keys = keys.filter(function(key) { return value[key] !== undefined; });
}

var length = keys.length;
var type = (length < 16) ? (0x80 + length) : (length <= 0xFFFF) ? 0xde : 0xdf;
token[type](encoder, length);
Expand Down