Simple JavaScript functions

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:

function camelCase( s ) {
  return s.replace(/\-(\w)/g, function( i, m ){
    return m.toUpperCase();
  });
}

var cc = camelCase('data-my-attribute'); //dataMyAttribute

//****************************************************

function pluck(array, property) {
  var plucked = [];
  for (var i = 0; i < array.length; ++i) {
    plucked.push(array[i][property]);
  }
  return plucked;
}

var arr=[   /* An array of 'objects' */
  { "firstname":"Gary", "lastname":"Storey" },
  { "firstname":"John", "lastname":"Doe" },
  { "firstname":"Jane", "lastname":"Doe" }
];

var lastnames = pluck(arr,'lastname');  // ["Storey","Doe","Doe"]

//****************************************************
function toProperCase( str ) { 
  return str.toLowerCase().replace(/^(.)|\s(.)/g, function ($1) { 
    return $1.toUpperCase(); 
  });
};    

var str = toProperCase('gary STOREY');  // Gary Storey

//****************************************************

function viewport() {
  var e = window, a = 'inner';
  if ( !( 'innerWidth' in window ) ) {
    a = 'client';
    e = document.documentElement || document.body;
  }
  return { width : e[ a+'Width' ] , height : e[ a+'Height' ] }
}

var is_mobile = !!(viewport().width < 768);  

//****************************************************

function nth( o ) {
  return o+(['st','nd','rd'][(o+'').match(/1?\d\b/)-1]||'th');
}

var placement = 10;
while (placement--) {
  console.log(nth( placement );
}
// 10th, 9th, 8th,...,4th, 3rd, 2nd, 1st

//****************************************************

// logic-less templating 
function build(template, map) {
  return template.replace(/{([a-z_]+[a-z0-9_]*)}/gi, function(tag, name) {
    return map[name] ? map[name]  : '';
  });
}

var str = build( 'Hi! My name is {name}.', {'name' : 'Gary'} );
console.log( str );
// Hi! My name is Gary.

//****************************************************

function markFound(sSource, sSearchFor) {
  var regex = new RegExp('('+sSearchFor+')', 'gi');
  return sSource.replace(regex, '<mark>$1</mark>');
}

var str ='Hi! My name is Gary.';
str = markfound(str,'Gary');
// Hi! My name is <mark>Gary</mark>.

//****************************************************

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:

var $ = document.querySelectorAll.bind(document);
Element.prototype.on = Element.prototype.addEventListener;

// Example Usage   
document.addEventListener('DOMContentLoaded', function() {
   $('body')[0].on('click', myEvent, false);
  function myEvent () {
     console.log(this);
  }
}, false);

//****************************************************

// Nodelist to array
var nodelist = document.querySelectorAll('div');
var nodesArray = [].slice.call( nodelist );

//****************************************************    

Hope you find these useful!
'Til next time,
-G