Added default value retrieval to pkgBuilder.getValue
This will assist in building generator prompts that return either the existing value, or a sane default. Change-Id: I66f47c70ebc529d0a1832677a925083c663adc8c
This commit is contained in:
parent
36695dd8f8
commit
bc11aa0cb9
|
@ -47,16 +47,18 @@
|
|||
}
|
||||
|
||||
/**
|
||||
* Get a specific value from the package.json file.
|
||||
* Get a specific value from the package.json file, or a default if the
|
||||
* value is not set.
|
||||
*
|
||||
* @param {String} name The name of the value.
|
||||
* @param {String} defaultValue A default value to return.
|
||||
* @returns {{}} A clone of the referenced value.
|
||||
*/
|
||||
function getValue (name) {
|
||||
function getValue (name, defaultValue) {
|
||||
if (pkgContent.hasOwnProperty(name)) {
|
||||
return JSON.parse(JSON.stringify(pkgContent[name]));
|
||||
}
|
||||
return undefined;
|
||||
return defaultValue || undefined;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
@ -72,6 +72,13 @@
|
|||
builder.fromJSON('{"name":"foo"}');
|
||||
expect(builder.getValue('invalidname')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return a default if provided.',
|
||||
function () {
|
||||
builder.fromJSON('{"name":"foo"}');
|
||||
expect(builder.getValue('invalidname', 'defaultValue'))
|
||||
.toBe('defaultValue');
|
||||
});
|
||||
});
|
||||
});
|
||||
})();
|
||||
|
|
Loading…
Reference in New Issue