dweomer
v1.1.1
Published
DWIM-er for turning boring old string values into what you actually meant.
Downloads
5
Maintainers
Readme
dweomer
DWIM-er for turning boring old string values into what you actually meant.
Let's say you have a hash of former query string variables, or a load of environment variables, or a lot of command line arguments. A big, flat hash.
All your values are strings. But some of those values, you don't mean for them to be strings. But JavaScript will make them all gross. The string 'false' evaluates as true. 'null' isn't null. It's a pain in the butt to make these simple transformations.
Presto-chango-dweomer.
var dweomer = require('dweomer')
, env
;
process.env.MAN_I_WISH_THIS_WAS_FALSE = 'false';
process.env.SOME_PEOPLE_ACTUALLY_DO_THIS = '{ "myJson": "has fleas" }';
env = dweomer(process.env);
env.MAN_I_WISH_THIS_WAS_FALSE; // false
env.SOME_PEOPLE_ACTUALLY_DO_THIS.myJson; // 'has fleas'
Default conversions include:
- 'true', 'TRUE', 'trUe' -> true
- 'false', 'FALSE', 'falsE' -> false
- '1234' -> 1234
- '077' -> 63 // octal
- '0xfff' -> 4095 // hex
- 'null', 'NULL' -> null
- 'UNDEfined' -> undefined
- '{"this": "that"}' -> { this: 'that' } // JSON
- 'anything else' -> 'anything else' // leave it alone
And special stuff can be made to happen with the options argument:
var options = {}
;
process.env.RANDOM_VALUE = '{{rando!}}';
process.env.LOG_TO = 'process.stdout';
// If a value looks like dot notation, peek in global to see if
// it matches something.
options.scopes = [ global ];
// If we see one of these keys as a value, run this as a special case.
options.specialCases =
{ '{{rando!}}': function (value, options) {
return Math.random();
}
};
env = dweomer(process.env, options);
env.RANDOM_VALUE; // 0.23223
env.LOG_TO; // process.stdout