Javascript Gerundization
- May 22, 2009 10:57 AM
- Javascript
- Comments (0)
Ever needed to "gerudize" a word? No? I needed to today to convert links and button labels like "Save" and "Submit" into their gerund equivalents: "Saving" and "Submitting". I found a set of rules on how to convert a verb into a noun here, and put together a simple Javascript implementation. I might do a ColdFusion one later to match my Pluralize() and Singularize() utility functions.
function gerundize (word) {
if ( word.match( /[^aeiou]e$/i ) ) {
word = word.slice(0, word.length-1);
} else if ( word.match( /[^aeiou][aeiou][^aeiou]$/i ) ) {
word = word + word.slice(word.length-1, word.length);
}
return word + 'ing';
}
if ( word.match( /[^aeiou]e$/i ) ) {
word = word.slice(0, word.length-1);
} else if ( word.match( /[^aeiou][aeiou][^aeiou]$/i ) ) {
word = word + word.slice(word.length-1, word.length);
}
return word + 'ing';
}
And the test code:
document.write(gerundize('Think'));
document.write('<br />');
document.write(gerundize('Submit'));
document.write('<br />');
document.write(gerundize('Hit'));
document.write('<br />');
document.write(gerundize('Take'));
document.write('<br />');
document.write(gerundize('Create'));
document.write('<br />');
document.write(gerundize('Save'));
document.write('<br />');
document.write('<br />');
document.write(gerundize('Submit'));
document.write('<br />');
document.write(gerundize('Hit'));
document.write('<br />');
document.write(gerundize('Take'));
document.write('<br />');
document.write(gerundize('Create'));
document.write('<br />');
document.write(gerundize('Save'));
document.write('<br />');