From 3b54b21743a9281828e72568d84241fab4f3a882 Mon Sep 17 00:00:00 2001 From: karim hamidou Date: Sun, 13 Oct 2013 17:30:59 +0200 Subject: [PATCH] added filters --- src/front/app/js/filters.js | 28 +++++++++++++++++++++++----- src/front/test/unit/filtersSpec.js | 25 +++++++++++++++++++------ 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/front/app/js/filters.js b/src/front/app/js/filters.js index aa7d692..cf72dbc 100644 --- a/src/front/app/js/filters.js +++ b/src/front/app/js/filters.js @@ -3,8 +3,26 @@ /* Filters */ angular.module('KiteMail.filters', []). - filter('interpolate', ['version', function(version) { - return function(text) { - return String(text).replace(/\%VERSION\%/mg, version); - } - }]); + filter('truncate', function() { + return function(text) { + return text.slice(0, 35) + "..."; + } + }). + + /* takes a date object and returns a formatted object */ + filter('formatDate', function() { + return function(date) { + var date2array = function(date) { + return (date+'').split(' '); + } + + var today = date2array(new Date(Date.now())); + var da = date2array(date); + + if(da[3] == today[3]) { + return da[1] + ", " + da[2]; + } else { + return da[1] + ", " + da[2] + ", " + da[3]; + } + } + }); diff --git a/src/front/test/unit/filtersSpec.js b/src/front/test/unit/filtersSpec.js index fa5e9b9..c4170c9 100644 --- a/src/front/test/unit/filtersSpec.js +++ b/src/front/test/unit/filtersSpec.js @@ -3,17 +3,30 @@ /* jasmine specs for filters go here */ describe('filter', function() { - beforeEach(module('KiteMail.filters')); + beforeEach(module('KiteMail.filters')); - describe('interpolate', function() { - beforeEach(module(function($provide) { - $provide.value('version', 'TEST_VER'); + describe('setup suite', function() { + beforeEach(module(function($provide) { })); - it('should replace VERSION', inject(function(interpolateFilter) { - expect(interpolateFilter('before %VERSION% after')).toEqual('before TEST_VER after'); + it('should truncate text', inject(function(truncateFilter) { + var testStr = "This is a long string with a lot of chars"; + var resStr = "This is a long string with a lot of" + "..."; + expect(truncateFilter(testStr)).toEqual(resStr); })); + + it('should format a date', inject(function(formatDateFilter) { + var date = new Date(2013, 9, 13); + expect(formatDateFilter(date)).toEqual("Oct, 13"); + })); + + it('should have the year in the string if the date is not from this year', inject(function(formatDateFilter) { + var date = new Date(2009, 9, 13); + expect(formatDateFilter(date)).toEqual("Oct, 13, 2009"); + })); + + }); }); -- 2.43.0