comfy
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "comfy", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Configure your applications comfortably", | ||
@@ -5,0 +5,0 @@ "main": "src/comfy.js", |
@@ -1,2 +0,2 @@ | ||
# comfy | ||
# comfy [![npm version](https://badge.fury.io/js/comfy.svg)](http://badge.fury.io/js/comfy) [![Circle CI](https://circleci.com/gh/filp/comfy.svg?style=svg)](https://circleci.com/gh/filp/comfy) | ||
@@ -20,2 +20,5 @@ ![](http://i.imgur.com/Eq6r1Q2.png) | ||
// Aliased property names: | ||
c.required("important_api_key", { alias: "some_service_key" }); | ||
// More complicated syntax if you're running low on your | ||
@@ -36,3 +39,7 @@ // LOC targets for this week: | ||
// If snake_case is your thing, you can also stick to that: | ||
console.log(config.sky_color) // => "red" | ||
console.log(config.sky_color); // => "red" | ||
// Name aliases work too: | ||
// process.env.IMPORTANT_API_KEY === "banana" | ||
console.log(config.someServiceKey); // => banana | ||
``` | ||
@@ -39,0 +46,0 @@ |
@@ -74,2 +74,4 @@ // comfy is good for your health, dummy | ||
Comfy.prototype.property = function (name, options) { | ||
var opts = this.withOptions(options); | ||
var envName = this.nameToEnvKey(name); | ||
@@ -79,4 +81,4 @@ var envValue = this.env[envName]; | ||
if (typeof envValue === "undefined") { | ||
if (options.optional) { | ||
envValue = options.defaultValue; | ||
if (opts.optional) { | ||
envValue = opts.defaultValue; | ||
} else { | ||
@@ -87,6 +89,10 @@ throw "Required property " + envName + " not present in env:" + this.env; | ||
if (options.transform) { | ||
envValue = options.transform.call(null, envValue); | ||
if (opts.transform) { | ||
envValue = opts.transform.call(null, envValue); | ||
} | ||
if (opts.alias) { | ||
name = opts.alias; | ||
} | ||
this.setProperty(name, envValue); | ||
@@ -143,3 +149,4 @@ }; | ||
optional: opts.optional || false, | ||
defaultValue: opts.defaultValue || undefined | ||
defaultValue: opts.defaultValue || undefined, | ||
alias: opts.alias || null | ||
}; | ||
@@ -146,0 +153,0 @@ }; |
@@ -16,8 +16,21 @@ "use strict"; | ||
c.required("required_prop"); | ||
c.optional("with_alias", "aliased_value", { alias: "aliased_name" }); | ||
}, env /* use the custom env object instead of process.env */); | ||
// Present required: | ||
assert.equal(config.requiredProp, "required_value"); | ||
assert.equal(config.required_prop, "required_value"); | ||
// Present optionals: | ||
assert.equal(config.optionalProp, "optional_value"); | ||
assert.equal(config.optional_prop, "optional_value"); | ||
// Missing optional with default value: | ||
assert.equal(config.nonExistantOptionalProp, "fallback_value"); | ||
// Aliasing | ||
assert.equal(config.aliasedName, "aliased_value"); | ||
assert.equal(config.aliased_name, "aliased_value"); | ||
console.log("OK!"); |
7598
162
54