— JavaScript, Simplate — 1 min read
Over time, I have collected a bunch of simple JavaScript functions that I find useful. I decided it was time I posted some of these scripts here. Each of the functions will be followed by a simple example of how to use it. So, without further ado, here they are:
1function camelCase( s ) {2 return s.replace(/\-(\w)/g, function( i, m ){3 return m.toUpperCase();4 });5}67var cc = camelCase('data-my-attribute'); //dataMyAttribute89//****************************************************1011function pluck(array, property) {12 var plucked = [];13 for (var i = 0; i < array.length; ++i) {14 plucked.push(array[i][property]);15 }16 return plucked;17}1819var arr=[ /* An array of 'objects' */20 { "firstname":"Gary", "lastname":"Storey" },21 { "firstname":"John", "lastname":"Doe" },22 { "firstname":"Jane", "lastname":"Doe" }23];2425var lastnames = pluck(arr,'lastname'); // ["Storey","Doe","Doe"]2627//****************************************************28function toProperCase( str ) {29 return str.toLowerCase().replace(/^(.)|\s(.)/g, function ($1) {30 return $1.toUpperCase();31 });32};3334var str = toProperCase('gary STOREY'); // Gary Storey3536//****************************************************3738function viewport() {39 var e = window, a = 'inner';40 if ( !( 'innerWidth' in window ) ) {41 a = 'client';42 e = document.documentElement || document.body;43 }44 return { width : e[ a+'Width' ] , height : e[ a+'Height' ] }45}4647var is_mobile = !!(viewport().width < 768);4849//****************************************************5051function nth( o ) {52 return o+(['st','nd','rd'][(o+'').match(/1?\d\b/)-1]||'th');53}5455var placement = 10;56while (placement--) {57 console.log(nth( placement );58}59// 10th, 9th, 8th,...,4th, 3rd, 2nd, 1st6061//****************************************************6263// logic-less templating64function build(template, map) {65 return template.replace(/{([a-z_]+[a-z0-9_]*)}/gi, function(tag, name) {66 return map[name] ? map[name] : '';67 });68}6970var str = build( 'Hi! My name is {name}.', {'name' : 'Gary'} );71console.log( str );72// Hi! My name is Gary.7374//****************************************************7576function markFound(sSource, sSearchFor) {77 var regex = new RegExp('('+sSearchFor+')', 'gi');78 return sSource.replace(regex, '<mark>$1</mark>');79}8081var str ='Hi! My name is Gary.';82str = markfound(str,'Gary');83// Hi! My name is <mark>Gary</mark>.8485//****************************************************
I will continue to post more of these as time permits.
Here a couple of JavaScript "one-liners" that might also help speed up development:
1var $ = document.querySelectorAll.bind(document);2Element.prototype.on = Element.prototype.addEventListener;34// Example Usage5document.addEventListener('DOMContentLoaded', function() {6 $('body')[0].on('click', myEvent, false);7 function myEvent () {8 console.log(this);9 }10}, false);1112//****************************************************1314// Nodelist to array15var nodelist = document.querySelectorAll('div');16var nodesArray = [].slice.call( nodelist );1718//****************************************************
Hope you find these useful!
'Til next time,
-G