Cool Currency Focused JS Library

Just found accounting.js, which is a Javascript library “… for number, money and currency formatting …”. I love it so far. Check out the examples, then go check out the lib here and here.

// Default usage:

accounting.formatMoney(12345678); // $12,345,678.00

// European formatting (custom symbol and separators), could also use options object as second param:
accounting.formatMoney(4999.99, "€", 2, ".", ","); // €4.999,99

// Negative values are formatted nicely, too:
accounting.formatMoney(-500000, "£ ", 0); // £ -500,000

// Simple `format` string allows control of symbol position [%v = value, %s = symbol]:
accounting.formatMoney(5318008, { symbol: "GBP",  format: "%v %s" }); // 5,318,008.00 GBP

Regular Expression in Array.indexOf()

A few minutes ago I found I needed to find the first index of an element in an array using RegEx. Unfortunately, such a tool is not in the JavaScript toolbox so without further delay, here’s what I came up with.

 * Regular Expresion IndexOf for Arrays
 * This little addition to the Array prototype will iterate over array
 * and return the index of the first element which matches the provided
 * regular expresion.
 * Note: This will not match on objects.
 * @param  {RegEx}   rx The regular expression to test with. E.g. /-ba/gim
 * @return {Numeric} -1 means not found
if (typeof Array.prototype.reIndexOf === 'undefined') {
    Array.prototype.reIndexOf = function (rx) {
        for (var i in this) {
            if (this[i].toString().match(rx)) {
                return i;
        return -1;

// Try it out

// Array of strings
// Should return 3
var test = ['foo', '-bar', 'droopy', 'dog'];
console.log( 'array of strings:', test.reIndexOf(/og/) );

// Array with numbers
// Should return 3
var test = ['foo', '-bar', '1', 2];
console.log( 'array with numbers:', test.reIndexOf(/2/) );

// Array with objects
// Should return 3
var test = ['foo', { 'test':'one' }, { 'test':2 }, 2];
console.log( 'array of obj:', test.reIndexOf(/2/) );

SQL Columns to CSV List

There have been a few times where I’ve needed to return the values of multiple columns as one column containing a CSV list of those values.

This is accomplished using a sub-query like so…

USE AdventureWorks
-- Check Table Column
FROM HumanResources.Shift
-- Get CSV values
(SELECT ',' + s.Name
FROM HumanResources.Shift s
FOR XML PATH('')),1,1,'') AS CSV

The above example came from Pinal Dave’s great post on SQL Authority. I substituted his use of SUBSTRING for STUFF since it will handle any length content.