Skip to content

Instantly share code, notes, and snippets.

@ohmyjersh
Created June 1, 2018 01:52
Show Gist options
  • Save ohmyjersh/3f71a04914e899b5bd8bbcf458d94be5 to your computer and use it in GitHub Desktop.
Save ohmyjersh/3f71a04914e899b5bd8bbcf458d94be5 to your computer and use it in GitHub Desktop.
/**
* @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