by Rick
24. August 2011 12:10
Similar to my previous post about extending JavaScrpt arrays to include a shuffle method... Any JavaScript object can be extended using the prototype object. The following line of code deomonstrates how to extend JavaScript string objects to include a LINQ style contains method. The function will return true if the string contains the input, or false, if it does not.
String.prototype.contains = function(input) {
return ~this.indexOf(input)
}
var rickLeitch = "Rick Leitch";
if(rickLeitch.contains("ck Le")) {
// Do Stuff
}
by Rick
19. August 2011 14:05
For one reason or another, I needed to randomly shuffle an array in JavaScript. Using the prototype object, one is able to add custom properties and methods to all instances of native, and custom, objects. The code below extends the Array object to include a shuffle function and demonstrates its usage.
Array.prototype.shuffle = function () {
var tmp = [];
while (this.length)
tmp.push(this.splice(Math.random() * this.length, 1));
while (tmp.length)
this.push(tmp.pop());
return this;
}
var array1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var array2 = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19];
array1.shuffle();
array2.shuffle();
I've encountered at least one knee jerk reaction that suggests... "DON'T MODIFY OBJECTS YOU DON'T OWN!?!" I tend to take advice like that with a grain of salt.
by Rick
12. August 2011 07:53
Depending on the level of convolutedness, one may, hopefully rarely, be required to search for all tables containing a particular column name. I found myself in this situation today. Refer to below T-SQL.
SELECT so.name
FROM sys.sysobjects AS so WITH(NOLOCK) INNER JOIN
sys.syscolumns AS sc WITH(NOLOCK) ON so.id = sc.id
WHERE (so.xtype = 'U') AND (sc.name = N'Column_Name')
ORDER BY so.name