{"id":2637,"date":"2012-01-05T21:43:07","date_gmt":"2012-01-06T02:43:07","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=2637"},"modified":"2012-01-05T21:43:07","modified_gmt":"2012-01-06T02:43:07","slug":"javascript-capitalize-text-like-in-this-title","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/javascript-capitalize-text-like-in-this-title\/","title":{"rendered":"Javascript: Capitalize Text Like In This Title"},"content":{"rendered":"<p>On this one, we&#8217;ll show of the dynamic nature of javascript and we&#8217;re going to make all strings have a new method called &#8220;capitalize()&#8221;.<\/p>\n<p>So you can do stuff like this:<br \/>\n[javascript]<br \/>\nconsole.log(&quot;this text should look nicer now&quot;.capitalize());<br \/>\n&gt;&gt; This Text Should Look Nicer Now<br \/>\n[\/javascript]<\/p>\n<p>Here&#8217;s the magic code you&#8217;ll need to add somewhere on your javascript files and all strings will have a new capitalize() method.<\/p>\n<p>[javascript]<br \/>\nString.prototype.capitalize = function(){<br \/>\n\t   return this.replace( \/(^|s)([a-z])\/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );<br \/>\n};<br \/>\n[\/javascript] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>On this one, we&#8217;ll show of the dynamic nature of javascript and we&#8217;re going to make all strings have a new method called &#8220;capitalize()&#8221;. So you can do stuff like this: [javascript] console.log(&quot;this text should look nicer now&quot;.capitalize()); &gt;&gt; This Text Should Look Nicer Now [\/javascript] Here&#8217;s the magic code you&#8217;ll need to add somewhere [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[],"class_list":["post-2637","post","type-post","status-publish","format-standard","hentry","category-code"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-Gx","jetpack-related-posts":[{"id":2233,"url":"https:\/\/www.gubatron.com\/blog\/twitterautolinks-jquery-extension-to-replace-twitter-names-for-links-to-their-respective-pages\/","url_meta":{"origin":2637,"position":0},"title":"twitterAutoLinks: jQuery extension to replace twitter @names for links to their respective pages.","author":"gubatron","date":"August 2, 2011","format":false,"excerpt":"Save the following on a .js file, preferably named jquery.twitterAutoLinks.js [javascript] \/** Auto replace all Twitter nicknames for links *\/ $.fn.twitterAutoLinks = function() { return this.each(function() { var html = $(this).html(); $(this).html(html.replace(\/B@([w-]+)\/gm, '<a href=\"http:\/\/twitter.com\/$1\" target=\"_blank\">@$1<\/a>')); }); }; [\/javascript] Usage After importing the jquery.twitterAutoLinks.js file (make sure it's after you've imported\u2026","rel":"","context":"In &quot;AJAX&quot;","block_context":{"text":"AJAX","link":"https:\/\/www.gubatron.com\/blog\/category\/ajax\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2632,"url":"https:\/\/www.gubatron.com\/blog\/javascript-get-n-random-elements-from-a-list\/","url_meta":{"origin":2637,"position":1},"title":"javascript: Get N random elements from a List","author":"gubatron","date":"January 5, 2012","format":false,"excerpt":"[javascript] \/** * Walks linearly through the list to find an element. * returns true if it's found. *\/ function elementIn(collection, element) { for (var i=0; i < collection.length; i++) { if (collection[i]==element) { return true; } } return false; } \/** * Returns a new list of n random\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3,"url":"https:\/\/www.gubatron.com\/blog\/javascript-quicksort-implementation-with-dynamic-comparator\/","url_meta":{"origin":2637,"position":2},"title":"Javascript Quicksort implementation with dynamic comparator.","author":"gubatron","date":"February 29, 2012","format":false,"excerpt":"[javascript] Array.prototype.swap=function(a, b) { var tmp=this[a]; this[a]=this[b]; this[b]=tmp; } function quickSort(array,comparator) { qsort(array,0,array.length,comparator); } \/** * NOTE: the comparator is a dynamic function you will define like so comparator(a,b) { if (a > b) return 1; else if (a < b) return -1; else { return 0; } } *\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2634,"url":"https:\/\/www.gubatron.com\/blog\/javascript-format-a-number-to-display-thousands-us-style\/","url_meta":{"origin":2637,"position":3},"title":"javascript: Format a number to display thousands US style (#,###,###,###)","author":"gubatron","date":"January 5, 2012","format":false,"excerpt":"[javascript] \/** * Format a number to display thousands like in the US -> 1000000 => 1,000,000 * @param number * @returns *\/ function formatThousands(number) { return Math.max(0, number).toFixed(0).replace(\/(?=(?:d{3})+$)(?!^)\/g, ','); } [\/javascript]","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2671,"url":"https:\/\/www.gubatron.com\/blog\/how-to-shuffle-a-listarray-in-javascript\/","url_meta":{"origin":2637,"position":4},"title":"How to shuffle a List (or Array) in Javascript","author":"gubatron","date":"February 24, 2012","format":false,"excerpt":"[javascript] \/** * Returns number starting from offset up to n-1 *\/ function getRandomWithOffset(n,offset) { return Math.floor(Math.random()*n+offset); } \/** * Returns random integer from 0 to n-1 *\/ function getRandom(n) { return getRandomWithOffset(n,0); } \/** Fisher\u2013Yates shuffle algorithm O(n) *\/ function shuffleList(list) { for (var i=list.length-1; i>0; i--) { var\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":402,"url":"https:\/\/www.gubatron.com\/blog\/an-useful-example-on-how-to-extend-javascript\/","url_meta":{"origin":2637,"position":5},"title":"An useful example on how to extend Javascript","author":"gubatron","date":"November 29, 2006","format":false,"excerpt":"Today I found out I could extend the functionality of __EXISTING__ html classes with javascript. I was manipulating DOM objects, better known as HTMLElement objects, and I needed a way to print the HTML that represents the tag of the object, not what's contained by the tags... a friend told\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=2637"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2637\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=2637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=2637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=2637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}