Created
June 1, 2018 01:52
-
-
Save ohmyjersh/3f71a04914e899b5bd8bbcf458d94be5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param context {WebtaskContext} | |
*/ | |
const errors = { | |
whenNoPhrase: 'Need to have phrase query string parameter', | |
whenNotEnoughWordsToMakeAnAcronym:'Need to have more than one word to make an acronym.' | |
} | |
const WORD_LIMIT_LENGTH = 2; | |
module.exports = function(context, cb) { | |
const phrase = context.query.phrase; | |
if(!phrase || /^\s*$/.test(phrase)) { | |
return cb(null, { error: errors.whenNoPhrase }); | |
} | |
const words = phrase.split(' '); | |
if(words.length < WORD_LIMIT_LENGTH) { | |
return cb(null, { error: errors.whenNotEnoughWordsToMakeAnAcronym }); | |
} | |
const acronym = words.reduce((acc, curr) => { | |
acc.push(curr[0]); | |
return acc; | |
},[]).join('').toUpperCase(); | |
return cb(null, { acronym }); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment