spudly.shuoink.com

the best way to predict the future is to implement it

Entries Comments


Category: JavaScript

Date.add(), Date.subtract(), & Date.truncate()

4 July, 2008 (07:36) | JavaScript | 1 comment

If you’ve ever worked extensively with dates in javascript, you may have missed methods like these. These methods allow you to add or subtract time from a Date object. Truncate forces your date object into a specified precision. Here’s some examples:
var d = new Date();
//=> Fri Jul 04 2008 07:48:53 GMT-0600 (MDT)
d.add( “years”, 1 );
//=> [...]

assert for javascript

1 July, 2008 (20:26) | JavaScript | No comments

I was looking through a C++ book the other day and realized that javascript doesn’t have any functionality (that I know of at least) to perform assertions.
An assertion allows you to check in your script for things that should never happen, and throw an error if the thing that wasn’t supposed to happen happened. In [...]

String.interpolate()

19 June, 2008 (21:39) | JavaScript | 1 comment

Here’s a string prototype that allows you to interpolate variables into a string (ruby style). All you have to do is create your template and call interpolate on it ( with an object literal as a parameter for the vars to interpolate ).
Example:
var template = “The #{foxAdj}, #{foxAdj2} fox jumped over the #{dogAdj} dog.”;

// alert [...]

Array.difference();

31 May, 2008 (17:00) | JavaScript | 2 comments

Here’s a prototyped method for Array that computes the symmetric difference between two arrays. It basically returns all the values that are in one or the other array but not both.
Usage:
var setA = [ 1, 2, 3, 4, 5, 6 ];
var setB = [ 4, 5, 6, 7, 8, 9, 10 ];
var diff = setA.difference( [...]

Array.complement();

31 May, 2008 (16:51) | JavaScript | No comments

Heres a method to get the relative complement of one array to another. Thus, it will return an array of everything that is in the current array but not in the array you pass in.
Usage:
var setA = [ 1, 2, 3 ];
var setB = [ 3, 4, 5 ];
var setC = setA.complement( setB ); // [...]

Array.union();

31 May, 2008 (15:36) | JavaScript | No comments

Here’s another set theory prototype for the Array object in JavaScript. This one performs a union of the two arrays.
Usage;
var setA = [ 1, 2, 3, 4, 5, 6 ];
var setB = [ 4, 5, 6, 7, 8 ];
var union = setA.union( setB ); // Now union is [ 1, 2, 3, 4, 5, 6, [...]

Array.intersection()

31 May, 2008 (14:48) | JavaScript | 1 comment

I’m sure a lot of you know what set theory is if you’re reading a programming blog. Anyway, a big part of set theory is the concept of an intersection between two sets of data. This is essentially the parts of the sets that the two have in common. Here’s an prototype for the intersection [...]

Array.grep

31 May, 2008 (14:38) | JavaScript | No comments

Here’s another prototype for the javascript Array class of objects: Array.grep(). This takes either a regular expression object or any other kind of value that can be compared with ===. If a regex is given, it will test the regex against each value. If something else is given, it will simply compare them with ===. [...]

Array.unique();

31 May, 2008 (09:07) | JavaScript | 2 comments

Here’s a prototype method for the Array object that allows you to get the unique values in an array!
Array.prototype.unique = function() {

var seen = {};
var unique = [];

for ( var i = 0; i < this.length; i++ ) {
if ( [...]

Using the XML DOM Without Writing 15,0000 Lines of Code

20 February, 2008 (22:17) | JavaScript | 16 comments

I’m a pretty big fan of using the XML DOM rather than innerHTML, simply because it seems to me to be better programming style. I hate hate hate, however, that it takes 15 lines of code to create one node. Today, I came up with an idea to solve the problem. I created a wrapper [...]

« Older entries