miktam/sizeof

sizeof can return NAN

Closed this issue · 4 comments

In Stats.js If obj passed into size(obj) is null then you end up with NAN as a returned value. add a default return to the bottom of the function.

function size(obj) {
if (_.isString(obj)) {
return obj.length * ECMA_SIZE.STRING;
}

if (_.isBoolean(obj)) {
    return ECMA_SIZE.BOOLEAN;
}

if (_.isNumber(obj)) {
    return ECMA_SIZE.NUMBER;
}
return 0; // <<< here

}

@michaelbushby if type is not recognised, default value is returned (0), check here: https://github.com/avrora/sizeof/blob/master/index.js#L42

Do you have the latest version of the library?

Added the test to check that null returns 0:
https://github.com/avrora/sizeof/blob/master/test/test.js#L10

Sorry I wasn't very clear try this example.

var _ = require('lodash');
var sizeof = require('object-sizeof');

var badData = {"1":{"depot_id":null,"hierarchy_node_id":null}};

var foo = sizeof(badData);
if(_.isNaN(foo)) {
console.log("I Am NAN");
console.log(JSON.stringify(badData));
} else {
console.log("I Am OK");
}

Fixed in #2