Check the original posting on StackOverflow for the full description.
© Copyright Bruce M. Axtens, 2018
Past (20 years or so) and present code. A variety of languages and platforms. Some gems. More gravel. Some useful stuff and some examples of how not to do it.
String.prototype.toProperCase = function () { return this.replace(/\w\S*/g, function (txt) { return String(txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()); }); };I found this on StackOverflow. It attempts to proper-case (title-case) every word in a string. I don't use it often but it was an interesting find.
String.prototype.after = function (a, including) { including = including || false; a = this.indexOf(a); return -1 === a ? String(this) : String(this.substr(a + (including ? 0 : 1))); };A staple, this gives whatever is after a substring in a string. The substring can be included or excluded for the result.
String.prototype.before = function (a, including) { including = including || false; a = this.indexOf(a); return -1 === a ? String(this) : String(this.substr(0, a + (including ? 1 : 0))); };Another staple, this gives whatever is before a substring in a string. Again, the substring can be included or excluded for the result.
String.prototype.stripChars = function (chars, cnt) { var i, j; cnt = cnt || 0; var answer = this; for (i = 0; i < chars.length; i++) { var chr = chars.charAt(i); if (cnt === 0) { var pat = new RegExp(chr, "g"); answer = answer.replace(pat, ""); } else { for (j = 0; j < cnt; j++) { answer = answer.replace(chr, ""); } } } return String(answer); };I don't use this often, but it has been useful. It removes each element of chars from the string. If cnt is undefined or zero, all are removed. If cnt is 1 or more, then that many are removed.
String.prototype.between = function (begin, end, including) { including = including || false; var left = this.indexOf(begin); if (left < 0) { return String(''); } left += begin.length; var right = typeof end === 'undefined' ? this.length : this.indexOf(end, left); if (right < 0) { return String(''); } return including ? String(begin + this.substring(left, right) + end) : String(this.substring(left, right)); };There are various ways of pulling SGML and other markups apart. This one, in various languages, has been in the toolkit for years. This variant permits excluding or including the delimiters. Apart from that, it isn't smart -- "drooling singing".between("droo","ing") gives l
String.prototype.endswith = function (str) { return this.indexOf(str) !== -1 && this.indexOf(str) === this.length - str.length; };Returns true if the strings ends with contents of str otherwise false. Has helped with detecting filetypes in filenames.
String.prototype.toSqlString = function () { return String("'" + String(this).replace(/'/g, "''") + "'"); };This puts a single quote at the start and end of the string and doubles any embedded single quotes. I use this in combination with .questionMarkIs() to format SQL statements.
String.prototype.toArrayOfLines = function () { return this.split(/\r\n|\r|\n/g); };Splitting a text file to lines is something I do a lot of. This is the functional variant of code I have in almost every script written over the last 7 years.
String.prototype.questionMarkIs = function (val) { return String(this.replace("?", val)); };For a while I was doing all kinds of weird things to build SQL queries. Lately I've taken to simply putting question marks in and then replacing them with the value (with or without .toSqlString()).
dhall is a programmable configuration language that is not Turing-complete. You can think of Dhall as: JSON + functions + types + importsI can think of some places where this approach to config would be helpful and less dangerous that what I am currently doing.