Skip to content
Gary Storey
TwitterGithub

Simple JavaScript functions

JavaScript, Simplate1 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}
6
7var cc = camelCase('data-my-attribute'); //dataMyAttribute
8
9//****************************************************
10
11function 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}
18
19var arr=[ /* An array of 'objects' */
20 { "firstname":"Gary", "lastname":"Storey" },
21 { "firstname":"John", "lastname":"Doe" },
22 { "firstname":"Jane", "lastname":"Doe" }
23];
24
25var lastnames = pluck(arr,'lastname'); // ["Storey","Doe","Doe"]
26
27//****************************************************
28function toProperCase( str ) {
29 return str.toLowerCase().replace(/^(.)|\s(.)/g, function ($1) {
30 return $1.toUpperCase();
31 });
32};
33
34var str = toProperCase('gary STOREY'); // Gary Storey
35
36//****************************************************
37
38function 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}
46
47var is_mobile = !!(viewport().width < 768);
48
49//****************************************************
50
51function nth( o ) {
52 return o+(['st','nd','rd'][(o+'').match(/1?\d\b/)-1]||'th');
53}
54
55var placement = 10;
56while (placement--) {
57 console.log(nth( placement );
58}
59// 10th, 9th, 8th,...,4th, 3rd, 2nd, 1st
60
61//****************************************************
62
63// logic-less templating
64function build(template, map) {
65 return template.replace(/{([a-z_]+[a-z0-9_]*)}/gi, function(tag, name) {
66 return map[name] ? map[name] : '';
67 });
68}
69
70var str = build( 'Hi! My name is {name}.', {'name' : 'Gary'} );
71console.log( str );
72// Hi! My name is Gary.
73
74//****************************************************
75
76function markFound(sSource, sSearchFor) {
77 var regex = new RegExp('('+sSearchFor+')', 'gi');
78 return sSource.replace(regex, '<mark>$1</mark>');
79}
80
81var str ='Hi! My name is Gary.';
82str = markfound(str,'Gary');
83// Hi! My name is <mark>Gary</mark>.
84
85//****************************************************

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;
3
4// Example Usage
5document.addEventListener('DOMContentLoaded', function() {
6 $('body')[0].on('click', myEvent, false);
7 function myEvent () {
8 console.log(this);
9 }
10}, false);
11
12//****************************************************
13
14// Nodelist to array
15var nodelist = document.querySelectorAll('div');
16var nodesArray = [].slice.call( nodelist );
17
18//****************************************************

Hope you find these useful!

'Til next time,

-G

© 2023 by Gary Storey. All rights reserved.