Permalink
Browse files

Add the possibility to define a format as function with Date.defineFo…

…rmat

- Update Docs
- Add specs for defineFormat and defineFormats
  • Loading branch information...
arian committed May 15, 2011
1 parent fc53a69 commit 58974b33869b3b64fb5afa3668f88c318f3a8446
Showing with 52 additions and 22 deletions.
  1. +8 −2 Docs/Types/Date.md
  2. +12 −20 Source/Types/Date.js
  3. +32 −0 Specs/1.3/Types/Date.js
View
@@ -424,14 +424,20 @@ Adds a new shortcut for [Date:format][].
### Arguments
-1. name - (*string*) name of the new format
-2. format - (*string*) format string (see [Date:format][])
+1. name - (*string*) name of the new format, as lowercase.
+2. format - (*string*, *function*) format string (see [Date:format][])
### Example
Date.defineFormat('time', '%H:%M');
new Date().format('time'); //17:30
+ // also possible to pass a function
+ Date.defineFormat('timeago', function(date){
+ var now = Date.now();
+ return Math.round((now - date) / (1000 * 60)) + ' minutes ago';
+ });
+
Static Method: defineFormats {#Date:Date-defineFormats}
-------------------------------------------------------
View
@@ -208,16 +208,15 @@ Date.implement({
return !isNaN((date || this).valueOf());
},
- format: function(f){
+ format: function(format){
if (!this.isValid()) return 'invalid date';
- if (!f) f = '%x %X';
- var formatLower = f.toLowerCase();
- if (formatters[formatLower]) return formatters[formatLower](this); // it's a formatter!
- f = formats[formatLower] || f; // replace short-hand with actual format
+ if (!format) format = '%x %X';
+ if (typeof format == 'string') format = formats[format.toLowerCase()] || format;
+ if (typeof format == 'function') return format(this);
var d = this;
- return f.replace(/%([a-z%])/gi,
+ return format.replace(/%([a-z%])/gi,
function($0, $1){
switch ($1){
case 'a': return Date.getMsg('days_abbr')[d.get('day')];
@@ -264,18 +263,15 @@ Date.implement({
strftime: 'format'
});
-var formats = {
- db: '%Y-%m-%d %H:%M:%S',
- compact: '%Y%m%dT%H%M%S',
- 'short': '%d %b %H:%M',
- 'long': '%B %d, %Y %H:%M'
-};
-
// The day and month abbreviations are standardized, so we cannot use simply %a and %b because they will get localized
var rfcDayAbbr = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
rfcMonthAbbr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
-var formatters = {
+var formats = {
+ db: '%Y-%m-%d %H:%M:%S',
+ compact: '%Y%m%dT%H%M%S',
+ 'short': '%d %b %H:%M',
+ 'long': '%B %d, %Y %H:%M',
rfc822: function(date){
return rfcDayAbbr[date.get('day')] + date.format(', %d ') + rfcMonthAbbr[date.get('month')] + date.format(' %Y %H:%M:%S %Z');
},
@@ -295,7 +291,6 @@ var formatters = {
}
};
-
var parsePatterns = [],
nativeParse = Date.parse;
@@ -407,11 +402,6 @@ Date.extend({
return this;
},
- defineFormats: function(formats){
- for (var name in formats) Date.defineFormat(name, formats[name]);
- return this;
- },
-
//<1.2compat>
parsePatterns: parsePatterns,
//</1.2compat>
@@ -432,6 +422,8 @@ Date.extend({
return this;
}
+}).extend({
+ defineFormats: Date.defineFormat.overloadSetter()
});
var regexOf = function(type){
View
@@ -673,6 +673,38 @@ describe('Date', function(){
});
+ describe('Date.defineFormat', function(){
+
+ it('should define a new formatter as a string', function(){
+ var format = '__' + String.uniqueID();
+ var _Date = Date.defineFormat(format, format);
+ var d = new Date();
+ expect(_Date).toEqual(Date);
+ expect(d.format(format)).toEqual(format);
+ });
+
+ it('should define a new formatter as a function', function(){
+ var format = '__' + String.uniqueID();
+ var d = new Date();
+ Date.defineFormat(format, function(date){
+ expect(date).toEqual(d);
+ return format + '__';
+ });
+ expect(d.format(format)).toEqual(format + '__');
+ });
+
+ it('should define mulitple formats', function(){
+ var formats = {},
+ format1 = '__' + String.uniqueID(),
+ format2 = '__' + String.uniqueID();
+ formats[format1] = format1;
+ formats[format2] = format2;
+ Date.defineFormats(formats);
+ expect(new Date().format(format1)).toEqual(format1);
+ });
+
+ });
+
});
})(this);

0 comments on commit 58974b3

Please sign in to comment.