dominictarr/rc

How best to parse non-strings?

Closed this issue · 5 comments

I understand that .ini files and ENV variables do not have types for values, but it is quite a pain having to check for ===false and ==='false' all over the place, for example. If a boolean in set in the default configuration passed on initialisation,for example, then it can expected to be a boolean, but in case it comes from anywhere else then you have to account for strings.

This behaviour can lead to annoying bugs.

Is there any sensible way to enforce parsing numbers and booleans properly? Is using a custom parser the best way? How then does one do this (no examples in docs that I can see)?

what I would recommend is writing a module that takes a js object, and replaces any boolean string values with propper booleans then passing your whole config to it.

var config = require('parse-booleans')(require('rc')('appname', defaults))

Ah, OK, so this module will just return an object with the strings replaced? Makes sense and pretty simple to do.

Wrote a node module that does exactly this: https://www.npmjs.com/package/parse-strings-in-object

if you like, make a PR to put a link in the readme, I guess in the section about ini format would be best?

Thanks!

Will do, once I've tested this a bit in my own projects and feel confident it's performing reliably.