{"id":158,"date":"2016-03-30T03:32:18","date_gmt":"2016-03-30T03:32:18","guid":{"rendered":"http:\/\/muthu.co\/?p=158"},"modified":"2021-01-02T14:06:03","modified_gmt":"2021-01-02T14:06:03","slug":"important-javascript-code-snippets","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/important-javascript-code-snippets\/","title":{"rendered":"Important Javascript Code snippets"},"content":{"rendered":"
function generateUUID() {\r\n var d = new Date().getTime();\r\n var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(\/[xy]\/g, function(c) {\r\n var r = (d + Math.random() * 16) % 16 | 0;\r\n d = Math.floor(d \/ 16);\r\n return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);\r\n });\r\n return uuid;\r\n }<\/pre>\nSample output :
48808c28-24e0-4081-be13-2c27948bfa27<\/code><\/p>\n
Extract Sentences from Paragraph<\/h1>\n
function extractSentences(paragraph) {\r\n return paragraph.match(\/[^\\.!\\?]+[\\.!\\?]+\/g); \/\/get sentences\r\n}<\/pre>\nProduces an array of sentences from paragraph.<\/p>\n
Sample output as executed in console:<\/p>\n
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\u00e7ois 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\u00e9 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\";\r\nundefined\r\n\r\nextractSentences(paragraph);\r\n\r\n[\"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\u00e7ois 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\u00e9 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..\"]<\/pre>\n<\/p>\n
Generate N-Grams from the Sentences<\/h1>\n
\/**\r\n * The method return ngrams for array of words passed\r\n * @param {integer} n - the size of each ngram\r\n * @param {array of strings} array_of_words [words as array]\r\n * @return {array of strings} [ngrams array]\r\n *\/\r\nfunction ngrams(n, array_of_words) {\r\n var nGrams,\r\n index, tindex = 0;\r\n\r\n nGrams = [];\r\n\r\n if (array_of_words === null || array_of_words === undefined) {\r\n return nGrams;\r\n }\r\n\r\n index = array_of_words.length - n + 1;\r\n\r\n if (index < 1) {\r\n return nGrams;\r\n }\r\n\r\n while (index--) {\r\n nGrams[index] = '';\r\n var upindex = tindex + n;\r\n for (var i = tindex; i < upindex; i++) {\r\n nGrams[index] += array_of_words[i] + ' ';\r\n }\r\n nGrams[index] = nGrams[index].trim();\r\n tindex++;\r\n }\r\n return nGrams;\r\n}<\/pre>\nSample output as executed in console:<\/p>\n
ngrams(2,[\"age\", \"of\" , \"empires\"]);\r\n[\"of empires\", \"age of\"]\r\n\r\nngrams(1,[\"age\", \"of\" , \"empires\"]);\r\n[\"empires\", \"of\", \"age\"]\r\n\r\nngrams(3,[\"age\", \"of\" , \"empires\"]);\r\n[\"age of empires\"]<\/pre>\nJavascript endsWith and startsWith methods<\/h1>\n
String.prototype.endsWith = function(suffix) {\r\n return this.indexOf(suffix, this.length - suffix.length) !== -1;\r\n};\r\n\r\nString.prototype.startsWith = function(searchString, position) {\r\n position = position || 0;\r\n return this.substr(position, searchString.length) === searchString;\r\n}<\/pre>\nJavascript code to generat random number between a given limit<\/h1>\n
function randomIntFromInterval(min, max) {\r\n return Math.floor(Math.random() * (max - min + 1) + min);\r\n }<\/pre>\nRegex for Email Id checking<\/h1>\n
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])?)';<\/pre>\nSort an\u00a0array of objects<\/h1>\n
function compare(a,b) {\r\n if (a.last_nom < b.last_nom)\r\n return -1;\r\n else if (a.last_nom > b.last_nom)\r\n return 1;\r\n else \r\n return 0;\r\n}\r\n\r\nobjs.sort(compare);<\/pre>\n<\/p>\n
<\/p>\n
I will keep updating this page with more utilities and snippets <\/p>\n","protected":false},"excerpt":{"rendered":"
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 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[52],"_links":{"self":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/158"}],"collection":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":1,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/158\/revisions"}],"predecessor-version":[{"id":1660,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/158\/revisions\/1660"}],"wp:attachment":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media?parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/categories?post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/tags?post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}