Blog

JavaScript Snippet – Undo Camel Case

Probably due to it being so late, I was looking for code to uncamelize (undo camel-casing) any string. I came across what claimed to be a solution in PHP but unfortunately did nothing but lowercased my string. Therefore I decided to write my own solution: function uncamelize(s) { return s.replace(/[A-Z]/g, Read more…

By Chris West, ago
Blog

EcmaScript 6 – String.prototype.repeat()

One simple function that is on the roster to be released in EcmaScript 6 is String.prototype.repeat. The following can be used to define it in browsers which don’t currently have it defined natively: String.prototype.repeat = String.prototype.repeat || function(count) { return Array(count >= 0 ? parseInt(count, 10) + 1 : -1).join(this); Read more…

By Chris West, ago
Blog

JavaScript – Indenting Text

One thing I needed to do recently was indent a series of strings with a variable number of tabs or spaces. Thusly, I decided to create the following function which can be used by anyone that would need such a function: /** * Indents the given string * @param {string} Read more…

By Chris West, ago
Blog

JavaScript – String.prototype.split Fix

In this post I proved that the String.prototype.split function can behave differently in IE8 and below than it does in other browsers. For that reason, I wrote the following code that will fix the issue so that split should work the same in all browsers: (function(split, toString, slice) { String.prototype.split Read more…

By Chris West, ago
Blog

JavaScript – Faking Here Document

Nearly a year ago I wrote a quick post about the awesome heredoc notation for strings in many languages such as PHP: $query = <<<QUERY SELECT * FROM table WHERE col_1 = val_1 AND col_2 = val_2; QUERY; Unfortunately, this doesn’t exist in JavaScript. If for some reason you do Read more…

By Chris West, ago