Specifically, I took the WeakMap API and redefined it for a multi-dimensional hashtable. I think my Concepts wiki page has a pretty good explanation of the problem I’m trying to solve:
Consider a HTML table:
9 2 3 8 1 4 7 6 5
The pattern behind this table’s layout is pretty obvious to us in two dimensions: the center has 1, and as you move in a spiral, you increment the value assigned to each cell. But if you didn’t know the pattern, or were simply storing data (as this article does), how would you do it?
The typical answer is to define a two-dimensional array – more specifically, an array of arrays. This is in fact precisely what HTML does. Observe the markup for the above table:<table border="1" style="width: 200px"> <caption>Table 1</caption> <tbody> <tr> <td>9</td> <td>2</td> <td>3</td> </tr> <tr> <td>8</td> <td>1</td> <td>4</td> </tr> <tr> <td>7</td> <td>6</td> <td>5</td> </tr> </tbody> </table>
This long string of characters serializes a two-dimensional array as a HTML table. This works fine when I need to store only two dimensions of data. But where might I put the number 10, if in neither an adjacent row nor an adjacent column? An adjacent floor, perhaps – a third dimension?
There’s a key assumption here. The assumption is that our data structure already has all the dimensions it will need. To add another dimension, another degree of freedom, you’d have to rewrite the data structure entirely. A HTML table is clearly not capable of handling three dimensions – nor do I mean to suggest it should. Here, it’s just an example.
The DimensionalMap library is supposed to give its users the ability to store data in its space, validate the keys (coordinates) being passed in, and add new dimensions to its space.
I also wrote a quick-and-dirty mockup of the Document Object Model to test DimensionalMap against. I found four specific uses for DimensionalMap in the mockup:
- Supporting namespaced DOM attributes (xlink, anyone?)
- Supporting undo and redo operations
- Reverting all “uncommitted” changes when an exception is thrown
- Shadow or anonymous content hidden from the mainstream DOM
This is an “alpha” release for two reasons:
- DimensionalMap depends on the Map and Proxy features of ECMAScript Harmony, which only Mozilla “Aurora” and Google Chrome “Dev” builds have.
- None of this code or documentation has been reviewed by anyone yet. I’m posting now because it’s time to ask for those reviews. Certainly I want to make it useful for others, not just me.
I’ve spent several months working on this, spare-time, as infrastructure for my Verbosio project. (It’s still not dead yet!) I chose the mockups test deliberately to see what challenges I would face in building my XML templates markup model – which I’ll be happy to explain to anyone interested, but it still doesn’t work.
Unlike other works of mine in the Mozilla community, this one is entirely web-safe, using to the best of my knowledge ECMAScript-compliant and Harmony-compliant code only.
As always, your feedback is most welcome!