Author: Barry van Oudtshoorn

  • Quicksort an array of objects

    Often, you will need to sort an array of objects in Javascript. The inbuilt sort() function can’t do this, but here is a Quicksort implementation for doing just this.

    Parameters

    array The array to be sorted. (See below for an implementation on the Array Native itself, which makes this variable unnecessary).

    key The key to sort by. Make sure every object in your array has this key.

    Examples

    [sourcecode language=’javascript’]
    var objs = [
    {fruit:”cherry”},
    {fruit:”apple”},
    {fruit:”banana”}
    ];

    console.log(objs.sortObjects(‘fruit’));
    // Logs [{fruit:”apple”},{fruit:”banana”},{fruit:”cherry”}] to the console
    [/sourcecode]

    The code

    [sourcecode language=’javascript’]
    sortObjects: function(array, key) {
    for (var i = 0; i < array.length; i++) { var currVal = array[i][key]; var currElem = array[i]; var j = i - 1; while ((j >= 0) && (array[j][key] > currVal)) {
    array[j + 1] = array[j];
    j–;
    }
    array[j + 1] = currElem;
    }
    }
    [/sourcecode]

    Implemented on the Array native:

    [sourcecode language=’javascript’]
    Array.implement({
    sortObjects: function(key) {
    for (var i = 0; i < this.length; i++) { var currVal = this[i][key]; var currElem = this[i]; var j = i - 1; while ((j >= 0) && (this[j][key] > currVal)) {
    this[j + 1] = this[j];
    j–;
    }
    this[j + 1] = currElem;
    }
    }
    });
    [/sourcecode]

  • Javascript string ellipsising

    Putting ellipses into strings that are too long has been around for a very long time. Unfortunately, Javascript doesn’t offer a native method of doing this, so below is a little function that’ll do it for you.

    This function returns a copy of the string it’s called on, ellipsised, and takes three parameters:

    toLength (required) The number of characters to truncate the string to (or 0 to disable ellipsising)

    where (optional, default ‘end’) A string representing where the ellipsis should be placed — ‘front’, ‘middle’, or ‘end’

    ellipsis (option, default ‘\u2026’) A string to be used as the ellipsis.

    Examples

    [sourcecode language=’javascript’]
    // Our clichéd string
    var s = ‘Jackdaws love my great big sphinx of quartz’;

    alert(s.ellipsise(10));
    // Alerts “Jackdaws l…”

    alert(s.ellipsise(10, ‘front’));
    // Alerts “… of quartz”

    alert(s.ellipsise(10, ‘middle’, ‘pony’));
    // Alerts “Jackdponyuartz”[/sourcecode]

    The code

    [sourcecode language=’javascript’]String.implement({
    ellipsise: function(toLength, where, ellipsis) { // Where is one of [‘front’,’middle’,’end’] — default is ‘end’
    if (toLength < 1) return this; ellipsis = ellipsis || '\u2026'; if (this.length < toLength) return this; switch (where) { case 'front': return ellipsis + this.substr(this.length - toLength); break; case 'middle': return this.substr(0, toLength / 2) + ellipsis + this.substr(this.length - toLength / 2) break; case 'end': default: return this.substr(0, toLength) + ellipsis; break; } } });[/sourcecode]If you're not using MooTools, you can use this variant instead:[sourcecode language='javascript']String.prototype.ellipsise = function(toLength, where, ellipsis) { // Where is one of ['front','middle','end'] -- default is 'end' if (toLength < 1) return this; ellipsis = ellipsis || '\u2026'; if (this.length < toLength) return this; switch (where) { case 'front': return ellipsis + this.substr(this.length - toLength); break; case 'middle': return this.substr(0, toLength / 2) + ellipsis + this.substr(this.length - toLength / 2) break; case 'end': default: return this.substr(0, toLength) + ellipsis; break; } }[/sourcecode]

  • Words I have invented

    Here are just two of the words that I have invented to fill gaps in the English language. Adopt, disseminate, and promulgate them!

    Spanxiety cf. spangst, spanxious

    The fear of being punished; trepidation at the thought of retribution for past transgressions.

    Little Jimmy, having been seen stealing the cookies, was now feeling quite spanxious.
    I suffered a bout of spanxiety at the thought of what she would do to me.


    Illigiterate cf. illigiteracy

    Children born out of wedlock who lack basic reading and writing skills.

    With the advent of widely-available contraceptives and education, illigiteracy has fallen in Australia.
    Illigiteracy Venn Diagram

  • Dauntless

    As you might have noticed, if you listen to much of my orchestral music, I really like writing epic, syncopated scores with great big horn melodies (and little flute ones) that float above rapid, percussive strings and brass. Well, here’s another example of that. 🙂

    This piece is a rondo in D minor. Let me know what you think!

    [audio:http://www.barryvan.com.au/music/Dauntless.mp3]

    Download

  • Camping: June 2009

    Well, Ariel and I have just returned from a weekend away at Dwellingup, camping. How was it? First and foremost, it was cold. Bitterly cold. Notwithstanding the sub-arctic conditions, however, it was absolutely fantastic.

    The Murray River is beautiful, and we were camped no more than fifty metres away from it. We camped in the “Stringers” area of the Lane Poole Reserve, and managed to wrangle probably the best camping site in the entire park — secluded, equipped with a fire pit, and quiet.

    As you can see from the photos below, our two primary concerns were a) exploring the banks of the river, and b) fire. When it’s so cold at noon that you can see your breath, a fire is a wonderful thing.

     

    (Click on an image for a larger view)

  • Songpack #1

    [Download archive]

    I’ve uploaded an archive of my music for easier download. This archive currently contains eighteen tracks, eight of which are no longer available anywhere else! The tracks included in this pack are:

    I may well create other ‘songpacks’ for download, so keep checking back. There’ll always be a couple of exclusive tracks in each pack, so it’ll be worth your while. I hope. 🙂

    Download all eighteen tracks now!

  • Clockwork

    This track is rather unusual for me; it doesn’t really fall into any of my usual styles. I personally find it quite relaxing, especially when I listen to it with headphones. Perhaps it’s a bit too relaxing: my wife had to go and have a nap after she heard the complete piece. 🙂

    Let me know what you think about it — any and all opinions are, as always, more than welcome.

    [audio:http://www.barryvan.com.au/music/Clockwork.mp3]

    Download

  • Avoid Javascript’s ‘with’ keyword

    Javascript is a fantastic language — in fact, it’s become the language that I do most of my programming in nowadays. It’s flexible, fast, and powerful. Unfortunately, though, it suffers from a few flaws, which, although not critical, can be frustrating. One of the potentially most confusing features is the with keyword, which promises a lot, but can really just make life difficult.

    The with keyword might appear to be harmless enough: it allows you to avoid typing long references; instead of
    [sourcecode language=’javascript’]ah.woom.ba.weh.lyric = ‘In the jungle’;[/sourcecode]
    we can type
    [sourcecode language=’javascript’]with (ah.woom.ba.weh) {
    lyric = ‘In the jungle’;
    }[/sourcecode]

    But what happens if we happen to have a variable in scope named lyric? In the example below, which lyric should be modified?
    [sourcecode language=’javascript’]var lyric = ‘In the jungle’;
    with (ah.woom.ba.weh) {
    lyric = ‘The mighty jungle’;
    }[/sourcecode]
    The simplest way to deal with this issue is to use a variable:
    [sourcecode language=’javascript’]var a = ah.woom.ba.weh;
    a.lyric = ‘The mighty jungle’;[/sourcecode]
    Now there is no ambiguity.

    Based on a post by Douglas Crockford at the YUI Blog.

  • Just to say

    I would die a thousand times,
    cross the deserts, tear the skies;
    oh I would do it all

    I would do it all for you
    just to say that I love you,
    that I love you.

    [audio:http://www.barryvan.com.au/music/JustToSay.mp3]

    Download

    Comments transferred from Trax In Space and Modplug Central.

  • Twelve Nineteen

    This one’s actually been sitting on my computer for a month or so now — I just haven’t been able to render it to wave, because my system just wasn’t fast enough. Today, though, I got a new motherboard, CPU, and RAM, and everything just purrs along, barely touching the processor. Very Happy

    Anyway, about the music itself. This piece is basically me having fun with Kontakt — each of the eight instruments has fairly complex insert effect chains, a couple of send effects, and a couple of modulated group inserts, too. Smile Basically, lots of effects, for some nice sound degradation.

    Because I’m still not a fan of OpenMPT’s automation handling, I managed to find a nice little workaround, that I used on a couple of instruments: I modulate various effects using the MIDI volume. This means that working with the modulation is much more immediate, and it also means that I can modulate a couple of different effects simultaneously completely independently.

    The piece is, hopefully, quite fun to listen to — it was definitely fun to write. Let me know what you think!

    [audio:http://www.barryvan.com.au/music/TwelveNineteen.mp3]

    Comments transferred from Trax In Space and Modplug Central.

  • The amazing Regret Index

    Ryan North, of Dinosaur Comics fame, has crafted an awesome little webapp: The Regret Index. Essentially, you vote on whether or not you regret certain things. You can even add your own regrets or search for regrets.

    Most of them aren’t particularly serious, and a lot of them are kinda fun. It’s worth having a browse through the archive of regrets, looking at the votes, and reading the comments people have left. Some of my favourite ‘regrets’ on the site include

    • Eating a kitten just to prove you’re evil
    • Supergluing your foot to the bathroom floor
    • Constantly fearing regret
    • Starting to think all these questions are addressing you specifically

    Check it out — it’s well worth a look.

  • Internet Explorer DOMDocument & XPath

    I discovered a couple of interesting things about Internet Explorer’s MSXML2.DOMDocument object. It turns out that there are essentially two “production-quality” versions of it available: 3.0 and 6.0. Version 6.0 is much the better version, but it’s quite new, and not available on all systems. This means that in IE7 and IE8, instantiating a new MSXML2.DOMDocument object gives you version 3.0.

    Now most of the time, this isn’t a problem. Today, though, I was constructing an XPath expression that used the substring() function; something like this:

    [sourcecode language=’xml’]//Element[substring(Child, 1, 3)=’abc’][/sourcecode]

    This will pull all Elements with a Child element whose value’s first three characters are “abc”. Not particularly complex. It turns out, though, that version 3.0 or the DOMDocument doesn’t actually use XPath as its default language: it uses a bastardised version of XPath called “XSLPatterns”, which just so happens to not support the substring() function at all.

    So how do we deal with this situation? One way is to always instantiate version 6.0 of the DOMDocument:

    [sourcecode language=’javascript’]xd = new ActiveXObject(‘msxml2.DOMDocument.6.0′)[/sourcecode]

    The problem with this approach is that, like I mentioned earlier, you can’t always be guaranteed that your users will have version 6.0 installed (even though it’s a free download). The safer way to deal with this problem is to switch the expression language to XPath in your 3.0 object:

    [sourcecode language=’javascript’]xd = new ActiveXObject(‘msxml2.DOMDocument’);
    xd.setProperty(“SelectionLanguage”, “XPath”);[/sourcecode]

    The advantage of this approach is that you’re not specifying a version, so when MS eventually changes the default to 7.0 (or whatever), your code will work without a problem.

    For more information on this, check out  this blog post from Microsoft’s XML team, which goes into a little bit more detail.