mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-13 09:13:12 +00:00
Added support for date filter
Date filter now supports:
yyyy: four digit year
yy: two digit year
MM: two digit month
dd: two digit day of month
HH: two digit hour in 0-23
KK: two digit hour in 0-12
mm: two digit minute
ss: two digit second
a: am/pm
Z: four digit timezone offset
example {{ timestamp | date:'yyyy-MM-dd HH:mm:ss' }} becomes 2010-10-13 14:45:23
This commit is contained in:
parent
1cc85a77cf
commit
0f104317df
3 changed files with 154 additions and 75 deletions
|
|
@ -30,12 +30,55 @@ angularFilter.number = function(amount, fractionSize){
|
||||||
}
|
}
|
||||||
return text;
|
return text;
|
||||||
};
|
};
|
||||||
|
function padNumber(num, digits, trim) {
|
||||||
|
var neg = '';
|
||||||
|
if (num < 0) {
|
||||||
|
neg = '-';
|
||||||
|
num = -num;
|
||||||
|
}
|
||||||
|
num = '' + num;
|
||||||
|
while(num.length < digits) num = '0' + num;
|
||||||
|
if (trim)
|
||||||
|
num = num.substr(num.length - digits);
|
||||||
|
return neg + num;
|
||||||
|
}
|
||||||
|
function dateGetter(name, size, option) {
|
||||||
|
return function(date) {
|
||||||
|
var value = date[name].call(date) + 1*(option===1);
|
||||||
|
if (option == -12 && value > 12) value += option;
|
||||||
|
return padNumber(value, size, option === true);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
var DATE_FORMATS = {
|
||||||
|
yyyy: dateGetter('getFullYear', 4),
|
||||||
|
yy: dateGetter('getFullYear', 2, true),
|
||||||
|
MM: dateGetter('getMonth', 2, 1),
|
||||||
|
dd: dateGetter('getDate', 2),
|
||||||
|
HH: dateGetter('getHours', 2),
|
||||||
|
KK: dateGetter('getHours', 2, -12),
|
||||||
|
mm: dateGetter('getMinutes', 2),
|
||||||
|
ss: dateGetter('getSeconds', 2),
|
||||||
|
a: function(date){return date.getHours() < 12 ? 'am' : 'pm'; },
|
||||||
|
Z: function(date){
|
||||||
|
var offset = date.getTimezoneOffset();
|
||||||
|
return padNumber(offset / 60, 2) + padNumber(Math.abs(offset % 60), 2);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var DATE_FORMATS_SPLIT = new RegExp('('+
|
||||||
|
map(DATE_FORMATS, function(value, key){return key;}).join('|')+')');
|
||||||
|
console.log(DATE_FORMATS_SPLIT);
|
||||||
|
|
||||||
angularFilter.date = function(date) {
|
angularFilter.date = function(date, format) {
|
||||||
if (date instanceof Date)
|
if (!date instanceof Date) return date;
|
||||||
return date.toLocaleDateString();
|
var text = date.toLocaleDateString(), fn;
|
||||||
else
|
if (format && isString(format)) {
|
||||||
return date;
|
text = '';
|
||||||
|
foreach(format.split(DATE_FORMATS_SPLIT), function(value){
|
||||||
|
fn = DATE_FORMATS[value];
|
||||||
|
text += fn ? fn(date) : value;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return text;
|
||||||
};
|
};
|
||||||
|
|
||||||
angularFilter.json = function(object) {
|
angularFilter.json = function(object) {
|
||||||
|
|
|
||||||
106
test/FiltersSpec.js
Normal file
106
test/FiltersSpec.js
Normal file
|
|
@ -0,0 +1,106 @@
|
||||||
|
describe('filter', function(){
|
||||||
|
|
||||||
|
var filter = angular.filter;
|
||||||
|
|
||||||
|
describe('Currency', function(){
|
||||||
|
it('should do basic filter', function(){
|
||||||
|
var html = jqLite('<span/>');
|
||||||
|
var context = {$element:html};
|
||||||
|
var currency = bind(context, filter.currency);
|
||||||
|
|
||||||
|
assertEquals(currency(0), '$0.00');
|
||||||
|
assertEquals(html.hasClass('ng:format-negative'), false);
|
||||||
|
assertEquals(currency(-999), '$-999.00');
|
||||||
|
assertEquals(html.hasClass('ng:format-negative'), true);
|
||||||
|
assertEquals(currency(1234.5678), '$1,234.57');
|
||||||
|
assertEquals(html.hasClass('ng:format-negative'), false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('FilterThisIsContext', function(){
|
||||||
|
it('should do basic filter', function(){
|
||||||
|
expectAsserts(1);
|
||||||
|
var scope = createScope();
|
||||||
|
scope.name = 'misko';
|
||||||
|
filter.testFn = function () {
|
||||||
|
assertEquals('scope not equal', 'misko', this.name);
|
||||||
|
};
|
||||||
|
scope.$eval("0|testFn");
|
||||||
|
delete angular.filter['testFn'];
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('NumberFormat', function(){
|
||||||
|
it('should do basic filter', function(){
|
||||||
|
var context = {jqElement:jqLite('<span/>')};
|
||||||
|
var number = bind(context, filter.number);
|
||||||
|
|
||||||
|
assertEquals('0', number(0, 0));
|
||||||
|
assertEquals('0.00', number(0));
|
||||||
|
assertEquals('-999.00', number(-999));
|
||||||
|
assertEquals('1,234.57', number(1234.5678));
|
||||||
|
assertEquals('', number(Number.NaN));
|
||||||
|
assertEquals('1,234.57', number("1234.5678"));
|
||||||
|
assertEquals("", number(1/0));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Json', function () {
|
||||||
|
it('should do basic filter', function(){
|
||||||
|
assertEquals(toJson({a:"b"}, true), filter.json.call({$element:jqLite('<div></div>')}, {a:"b"}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Lowercase', function() {
|
||||||
|
it('should do basic filter', function(){
|
||||||
|
assertEquals('abc', filter.lowercase('AbC'));
|
||||||
|
assertEquals(null, filter.lowercase(null));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Uppercase', function() {
|
||||||
|
it('should do basic filter', function(){
|
||||||
|
assertEquals('ABC', filter.uppercase('AbC'));
|
||||||
|
assertEquals(null, filter.uppercase(null));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Html', function() {
|
||||||
|
it('should do basic filter', function(){
|
||||||
|
var html = filter.html("a<b>c</b>d");
|
||||||
|
expect(html instanceof HTML).toBeTruthy();
|
||||||
|
expect(html.html).toEqual("a<b>c</b>d");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Linky', function() {
|
||||||
|
it('should do basic filter', function(){
|
||||||
|
var linky = filter.linky;
|
||||||
|
assertEquals(
|
||||||
|
'<a href="http://ab/">http://ab/</a> ' +
|
||||||
|
'(<a href="http://a/">http://a/</a>) ' +
|
||||||
|
'<<a href="http://a/">http://a/</a>> ' +
|
||||||
|
'<a href="http://1.2/v:~-123">http://1.2/v:~-123</a>. c',
|
||||||
|
linky("http://ab/ (http://a/) <http://a/> http://1.2/v:~-123. c").html);
|
||||||
|
assertEquals(undefined, linky(undefined));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('date', function(){
|
||||||
|
var date = angular.String.toDate('2010-10-13T14:45:23Z');
|
||||||
|
date.setHours(14);
|
||||||
|
|
||||||
|
it('should do basic filter', function() {
|
||||||
|
expect(filter.date(date)).toEqual(date.toLocaleDateString());
|
||||||
|
expect(filter.date(date, '')).toEqual(date.toLocaleDateString());
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should accept format', function() {
|
||||||
|
expect(filter.date(date, "yyyy-MM-dd HH:mm:ss")).toEqual('2010-10-13 14:45:23');
|
||||||
|
expect(filter.date(date, "yy-MM-dd KK:mm:ssaZ")).toEqual('10-10-13 02:45:23pm0700');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
@ -1,70 +0,0 @@
|
||||||
FiltersTest = TestCase('FiltersTest');
|
|
||||||
|
|
||||||
FiltersTest.prototype.testCurrency = function(){
|
|
||||||
var html = jqLite('<span/>');
|
|
||||||
var context = {$element:html};
|
|
||||||
var currency = bind(context, angular.filter.currency);
|
|
||||||
|
|
||||||
assertEquals(currency(0), '$0.00');
|
|
||||||
assertEquals(html.hasClass('ng:format-negative'), false);
|
|
||||||
assertEquals(currency(-999), '$-999.00');
|
|
||||||
assertEquals(html.hasClass('ng:format-negative'), true);
|
|
||||||
assertEquals(currency(1234.5678), '$1,234.57');
|
|
||||||
assertEquals(html.hasClass('ng:format-negative'), false);
|
|
||||||
};
|
|
||||||
|
|
||||||
FiltersTest.prototype.testFilterThisIsContext = function(){
|
|
||||||
expectAsserts(1);
|
|
||||||
var scope = createScope();
|
|
||||||
scope.name = 'misko';
|
|
||||||
angular.filter.testFn = function () {
|
|
||||||
assertEquals('scope not equal', 'misko', this.name);
|
|
||||||
};
|
|
||||||
scope.$eval("0|testFn");
|
|
||||||
delete angular.filter['testFn'];
|
|
||||||
};
|
|
||||||
|
|
||||||
FiltersTest.prototype.testNumberFormat = function(){
|
|
||||||
var context = {jqElement:jqLite('<span/>')};
|
|
||||||
var number = bind(context, angular.filter.number);
|
|
||||||
|
|
||||||
assertEquals('0', number(0, 0));
|
|
||||||
assertEquals('0.00', number(0));
|
|
||||||
assertEquals('-999.00', number(-999));
|
|
||||||
assertEquals('1,234.57', number(1234.5678));
|
|
||||||
assertEquals('', number(Number.NaN));
|
|
||||||
assertEquals('1,234.57', number("1234.5678"));
|
|
||||||
assertEquals("", number(1/0));
|
|
||||||
};
|
|
||||||
|
|
||||||
FiltersTest.prototype.testJson = function () {
|
|
||||||
assertEquals(toJson({a:"b"}, true), angular.filter.json.call({$element:jqLite('<div></div>')}, {a:"b"}));
|
|
||||||
};
|
|
||||||
|
|
||||||
FiltersTest.prototype.testLowercase = function() {
|
|
||||||
assertEquals('abc', angular.filter.lowercase('AbC'));
|
|
||||||
assertEquals(null, angular.filter.lowercase(null));
|
|
||||||
};
|
|
||||||
|
|
||||||
FiltersTest.prototype.testUppercase = function() {
|
|
||||||
assertEquals('ABC', angular.filter.uppercase('AbC'));
|
|
||||||
assertEquals(null, angular.filter.uppercase(null));
|
|
||||||
};
|
|
||||||
|
|
||||||
FiltersTest.prototype.testHtml = function() {
|
|
||||||
var html = angular.filter.html("a<b>c</b>d");
|
|
||||||
expect(html instanceof HTML).toBeTruthy();
|
|
||||||
expect(html.html).toEqual("a<b>c</b>d");
|
|
||||||
};
|
|
||||||
|
|
||||||
FiltersTest.prototype.testLinky = function() {
|
|
||||||
var linky = angular.filter.linky;
|
|
||||||
assertEquals(
|
|
||||||
'<a href="http://ab/">http://ab/</a> ' +
|
|
||||||
'(<a href="http://a/">http://a/</a>) ' +
|
|
||||||
'<<a href="http://a/">http://a/</a>> ' +
|
|
||||||
'<a href="http://1.2/v:~-123">http://1.2/v:~-123</a>. c',
|
|
||||||
linky("http://ab/ (http://a/) <http://a/> http://1.2/v:~-123. c").html);
|
|
||||||
assertEquals(undefined, linky(undefined));
|
|
||||||
};
|
|
||||||
|
|
||||||
Loading…
Reference in a new issue