Comparing version 9.7.0 to 9.7.1
/** | ||
* Module dependencies | ||
*/ | ||
var _ = require('lodash'); | ||
/** | ||
* Given an RTTC "display type" aka "typeclass" string, | ||
@@ -18,5 +25,19 @@ * return the appropriate human-readable label for that type. | ||
* | ||
* @optional {Dictionary} options | ||
* | ||
* @property {Boolean} plural | ||
* If enabled, the returned display type will be plural. | ||
* @default false | ||
* | ||
* @property {String} capitalization | ||
* One of: | ||
* • "title" (e.g. "JSON-Compatible Value", or "String") | ||
* • "start" (e.g. "JSON-compatible value", or "String") | ||
* • "fragment" (e.g. "JSON-compatible value", or "string") | ||
* @default "title" | ||
* | ||
* | ||
* @return {String} | ||
*/ | ||
module.exports = function getDisplayTypeLabel(type){ | ||
module.exports = function getDisplayTypeLabel(type, options){ | ||
if (typeof type !== 'string') { | ||
@@ -30,25 +51,70 @@ throw new Error('Usage error: rttc.getDisplayTypeLabel() expects a string display type such as '+ | ||
// Set up defaults | ||
options = options || {}; | ||
options = _.defaults(options, { | ||
plural: false, | ||
capitalization: 'title' | ||
}); | ||
if (!_.contains(['title', 'start', 'fragment'], options.capitalization)) { | ||
throw new Error('Usage error: Unrecognized `capitalization` option: `'+options.capitalization+'`. '+ | ||
'Should be either "title", "start", or "fragment". (defaults to "title")'); | ||
} | ||
if (type === 'string') { | ||
return 'String'; | ||
switch (options.capitalization) { | ||
case 'title': | ||
case 'start': return !options.plural ? 'String' : 'Strings'; | ||
case 'fragment': return !options.plural ? 'string' : 'strings'; | ||
} | ||
} | ||
else if (type === 'number') { | ||
return 'Number'; | ||
switch (options.capitalization) { | ||
case 'title': | ||
case 'start': return !options.plural ? 'Number' : 'Numbers'; | ||
case 'fragment': return !options.plural ? 'number' : 'numbers'; | ||
} | ||
} | ||
else if (type === 'boolean') { | ||
return 'Boolean'; | ||
switch (options.capitalization) { | ||
case 'title': | ||
case 'start': return !options.plural ? 'Boolean' : 'Booleans'; | ||
case 'fragment': return !options.plural ? 'boolean' : 'booleans'; | ||
} | ||
} | ||
else if (type === 'lamda') { | ||
return 'Function'; | ||
switch (options.capitalization) { | ||
case 'title': | ||
case 'start': return !options.plural ? 'Function' : 'Functions'; | ||
case 'fragment': return !options.plural ? 'function' : 'functions'; | ||
} | ||
} | ||
else if (type === 'dictionary') { | ||
return 'Dictionary'; | ||
switch (options.capitalization) { | ||
case 'title': | ||
case 'start': return !options.plural ? 'Dictionary' : 'Dictionaries'; | ||
case 'fragment': return !options.plural ? 'dictionary' : 'dictionaries'; | ||
} | ||
} | ||
else if (type === 'array') { | ||
return 'Array'; | ||
switch (options.capitalization) { | ||
case 'title': | ||
case 'start': return !options.plural ? 'Array' : 'Arrays'; | ||
case 'fragment': return !options.plural ? 'array' : 'arrays'; | ||
} | ||
} | ||
else if (type === 'json') { | ||
return 'JSON-Compatible Value'; | ||
switch (options.capitalization) { | ||
case 'title': return !options.plural ? 'JSON-Compatible Value' : 'JSON-Compatible Values'; | ||
case 'start': | ||
case 'fragment': return !options.plural ? 'JSON-compatible value' : 'JSON-compatible values'; | ||
} | ||
} | ||
else if (type === 'ref') { | ||
return 'Anything'; | ||
switch (options.capitalization) { | ||
case 'title': return !options.plural ? 'Anything' : 'Any Values'; | ||
case 'start': return !options.plural ? 'Value of any type' : 'Values of any types'; | ||
case 'fragment': return !options.plural ? 'value of any type' : 'values of any types'; | ||
} | ||
} | ||
@@ -59,2 +125,5 @@ else { | ||
// (should never make it here!) | ||
throw new Error('Consistency violation: Could not get display type due to an internal error in RTTC.'); | ||
}; |
/** | ||
* Module dependencies | ||
*/ | ||
var _ = require('lodash'); | ||
var getDisplayTypeLabel = require('./get-display-type-label'); | ||
/** | ||
* getNounPhrase() | ||
@@ -19,5 +27,32 @@ * | ||
* | ||
* | ||
* @optional {Dictionary} options | ||
* | ||
* @property {Boolean} plural | ||
* If enabled, the returned noun phrase will be plural. | ||
* @default false | ||
* | ||
* @property {Boolean} completeSentence | ||
* If enabled, a complete sentence with a capital letter | ||
* & ending punctuation (a period) will be returned. | ||
* Otherwise (by default), the returned noun phrase will | ||
* be a fragment designed for use in an existing sentence. | ||
* @default false | ||
* | ||
* @property {String} determiner | ||
* One of: | ||
* • "the" (definite article) | ||
* • "a" (indefinite article) | ||
* • "any" (existential qualifier) | ||
* • "" (no determiner) | ||
* > Note that if "a" is specified, either "a" or "an" will be used, | ||
* > whichever is more appropriate. | ||
* > (for more background, see https://en.wikipedia.org/wiki/Article_(grammar) | ||
* > and/or https://en.wikipedia.org/wiki/English_determiners) | ||
* @default "a" (or "", if plural) | ||
* | ||
* | ||
* @return {String} [noun phrase] | ||
*/ | ||
module.exports = function getNounPhrase(type){ | ||
module.exports = function getNounPhrase(type, options){ | ||
if (typeof type !== 'string') { | ||
@@ -31,26 +66,103 @@ throw new Error('Usage error: rttc.getNounPhrase() expects a string display type such as '+ | ||
// Set up defaults | ||
options = options || {}; | ||
options = _.defaults(options, { | ||
plural: false, | ||
completeSentence: false, | ||
determiner: !options.plural ? 'a' : '' | ||
}); | ||
// Tolerate "an" for "a" | ||
if (options.determiner === 'an') { | ||
options.determiner = 'a'; | ||
} | ||
// Validate determiner | ||
if (!_.contains(['the', 'a', 'any', ''], options.determiner)) { | ||
throw new Error('Usage error: Unrecognized `determiner` option: `'+options.determiner+'`. '+ | ||
'Should be either "the", "a", "any", or "". (defaults to "a", or "" if plural)'); | ||
} | ||
// Ensure we're not trying to use "a" in a plural noun phrase. | ||
if (options.plural && options.determiner === 'a') { | ||
throw new Error('Usage error: Cannot use that determiner ("a") to generate a plural noun phrase. '+ | ||
'Trust me, it wouldn\'t sound right.'); | ||
} | ||
// Compute the display type label that will be used below. | ||
var displayTypeLabel = getDisplayTypeLabel(type, { | ||
capitalization: 'fragment', | ||
plural: options.plural | ||
}); | ||
// Determine the appropriate naked noun phrase. | ||
// (i.e. with determiner, but without ending punctuation or start-sentence capitalization) | ||
var nounPhrase; | ||
if (type === 'string') { | ||
return 'A string.'; | ||
switch (options.determiner) { | ||
case 'the': nounPhrase = 'the '+displayTypeLabel; break; | ||
case 'a': nounPhrase = 'a '+displayTypeLabel; break; | ||
case 'any': nounPhrase = 'any '+displayTypeLabel; break; | ||
case '': nounPhrase = displayTypeLabel; break; | ||
} | ||
} | ||
else if (type === 'number') { | ||
return 'A number.'; | ||
switch (options.determiner) { | ||
case 'the': nounPhrase = 'the '+displayTypeLabel; break; | ||
case 'a': nounPhrase = 'a '+displayTypeLabel; break; | ||
case 'any': nounPhrase = 'any '+displayTypeLabel; break; | ||
case '': nounPhrase = displayTypeLabel; break; | ||
} | ||
} | ||
else if (type === 'boolean') { | ||
return 'A boolean.'; | ||
switch (options.determiner) { | ||
case 'the': nounPhrase = 'the '+displayTypeLabel; break; | ||
case 'a': nounPhrase = 'a '+displayTypeLabel; break; | ||
case 'any': nounPhrase = 'any '+displayTypeLabel; break; | ||
case '': nounPhrase = displayTypeLabel; break; | ||
} | ||
} | ||
else if (type === 'lamda') { | ||
return 'A function.'; | ||
switch (options.determiner) { | ||
case 'the': nounPhrase = 'the '+displayTypeLabel; break; | ||
case 'a': nounPhrase = 'a '+displayTypeLabel; break; | ||
case 'any': nounPhrase = 'any '+displayTypeLabel; break; | ||
case '': nounPhrase = displayTypeLabel; break; | ||
} | ||
} | ||
else if (type === 'dictionary') { | ||
return 'A dictionary.'; | ||
switch (options.determiner) { | ||
case 'the': nounPhrase = 'the '+displayTypeLabel; break; | ||
case 'a': nounPhrase = 'a '+displayTypeLabel; break; | ||
case 'any': nounPhrase = 'any '+displayTypeLabel; break; | ||
case '': nounPhrase = displayTypeLabel; break; | ||
} | ||
} | ||
else if (type === 'array') { | ||
return 'An array.'; | ||
switch (options.determiner) { | ||
case 'the': nounPhrase = 'the '+displayTypeLabel; break; | ||
case 'a': nounPhrase = 'an '+displayTypeLabel; break; | ||
case 'any': nounPhrase = 'any '+displayTypeLabel; break; | ||
case '': nounPhrase = displayTypeLabel; break; | ||
} | ||
} | ||
else if (type === 'json') { | ||
return 'A JSON-compatible value.'; | ||
// might be a string, number, boolean, dictionary, array, or even null | ||
switch (options.determiner) { | ||
case 'the': nounPhrase = 'the '+displayTypeLabel; break; | ||
case 'a': nounPhrase = 'a '+displayTypeLabel; break; | ||
case 'any': nounPhrase = 'any '+displayTypeLabel; break; | ||
case '': nounPhrase = displayTypeLabel; break; | ||
} | ||
// for future reference, this is where we could do: | ||
// > "might be a string, number, boolean, dictionary, array, or even null" | ||
} | ||
else if (type === 'ref') { | ||
return 'A value of any type.'; | ||
switch (options.determiner) { | ||
case 'the': nounPhrase = 'the '+displayTypeLabel; break; | ||
case 'a': nounPhrase = 'a '+displayTypeLabel; break; | ||
case 'any': nounPhrase = 'any '+displayTypeLabel; break; | ||
case '': nounPhrase = displayTypeLabel; break; | ||
} | ||
} | ||
@@ -61,2 +173,12 @@ else { | ||
// Finally, deal with sentence capitalization and ending punctuation (if relevant). | ||
if (options.completeSentence) { | ||
nounPhrase = nounPhrase[0].toUpperCase() + nounPhrase.slice(1); | ||
nounPhrase += '.'; | ||
} | ||
// And return our noun phrase. | ||
return nounPhrase; | ||
}; | ||
{ | ||
"name": "rttc", | ||
"version": "9.7.0", | ||
"version": "9.7.1", | ||
"description": "Runtime type-checking for JavaScript.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
272416
5468