electron-data icon indicating copy to clipboard operation
electron-data copied to clipboard

some falsey values cause silent failure

Open mattlohkamp opened this issue 10 years ago • 3 comments

passing the following values to ElectronData.set result in no data being written to the json file:

  • 0 (number)
  • '' (blank string)
  • false (boolean)
  • null, undefined, NaN (javascript value-absence indicators)

Here's a code example:

let TestData = new ElectronData();
TestData.set('test1',0);
TestData.set('test2','');
TestData.set('test3',false);
TestData.set('test4',null);
TestData.set('test5',undefined);
TestData.set('test6',NaN);
TestData.save();

... which results in data.json containing a blank object: {}

Interestingly enough, the first three values will be recorded if they're passed as new instances of their base types:

let TestData = new ElectronData();
TestData.set('test1',new Number());
TestData.set('test2',new String());
TestData.set('test3',new Boolean());
TestData.save();

result: {"test1":0,"test2":"","test3":false}

mattlohkamp avatar Nov 12 '16 02:11 mattlohkamp

I can understand null, undefined, and NaN - the JSON spec doesn't support these special types - but numbers, strings, and booleans are all supported, and their respective falsy values ought to be written as expected. null, undefined, and NaN ought to fail not-so-silently.

mattlohkamp avatar Nov 12 '16 02:11 mattlohkamp

First of all: Thank you for notifying this issue. You are absolutely right, silent-failures are horrible.

I fixed this in v1.2.1.

FYI: null is a valid JSON value type. For an overview of supported value types, have a look at json.org

sydev avatar Nov 12 '16 11:11 sydev

ah shit, I should've updated first. thanks!

mattlohkamp avatar Nov 13 '16 01:11 mattlohkamp