To Generate UUID
function generateUUID() { var d = new Date().getTime(); var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); return uuid; }
Sample output : 48808c28-24e0-4081-be13-2c27948bfa27
Extract Sentences from Paragraph
function extractSentences(paragraph) { return paragraph.match(/[^\.!\?]+[\.!\?]+/g); //get sentences }
Produces an array of sentences from paragraph.
Sample output as executed in console:
var paragraph = "In that year, German inventor Karl Benz built the Benz Patent-Motorwagen.. Electric cars, which were invented early in the history of the car, began to become commercially available in 2008.. His inventions were, however, handicapped by problems with water supply and maintaining steam pressure.. Coincidentally, in 1807 the Swiss inventor François Isaac de Rivaz designed his own 'de Rivaz internal combustion engine' and used it to develop the world's first vehicle to be powered by such an engine.. In November 1881, French inventor Gustave Trouvé demonstrated a working three-wheeled car powered by electricity at the International Exposition of Electricity, Paris.. Although several other German engineers (including Gottlieb Daimler, Wilhelm Maybach, and Siegfried Marcus) were working on the problem at about the same time, Karl Benz generally is acknowledged as the inventor of the modern car.. Many of his other inventions made the use of the internal combustion engine feasible for powering a vehicle.. He was awarded the patent for its invention as of his application on 29 January 1886 (under the auspices of his major company, Benz & Cie.. In August 1888 Bertha Benz, the wife of Karl Benz, undertook the first road trip by car, to prove the road-worthiness of her husband's invention.. Since the car was first invented, its controls have become fewer and simpler through automation"; undefined extractSentences(paragraph); ["In that year, German inventor Karl Benz built the Benz Patent-Motorwagen..", " Electric cars, which were invented early in the history of the car, began to become commercially available in 2008..", " His inventions were, however, handicapped by problems with water supply and maintaining steam pressure..", " Coincidentally, in 1807 the Swiss inventor François Isaac de Rivaz designed his own 'de Rivaz internal combustion engine' and used it to develop the world's first vehicle to be powered by such an engine..", " In November 1881, French inventor Gustave Trouvé demonstrated a working three-wheeled car powered by electricity at the International Exposition of Electricity, Paris..", " Although several other German engineers (including Gottlieb Daimler, Wilhelm Maybach, and Siegfried Marcus) were working on the problem at about the same time, Karl Benz generally is acknowledged as the inventor of the modern car..", " Many of his other inventions made the use of the internal combustion engine feasible for powering a vehicle..", " He was awarded the patent for its invention as of his application on 29 January 1886 (under the auspices of his major company, Benz & Cie..", " In August 1888 Bertha Benz, the wife of Karl Benz, undertook the first road trip by car, to prove the road-worthiness of her husband's invention.."]
Generate N-Grams from the Sentences
/** * The method return ngrams for array of words passed * @param {integer} n - the size of each ngram * @param {array of strings} array_of_words [words as array] * @return {array of strings} [ngrams array] */ function ngrams(n, array_of_words) { var nGrams, index, tindex = 0; nGrams = []; if (array_of_words === null || array_of_words === undefined) { return nGrams; } index = array_of_words.length - n + 1; if (index < 1) { return nGrams; } while (index--) { nGrams[index] = ''; var upindex = tindex + n; for (var i = tindex; i < upindex; i++) { nGrams[index] += array_of_words[i] + ' '; } nGrams[index] = nGrams[index].trim(); tindex++; } return nGrams; }
Sample output as executed in console:
ngrams(2,["age", "of" , "empires"]); ["of empires", "age of"] ngrams(1,["age", "of" , "empires"]); ["empires", "of", "age"] ngrams(3,["age", "of" , "empires"]); ["age of empires"]
Javascript endsWith and startsWith methods
String.prototype.endsWith = function(suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; }; String.prototype.startsWith = function(searchString, position) { position = position || 0; return this.substr(position, searchString.length) === searchString; }
Javascript code to generat random number between a given limit
function randomIntFromInterval(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); }
Regex for Email Id checking
var REGEX_EMAIL = '([a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)';
Sort an array of objects
function compare(a,b) { if (a.last_nom < b.last_nom) return -1; else if (a.last_nom > b.last_nom) return 1; else return 0; } objs.sort(compare);
I will keep updating this page with more utilities and snippets