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:
Michael Krotscheck 2016-05-16 08:27:36 -07:00
parent 36695dd8f8
commit bc11aa0cb9
No known key found for this signature in database
GPG Key ID: 20E618D878DE38AB
2 changed files with 12 additions and 3 deletions

View File

@ -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 = {

View File

@ -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');
});
});
});
})();