Skip to content
This repository has been archived by the owner on May 23, 2019. It is now read-only.

Icu formatters #36

Merged
merged 10 commits into from
Jan 16, 2014
125 changes: 116 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,119 @@
REGEX_TOKEN_BREAK = /(\$?\{?[^\$\{\}]*\}?)/gi,
REGEX_TOKEN_AND_FORMATTER = /\$?\{([-\w]*):?([-\w]*)?\}/i,

DEFAULT_FORMATTERS = {
// TYPE: number
number_integer: function (val, locale) {
// 20000 -> 20,000
return (new Intl.NumberFormat(locale)).format(val);
},
number_currency: function (val, locale, options) {
// 20000 -> $20,000.00
var currencyFormat = new Intl.NumberFormat(locale, {
style: 'currency',
currency: options.currency || options.CURRENCY || 'USD'
});
return currencyFormat.format(val);
},
number_percent: function (val, locale) {
// 20000 -> 200%
return (new Intl.NumberFormat(locale, { style: 'percent'})).format(val);
},

// TYPE: date
// Date formats
date_short: function (val, locale, options) {
var dateFormat = new Intl.DateTimeFormat(locale, {
timeZone : options.timeZone || null,
month: 'numeric',
day : 'numeric',
year : '2-digit'
});

return dateFormat.format(val);
},

date_medium: function (val, locale, options) {
var dateFormat = new Intl.DateTimeFormat(locale, {
timeZone : options.timeZone || null,
month: 'short',
day : 'numeric',
year : 'numeric'
});

return dateFormat.format(val);
},

date_long: function (val, locale, options) {
var dateFormat = new Intl.DateTimeFormat(locale, {
timeZone : options.timeZone || null,
month: 'long',
day : 'numeric',
year : 'numeric'
});

return dateFormat.format(val);
},

date_full: function (val, locale, options) {
var dateFormat = new Intl.DateTimeFormat(locale, {
timeZone : options.timeZone || null,
weekday: 'long',
month : 'long',
day : 'numeric',
year : 'numeric'
});

return dateFormat.format(val);
},

// TYPE: time
time_short: function (val, locale, options) {
var timeFormat = new Intl.DateTimeFormat(locale, {
timeZone: options.timeZone || null,
hour : 'numeric',
minute : 'numeric'
});

return timeFormat.format(val);
},

time_medium: function (val, locale, options) {
var timeFormat = new Intl.DateTimeFormat(locale, {
timeZone: options.timeZone || null,
hour : 'numeric',
minute : 'numeric',
second : 'numeric'
});

return timeFormat.format(val);
},

time_long: function (val, locale, options) {
var timeFormat = new Intl.DateTimeFormat(locale, {
timeZone : options.timeZone || null,
hour : 'numeric',
minute : 'numeric',
second : 'numeric',
timeZoneName: 'short'
});

return timeFormat.format(val);
},

time_full: function (val, locale, options) {
var timeFormat = new Intl.DateTimeFormat(locale, {
timeZone : options.timeZone || null,
hour : 'numeric',
minute : 'numeric',
second : 'numeric',
timeZoneName: 'short'
});

return timeFormat.format(val);
}
},

// localeData registered by __addLocaleData()
localeData = {};

Expand Down Expand Up @@ -123,15 +236,9 @@
this.pattern = pattern;

// store formatters
this.formatters = {};

if (optFieldFormatters) {
for (p in optFieldFormatters) {
if (optFieldFormatters.hasOwnProperty(p) && typeof optFieldFormatters[p] === 'function') {
this.formatters[p] = optFieldFormatters[p];
}
}
}
this.formatters = optFieldFormatters || {};
/*jshint proto:true*/
this.formatters.__proto__ = DEFAULT_FORMATTERS;
}

/**
Expand Down
46 changes: 25 additions & 21 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,35 +132,39 @@ describe('IntlMessageFormat', function () {

describe('#formatters', function () {
it('should be an empty object without a third parameter', function () {
var msgFmt = new IntlMessageFormat(),
p, pCount = 0;
var msgFmt = new IntlMessageFormat();

expect(msgFmt.formatters).to.be.an('object');

for (p in msgFmt.formatters) {
if (msgFmt.formatters.hasOwnProperty(p)) {
pCount++;
}
}

expect(pCount).to.equal(0);
// Randomly test for default formatters to exist
expect(msgFmt.formatters.number_integer).to.be.a('function');
expect(msgFmt.formatters.date_short).to.be.a('function');
expect(msgFmt.formatters.time_long).to.be.a('function');
});

it('should only contain formatter functions from the third parameter', function () {
var msgFmt = new IntlMessageFormat(null, null, {
'num': 3,
'str': 'foo',
'fn' : function () { }
}),
it('should maintain the default formatters', function () {
var msgFmtA = new IntlMessageFormat(null, null, {
foo: function (val) {
return 'foo: ' + val;
}
}),
msgFmtB;

formatters = msgFmt.formatters;

/*jshint expr:true */
expect(formatters.fn).to.exist;
/*jshint expr:true */
expect(formatters.num).to.not.exist;
/*jshint expr:true */
expect(formatters.str).to.not.exist;
expect(msgFmtA.formatters.foo).to.be.a('function');
expect(msgFmtA.formatters.time_long).to.be.a('function');
expect(msgFmtA.formatters.foo('bar')).to.equal('foo: bar');




msgFmtB = new IntlMessageFormat();

/*jshint expr:true*/
expect(msgFmtB.formatters.foo).to.not.exist;
/*jshint expr:true*/
expect(msgFmtB.formatters.time_long).to.exist;
});

});
Expand Down