Archive for the 'Javascript' Category

JavaScript Number Formatting Library v1.3 released

Download Number Formatting Library

I’ve updated my JavaScript Number Formatting Library to version 1.3. This release adds the ability to customize how not-a-number (NaN), positive infinity and negative infinity are formatted. All you need to do is set the appropriate constant in Number.prototype:

  • Number.prototype.NaN
  • Number.prototype.posInfinity
  • Number.prototype.negInfinity

For more documentation, see the original article on JavaScript number formatting.

Technorati Tags:, , , , , ,

You might also like:

  1. How to format numbers in JavaScript flexibly and efficiently
  2. JavaScript number-formatting library updated
  3. Javascript date parsing and formatting, Part 2
  4. JavaScript formatting library update
  5. JavaScript date parsing and formatting, Part 1

JavaScript number-formatting library updated

Download number-functions

I’ve released a new version of my powerful, flexible, efficient JavaScript number-formatting library, which is probably the best available. This release adds a fix for zero-padding negative numbers.

If you find bugs, please send me test cases I can use to reproduce and add to the unit test suite. One test per line, like “input”, “format”, “expected” is best. For example, this is a great test case:

-1, "#,#.00", "-1.00"

I can plug that directly into the unit test suite, run it, and if it gives back “-01.00″ it will fail the test. This makes it much easier and more convenient for me to fix bugs.

Sponsoring bug fixes wouldn’t hurt either ;-)

Technorati Tags:, , , , , , , ,

You might also like:

  1. JavaScript Number Formatting Library v1.3 released
  2. JavaScript formatting library update
  3. How to format numbers in JavaScript flexibly and efficiently
  4. Javascript date parsing and formatting, Part 2
  5. JavaScript date formatting benchmarks

How to create input masks in HTML

Download HTML Input Mask

Note that this is not compatible with all browsers, has known problems and limitations, and I am not maintaining it or replying to requests for help. Thanks! (But also note that you are free to change and redistribute under the license terms, which you should read after downloading)

Have you ever wanted to apply an input mask to an HTML form field? Input masks are common in traditional GUI applications, but HTML has no such feature. This article introduces a library that adds input masks to form fields with unobtrusive JavaScript.

What’s an input mask?

View the Demo

Input masks are guides to help users enter data in the correct format. They typically do not actually validate data; they just ensure the right types of characters are entered in the right places. Typical uses are for dates, times, social security numbers, phone numbers, and credit card numbers. The user enters un-formatted input, and the mask takes care of adding dashes and other separators in appropriate places.

For example, in the United States most people use MM/DD/YY format to write dates. A well-written GUI application honors the user’s locale and creates an appropriate input mask, such as ##/##/##, for date entry. The user types the numbers, and the program inserts the slashes. If the user types something other than a number, that character is discarded, not entered into the field.

How to do this with JavaScript

There are several problems you need to solve to simulate this in a web browser. First things first: let’s state the requirements.

  1. Help the user avoid entering invalid characters.
  2. Automatically insert separators as the user types.
  3. Constrain the length of the input.

Second, let’s create a spec for the masking syntax. In Windows Forms programming, controls have a Mask property, and other GUI libraries have similar functionality. The full behavior of these masks is complex. For an example, see the MSDN documentation for masked edit controls. You can get a lot of that functionality with a simpler specification, though. The following will suffice for many uses:

  1. The mask only allows one type of character for the entire mask. For example, the mask can allow either all digits or all alphanumerics, but you can’t constrain one character to be a digit while letting other characters accept alphanumerics.
  2. The mask specifies the placeholders for input with spaces, and separators as non-spaces.

An example mask, then, has two parts: the format, which says which places can accept user input, and the type, which says what type of character can go in those places. We’ll see how to actually do this later.

The third problem is to unobtrusively attach the masking functionality to input fields, with gracefully degrading behavior if the browser doesn’t support it, and without adding a lot of markup to your forms to specify the mask format and type. This is easy, using the principles I laid out in an earlier article on using classes to specify data types. This technique is 100% appropriate because classes aren’t just hooks for CSS, they’re general-purpose processing information. This lets you easily specify a) which inputs get masks, and b) which type of mask they get.

How it works

To add masks to form fields, reference my library, then make the page’s load event fire the Xaprb.InputMask.setupElementMasks() function in my library. This will find all elements with the class input_mask, which specifies that the element should get a mask. Each element should also have a mask_??? class, where the ??? specifies which mask to attach. The library takes care of the rest.

By the way, this library depends on the Prototype library, so you will also need to reference that in your page. If you don’t, you won’t get an error, but nothing will happen.

The setup function iterates over the elements and connects a callback to the onkeypress event. The callback is created by another function. To decide which mask to apply, it does a regular expression match against the element’s className. If the element’s class is “input_mask mask_date_us“, the regular expression captures “date_us,” and looks up the date_us mask. Here’s how that is defined:

      date_us: {
         format: '  /  /    ',
         regex:  /\d/,
      }

The format property is a string with spaces where input should go, and other characters get inserted automatically. The regex property is a regular expression that matches a valid character, in this case a digit.

Here’s how the callback function works: when it fires, it checks each character in the form field’s value. If there’s a space in that place in the mask’s format string, it looks to see if the character matches the mask’s regular expression. If so, the character is valid for that place in the input; if not, the character is rejected. If there isn’t a space in that place in the format string, the character from the format string is copied into the form field (this is how separators are automatically inserted).

Demonstration

Enough talk, let’s see it in action. This demonstration of Javascript form input masks shows a few of the masks I discussed above: US date, time, and phone number.

If you like the way the form input fields look, you can thank the fine folks at Particletree. I borrowed the styling from their article on how to make forms suck less (it makes the borders of the input areas easier to see).

Limitations

Since this is really just a hack on top of existing HTML form inputs, there are some things that will never work quite as well as a natively designed widget (the same is true for my JavaScript Combo Box widget). Here are some of the limitations:

  • No unicode or international characters (this might be easy to fix).
  • No spaces as placeholders. Sometimes you might want spaces between user input, rather than non-space separators.
  • Only one type of character for the entire input; you can’t constrain the first character to be a digit, and the second a letter.
  • It doesn’t show the mask ahead of time and let the user ‘fill in’ the missing characters; instead, it reveals the mask as the user types.
  • You can’t have two adjacent separators.
  • You can’t type into the middle of the text; all input you type is appended to the end.
  • It hijacks things like Ctrl+A to select all.

Despite the length of that list, these are such minor things (except for maybe international characters) that it’s practically a complete implementation. And as far as I know, everything here could be solved easily. I just haven’t done it, because you haven’t yet told me which things are problems for you (hint, hint: leave a comment, and patches are very welcome). I deliberately kept things really simple in this first version. Future versions can get fancier, or not.

Conclusion

So that’s it! Simple, lightweight, intuitive input masks. With a proper form validation library on the back-end, you should be able to use this to help your users enter data in the format you desire. Again, let me know what you think, and by all means improve this, and send me the results!

Technorati Tags:No Tags

You might also like:

  1. JavaScript date chooser
  2. Javascript date parsing and formatting, Part 2
  3. JavaScript number-formatting library updated
  4. JavaScript date parsing and formatting, Part 1
  5. How to find and fix invalid character data in MySQL

Automatic image captions with unobtrusive JavaScript

View the Demo

You’ve probably heard of unobtrusive JavaScript. It’s a simple technique that lets you create richer documents without cluttered markup or accessibility problems. I often see link decoration and drop-shadow effects done with unobtrusive JavaScript. How about unobtrusive image captions? Instead of writing a caption into the page, let JavaScript do it. In this article I’ll show you how simple this can be.

The idea

How often have you seen websites that use a table or a div to enclose an image and some text to create a “captioned” image? Far too often, I’d guess. Semantically speaking, though, nothing in the markup actually associates the image and the “caption” — this isn’t a good way to give an image a caption. Until HTML itself actually has a true mechanism for a captioned image, or until someone makes a microformat for it, you may be better off taking the path of compromise between richness and semantic content.

That compromise, in my view, is using unobtrusive JavaScript to add richness to the site after it is loaded. The document is delivered to the browser in plain-Jane fashion, then altered after the browser loads it. The end result is no more semantic than hard-coding all the extra markup, but it’s a good trade-off in my opinion.

Plus, it makes authoring content much easier. Let’s see how.

The old way

Here’s typical code you might see to create a “captioned” image:

<div style="width:100px">
   <img src="treefrog.png" alt="A tree frog"
      width="100" height="100" />
   <p>A tree frog</p>
</div>

You may also see inline styles, a table instead of a div, and other ugliness.

The new way

The better way to do this is simply eliminate all that redundant typing. What’s redundant, you say? Well, the width of the wrapper element and the text of the “caption,” to mention two things. The image already has a width — why repeat it in the wrapper element? The image already has a place for associated text in its title attribute — why write it out again?

Let’s strip things down to the bare minimum:

<img src="treefrog.png" alt="A tree frog"
      title="A tree frog" class="figure"
      width="100" height="100" />

Does that look minimal to you? If you’re thinking the alt attribute and the title attribute are redundant, I disagree. The alt attribute is never to be presented to the user when the image can be presented. It is an alternate representation of the content when the image isn’t available. It is not a title for the image. That’s why there is a title attribute for images! If it were redundant to the alt attribute, it wouldn’t be there.

I propose this as the bare minimum for an image, except for one thing: CSS styling. You may want to use CSS to float your image to the left or right. Nobody wants an ugly break in the text flow for an image. But that’s where I’ll start with this article.

CSS styling

Time for a demonstration of the starting point. I’ll insert two images from stock.xchng, a great free stock photography site. I’ll float one of them left and the other right with the following CSS:

.left {
   float: left;
   margin-right: 5px;
}
.right {
   float: right;
   margin-left: 5px;
}

You can see a demonstration here. It’s pretty plain, isn’t it? But it works. Nothing fancy, but then again you can do a lot with this basic markup. Now let’s add some captions to those images.

The magic

It’s actually really easy to lift the images out of the page, insert a wrapper element, and put the image back into the wrapper. There are some subtleties, and I’ve done a thing or two to make the technique easily extensible. Here’s the code:

function addCaptionsToImages() {
   wrapImagesInDiv( 'figure', [], [ 'float' ] );
}

if(typeof window.addEventListener != 'undefined') {
   window.addEventListener('load', addCaptionsToImages, false);
}
else if(typeof document.addEventListener != 'undefined') {
   document.addEventListener('load', addCaptionsToImages, false);
}
else if(typeof window.attachEvent != 'undefined') {
   window.attachEvent('onload', addCaptionsToImages);
}

function wrapImagesInDiv( className, attributes, styles ) {
   var images = document.getElementsByClassName(className);
   for ( var i = 0; i < images.length; ++i ) {
      var img = images[i];
      // Lift the image out of the page and insert a div under it.
      var parent = img.parentNode;
      var frame = document.createElement('div');
      var txt = document.createTextNode(img.getAttribute('title'));
      parent.insertBefore(frame, img);
      parent.removeChild(img);
      frame.appendChild(img);
      frame.appendChild(txt);
      // These are special cases.  We always copy these from the image to the
      // div.
      frame.style.width = img.getAttribute('width') + 'px';
      frame.className = img.className;
      // Copy specified attributes and style properties from the image to the
      // div.
      for ( var j = 0; j < attributes.length; ++j ) {
         frame.setAttribute(attributes[j], img.getAttribute(attributes[j]));
      }
      for ( var j = 0; j < styles.length; ++j ) {
         frame.style[styles[j]] = img.style[styles[j]];
      }
   }
}

I’ll break that down a bit at a time. The first few lines define a function to be called on page load. I use Scott Andrew’s cross-browser onload functionality to add an event that’ll call the wrapImagesInDiv function with the desired arguments. I do this because I want the flexibility of passing desired arguments.

Next I define the wrapImagesInDiv function itself. This function accepts a class name, an array of attribute names, and an array of style property names. The class name defines the target elements: everything with a class of “figure.” The array of attribute names specifies which attribute values should be copied from the image to its wrapper, and likewise with style properties. You’ll see where I use those in a bit.

The first line inside the function uses the document.getElementsByClassName method supplied by the Prototype library to find all the images labelled with class="figure". There are other ways to do this, but I think Prototype is ubiquitous enough that this is a fine choice.

After that, the script simply loops through the array of resulting objects. For each object, it gets a reference to the parent element, creates a div element, moves the image inside that, and appends the image’s title attribute as text afterward.

Then it does a couple of tricky things. First, it sizes the wrapper horizontally to match the image. This is necessary so the wrapper doesn’t take over the entire width of the page. Next it copies the image’s class to the wrapper. This is necessary to preserve any CSS styling that was used to place the image relative to the text. In my case, this preserves the left-floating and right-floating I created in the first example.

Finally, it loops through every other attribute and style property I specified, and copies those from the image to its wrapper. This is an easy way to get the same script to do lots of different things — you just give it different arguments. Do you want the wrapper to have the same title attribute as the image? Fine, just add ‘title’ to the argument array — I’m all about choice.

I’ll add a couple extra lines of CSS to make things prettier, too. Here’s the result: automatic image captions without ugly markup.

Further tweaks

This is a really simple technique, and you can go much further with it. Here are some more ideas:

  1. Use Roger Johansson’s technique to add custom corners, borders, and drop shadows. Why not?
  2. In keeping with thinking of the images as “figures,” auto-number them, and create references to them on the fly by adding in other hooks for JavaScript.
  3. Tables could be figures, too, and you could auto-number and auto-reference them. But don’t auto-caption tables, because they already have a caption element of their own.
  4. Generate a table of figures for your document.

If you haven’t done much reading on unobtrusive JavaScript, you really should. You can do so much with it. And the best part is, it lets you keep your HTML sooooo much cleaner and saves you so much work.

Conclusion

I hope this article has helped you break out of a common web design rut and snazzy up your documents without adding un-semantic markup bloat. If you thought this was good, let me know! If you want to suggest some improvement, let me know that too! And if you want to subscribe to my articles, go right ahead. It’s free and easy.

Technorati Tags:No Tags

You might also like:

  1. How to display an HTML table as a folder tree
  2. Advanced HTML table features, Part 1
  3. JavaScript date chooser
  4. Stock images are too popular
  5. How to style HTML lists consistently in all browsers

Announcement: Xaprb scripts are re-licensed

I have re-licensed some of my scripts under the LGPL, which means you can use them as part of other non-GPL software.

The scripts in question are:

Why the change?

I’ve always released my work under the GPL, but now that I’m writing and making available smaller scripts that might be used as part of a larger software system, the GPL isn’t as appropriate, because of the licensing requirements it places on the rest of the systems with which the scripts may be used.

I’m not making the decision casually. I’m a firm believer in free software, as in software freedom. Not only should software be open and un-encumbered by restrictions, but this freedom should be guaranteed even to the extent of prohibiting someone from making a restricted non-free work based on it. Software is encoded knowledge that belongs to humanity as a whole, just as certainly as mathematical formulae. That’s why I oppose non-free software and software patents.

However, for certain small scripts I’ve written, this rigid approach doesn’t make sense. I’m not releasing these scripts as “software.” The scripts mainly serve as working examples of principles and methods I’m trying to illustrate. From that point of view, the knowledge encoded in the scripts is already free — it’s in the articles I write to introduce the scripts, techniques and algorithms. Plus, I’m not breaking any new ground with the articles, either. Nothing I say here is revolutionary.

I read Richard Stallman’s writing carefully. Though he has a reputation for being a hard-liner, I value his opinions and judgement highly. When I’ve had contact with him, he’s come across just as he does in his essays: as a man of principles, which he will not yield. I respect that. One of his essays is about not using the LGPL. For the reasons stated above, I don’t think the points raised in that essay are applicable to the scripts I’ve released on this website. I believe these scripts are best released under a more permissive license.

The future

This is also a forward-looking change. I’m not trying to be grandiose about these little scripts I’ve written. They’re small snippets and they won’t change the world. But I’ve been working on some other things that are more significant, and I want everything I release to be licensed consistently with my beliefs. When I release these other projects, I’ll be careful to license them in accordance with what they are, what they do, and how others might find them useful.

I hope you, or the projects you’re working on, will find my work useful. Please feel free to contact me if you have any questions, comments, or improvements.

Technorati Tags:No Tags

You might also like:

  1. How to read the clipboard from JavaScript
  2. MySQL Community Member of the Year
  3. The truth about MySQL Community and Enterprise
  4. Why I write Free Software

JavaScript date formatting benchmarks

Two earlier articles demonstrated how to format and parse dates flexibly with JavaScript. I asserted in those articles that my approach was efficient, though I didn’t provide any numbers to prove my claim. This article compares the performance of my date-formatting library against several other date-formatting libraries I’ve found online.

I’d like to benchmark my date-parsing library too, but I haven’t seen any comparable implementations. By the way, my date-formatting and date-parsing libraries are wrapped into a single file, so even though I’m not actually executing the date-parsing functions in this benchmark, they’re compiled anyway.

Motivation

My motivation for this article is simply to demonstrate the truth of what I said earlier: writing code to write code can be very efficient. I’m explicitly not trying to say I’m “better” than anyone, or someone else does anything “wrong” or anything like that. This series of articles really isn’t even about date manipulation! It’s about demonstrating a programming technique: write a function that can write other functions, making it possible to optimize code whose behavior depends on input that is not known until runtime. That’s why my first date-formatting article was originally titled “JavaScript closures for runtime efficiency.”

Note that my motivation also isn’t to make the fastest date formatting code. I haven’t really tried to optimize for speed, but if I wanted to, I think I could probably find some bottlenecks and optimize my own code further.

All I’m trying to do is demonstrate the general coding methodology I used, because I often see folks using a much less optimal solution, probably because they don’t know about (or aren’t comfortable with) dynamic code generation. That’s just my opinion.

Setup

I ran these benchmarks on Firefox 1.5.0.2 on my home computer, a fairly new and powerful AMD64 machine running Gentoo GNU/Linux. I won’t bother telling you all the hardware specs… that always makes my eyes glaze over.

I created a set of pages that do nothing but include the JavaScript files needed, and run 100,000 iterations of date-formatting. I closed my browser window between each test, and nothing else was running on my machine. I ran each test several times and averaged the results, rounding to four significant digits.

I had to increase the script timeout so Firefox wouldn’t interrupt the tests. I did this by opening about:config, then changing dom.max_script_run_time to 5000 seconds.

Results

Here’s a graph of the times. The algorithms are in alphabetical order:

JavaScript date-formatting benchmark

Obviously, the method I use is much faster — between 3.15 and 4.89 times faster. Here are the results as numbers in a table. WARNING: If you click on the links to the benchmarks, your browser will probably freeze for the better part of a minute on a fast machine — maybe longer on a slow machine.

Is this an apples-to-apples comparison?

Absolutely not, and if it were, the slowness of the other methods would be even more obvious.

First of all, I’m only testing a single method of formatting — producing a date in YYYY-MM-DD format. I also haven’t been scientific enough to really be accurate.

Beyond that, though, these various bits of code I’ve benchmarked are vastly different. The one that provides the most similar formatting functionality to mine is Svend Tofte’s (that’s probably why it’s the slowest), but even that one only does parsing, not formatting (mine does both). The others are much less fully-featured, which means they’d probably be even less performant if someone extended them to implement the same set of functionality.

As I said above, I’m not doing this to pick on anyone, but the Dojo method is probably the least efficient. It’s the simplest of all, providing only a few formatting characters, and it’s not really that much faster than Svend Tofte’s implementation. It’s probably so slow because it a) uses lots of if statements and b) uses repeated string replacements with regular expressions. This is just a hunch, but if it had the rich feature set of my implementation or Svend Tofte’s, I think it would probably be the slowest by far.

Technorati Tags:No Tags

You might also like:

  1. Javascript date parsing and formatting, Part 2
  2. JavaScript date parsing and formatting, Part 1
  3. JavaScript formatting library update
  4. How to format numbers in JavaScript flexibly and efficiently
  5. JavaScript Number Formatting Library v1.3 released

JavaScript regular expression toolkit

I have created a web page that matches regular expressions against arbitrary input text and displays the results graphically, so you can take some sample text and build regular expressions the easy way, with immediate feedback about what matches and where, where you have errors, and more.

Regular Expression Toolkit Screenshot

I’ve been working on this for a couple of weeks, a minute here and there as I get time. A few days ago I saw someone created a very nifty similar app over at Rex V. Apparently the pundits are right — never assume you’re the only one with an idea.

Mine is simpler and doesn’t use AJAX. It’s JavaScript only. Thanks to the folks at ActiveState for the idea — I was inspired by Komodo. I have to wonder whether Rex V was too!

Here it is: the JavaScript regular expression toolkit.

I used my work on grouping data visually with row groups and browser variations in RexExp.exec() to build this tool.

Technorati Tags:No Tags

You might also like:

  1. How to avoid VBScript regular expression gotchas
  2. Javascript date parsing and formatting, Part 2
  3. Browser variations in RegExp.exec()
  4. MySQL Find 0.9.0 released
  5. How to block ads effectively with AdBlock regular expressions

Browser variations in RegExp.exec()

IE6, Firefox 1.5 and Opera 8.5 handle regular expressions slightly differently. I analyzed them and learned some interesting things about the browsers. For this article, I’m using abcde as the input, and /abc|d(e)/g as the regular expression. The expression should match twice, and the second match should capture the letter “e” because it’s in a capturing subexpression.

Properties of match results

Calling RegExp.prototype.exec() with a global regular expression returns an Array object with some extra properties. IE and Opera don’t quite agree with ECMA-262 — they add extra properties and don’t create properties that should exist (Firefox gets it right).

In case you don’t know the ins and outs of JavaScript regexes, there is a subtlety about capturing subexpressions and global regular expressions. exec actually only looks for one match even though the expression is global, but sets the index at which the match happened. If I call it again, it keeps looking from where it left off, so I can loop through the successive matches. Each time the result is an array containing the current match, plus the captures and information about where the match occurred. As usual, index 0 of the array contains the text of the entire match, index 1 contains the first captured subexpression, and so on.

Here’s what I get when I call /abc|d(e)/g.exec("abcde") twice in a row on the browsers in question:

I got the results with a for/in loop, like so:

var re = /abc|d(e)/g;
var result = re.exec("abcde");
for (var prop in result) {
    ...

Here are the differences:

  • Opera doesn’t enumerate over the first captured subexpression in the first result. In Firefox, it exists without a value (has the special value undefined), and in IE it exists with a value — the empty string.
  • IE adds the proprietary lastIndex property to the result.

Subexpressions

I said Opera doesn’t enumerate over the property named “1″ in the first result. According to the spec, the property named “1″ should still exist. Opera knows the property should exist, as I proved by examining the length property. Its value is 2 in all browsers, which is correct as specified by section 15.10.6.2 of the spec:

15.10.6.2 RegExp.prototype.exec(string)

Performs a regular expression match of string against the regular expression and returns an Array object containing the results of the match, or null if the string did not match

…snip…

  1. …[1-11 omitted]…
  2. Let n be the length of r’s captures array. (This is the same value as 15.10.2.1’s NCapturingParens.)
  3. Return a new array with the following properties:
    • The index property is set to the position of the matched substring within the complete string S.
    • The input property is set to S.
    • The length property is set to n + 1.
    • The 0 property is set to the matched substring (i.e. the portion of S between offset i inclusive and offset e exclusive).
    • For each integer i such that I > 0 and In, set the property named ToString(i) to the ith element of r’s captures array.

In other words, the length of the array should be 2 even in the first match, because the length of the array depends only on the number of capturing subexpressions in the pattern — so the browsers are doing the right thing.

If the property exists, Opera should enumerate it in the for/in loop. The spec is clear about what properties are enumerable (section 15.2.4.7), and it never says such a property should get the dontEnum attribute, so I think Opera’s behavior is incorrect. In fact, I’m pretty sure Opera is actually never creating the property. I ran some tests with an Array and set one of the properties to undefined. Opera still enumerates it, so it’s not as though Opera doesn’t enumerate properties that have no value. I think Opera is setting length to 2, but never creating properties for capturing subexpressions that don’t participate in the match. Technically this does not violate the spec’s instructions on an Array’s length property, but it is suspicious.

The moral of the story is you shouldn’t use a for/in loop when iterating through subexpressions. Just iterate from 0 through length minus one.

I take exception to IE giving the capture a value. The subexpression doesn’t capture anything and doesn’t participate in the match, so it should not have a value — not even the empty string or null. I suppose this one is up for debate, but that’s my personal opinion.

IE’s lastIndex property

Technically, this property shouldn’t be there; it should be a property of the global RegExp object in ECMA-262, or the regex itself in later versions of JavaScript (I have no idea why you’d make it a property of a global object; that seems like it would cause all sorts of stupid bugs, so I think the way IE does it is probably a lot smarter than the spec).

Other stuff

I spent a lot of time messing with the various browsers to see if I could find obscure bugs enumerating properties that don’t exist, setting a value and then unsetting it on subsequent calls, and so forth. The good news is I didn’t find any more bugs (though they could still exist!), and I found that the quasi-bugs discussed above are really trivial.

Technorati Tags:No Tags

You might also like:

  1. How to avoid VBScript regular expression gotchas
  2. JavaScript regular expression toolkit
  3. How to create input masks in HTML
  4. How to find duplicate and redundant indexes in MySQL
  5. Javascript date parsing and formatting, Part 2

How to format numbers in JavaScript flexibly and efficiently

Download number-functions

This article continues my series on parsing and formatting data with JavaScript, this time with numeric data. I don’t need to do number parsing, but formatting is very useful. The technique is similar to my date formatting code — code that writes code (for raw speed), using custom format specifier strings (for flexibility and ease of use). The result is number formatting functionality that is highly efficient, flexible, and easy to use.

First, the idea: you have a number, you want it formatted a certain way. Here’s how:

var dollars = 5.001;
alert(dollars.numberFormat("$0.00");
// result: "$5.00"
var percent = .08134;
alert(percent.numberFormat("0.00%");
// result: "8.13%"
var bignum = 12831242485472;
alert(bignum.numberFormat("0,0,, million");
// result: "12,831,243 million"

My custom date formatting code used PHP’s date-formatting syntax because it’s much less context-sensitive and (I think) more useful than Microsoft’s, but my number-formatting syntax is similar to Microsoft’s because it’s much more widely used and I don’t see an existing, better alternative. Rather than documenting it separately, I’ll just point you to the (poor quality) Microsoft documentation for the .NET Custom Numeric Format Strings functionality, and list the differences from my implementation:

  • Rounding works differently in multi-section format strings. In .NET with a two-section string,

    If the number to be formatted is negative, but becomes zero after rounding according to the format in the second section, then the resulting zero is formatted according to the first section.

    This is not true in my code — the number is formatted according to its value, and once the code decides which section applies, that section will be used no matter what happens during rounding.
  • Question marks are digit placeholders just like the number sign (#), but if there’s no digit to insert, they get replaced with spaces, not removed. They can be used for space-padding, which might be useful for, say, accounting notation.
  • You don’t have to enter quotes around strings that should be mixed in with the number placeholders. In fact, my syntax is much more permissive than the Microsoft syntax: anything can go anywhere. You can put arbitrary strings smack in the middle of your number if you want.
  • It’s not internationalized.

I’ve only implemented a subset of the various number-formatting syntaxes I’ve seen in spreadsheets and so forth. The subset is about 85% complete in my opinion. However, I think it’s functionally about 99% complete, which means I think 99% of the time you want to format a number, it will do what you want. The tradeoff is simplicity and speed. Number formatting is actually much more difficult than date formatting, and I’ve tried to keep the code sane.

I have a set of unit tests, which use the excellent JsUnit library. Bring up the unit test page and enter the following url to be tested: www.xaprb.com/articles/number-test.html.

Of course there’s the obligatory demo page, too.

Technorati Tags:, , , , ,

You might also like:

  1. Javascript date parsing and formatting, Part 2
  2. JavaScript date parsing and formatting, Part 1
  3. Excel vs. OpenOffice.org Calc in number formatting
  4. JavaScript formatting library update
  5. JavaScript Number Formatting Library v1.3 released

Don’t use future reserved words as identifiers in JavaScript

Just a quick note: I found recently that some browsers don’t complain when I use future reserved words as identifiers in JavaScript. Specifically, I used char as a variable name, and no Windows browser had a problem with it — but when I tested on Mac, both Safari and Firefox complained. Apparently there are differences in JavaScript implementation between the Windows and Mac versions of Firefox 1.5!

Nitty-gritty details: the ECMA-262 spec defines reserved words and future reserved words in section 7.5.1. Many OO-ish keywords like implements are reserved for the future, as well as specific data types like int and … you guessed it, char.

Technorati Tags:No Tags

You might also like:

  1. Browser variations in RegExp.exec()