Skip to content

Instantly share code, notes, and snippets.

@neduma
Created October 7, 2010 06:44
Show Gist options
  • Save neduma/614668 to your computer and use it in GitHub Desktop.
Save neduma/614668 to your computer and use it in GitHub Desktop.
javascript_augmentation.js
// to reverse word
String.prototype.reverse = function() {
var s = "";
var i = this.length;
while (i>0) {
s += this.substring(i-1,i);
i--;
}
return s;
}
// factory to return word processor object
var wordProcessor = function(initialData) {
var _public = {};;
_public.data = initialData;
// private
function trimAndCleanse(arr) {
var arr1 = new Array();
for (elem in arr) {
if (arr[elem] && /\w+/.test(arr[elem])) {
var err = arr[elem].trim();
arr1.push(err);
}
}
return arr1;
}
// return array of sentences
_public.getSentences = function() {
var arr = this.data.split(/\./);
return trimAndCleanse(arr);
}
// return array of reverse sentences
_public.getReverseSentences = function() {
var arr1 = new Array();
var arr = this.getSentences();
for (elem in arr) {
var re = arr[elem].split(/\b/).reverse().join("").trim();
arr1.push(re);
}
return arr1;
}
// return array of words
_public.getWords = function(limit) {
var arr = this.data.split(/\b/);
arr = trimAndCleanse(arr);
if(limit) {
return arr.splice(0, limit);
} else return arr;
}
// return array of reversed words
_public.getReversedWords = function(limit) {
var arr = this.getWords(limit);
var arr1 = new Array();
for (elem in arr) {
arr1.push(arr[elem].reverse());
}
return arr1;
}
// count words beginning with RegExp
_public.countWordsBeginningWith = function(regexp) {
var arr = this.getWords();
var count = 0;
var re = new RegExp(regexp, "i");
for (elem in arr) {
if ( re.test(arr[elem]) ) {
count++;
}
}
return count;
}
// sort
_public.sortWords = function(sortFunc) {
var arr = this.getWords();
if(sortFunc == "alpha") {
return arr.sort(this.alphaSort());
}
if (sortFunc == "length") {
return arr.sort(this.lengthSort());
}
}
_public.alphaSort = function() {
return function (a, b) {
return a > b ? 1 : -1;
};
}
_public.lengthSort= function() {
return function (a, b) {
return a.length > b.length ? 1 : -1;
};
}
// convert words to pig latin
_public.convertWordsToPigLatin = function() {
var arr = this.getWords();
var arr2 = new Array();
for (elem in arr) {
var text = arr[elem];
// Rule 1: any word starting with a consonant gets all the leading
// consonants moved to the end of the word and 'ay' appended
// Rule 2: any word starting with a vowel gets 'way' appended
text = text.replace(/\b([aeiou][a-z]*)\b/gi, "$1way"); // Rule 2
text = text.replace(/\b([bcdfghjklmnpqrstvwxy]+)([a-z]*)\b/gi, "$2$1ay"); // Rule 1
arr2.push(text);
}
return arr2;
}
return _public;
}
function assertIfNotEqual(a, b, mesg) {
if (a !== b) console.log(mesg);
}
// FIXTURE
var sampleData = 'My experience in the nature study area was full of surprises. First of all, many unexpected creatures crossed our path. For example, as soon as we left the parking area and entered the grassy path, a long snake slithered along the edge of the high grass and quickly disappeared.';
// ASSERTS
var wp = new wordProcessor(sampleData);
assertIfNotEqual(wp.getSentences().length, 3, "Wrong number of sentences by getSentences");
assertIfNotEqual(wp.getWords().length, 49, "Wrong number of words by getWords");
assertIfNotEqual(wp.countWordsBeginningWith("My"), 1, "Wrong count of countsWorkdsBeginningWith words");
assertIfNotEqual(wp.convertWordsToPigLatin()[0], "Myay", "Wrong Pig Lating word");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment