Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #52 from otang/default-format
Browse files Browse the repository at this point in the history
Add default format (angularMomentConfig.format config property)
  • Loading branch information
urish committed Jun 1, 2014
2 parents d01c8bc + ff5d871 commit f53e86f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
24 changes: 20 additions & 4 deletions angular-moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,20 @@
* The default timezone (e.g. 'Europe/London'). Empty string by default (does not apply
* any timezone shift).
*/
timezone: ''
timezone: '',

/**
* @ngdoc property
* @name angularMoment.config.angularMomentConfig#format
* @propertyOf angularMoment.config:angularMomentConfig
* @returns {string} The pre-conversion format of the date
*
* @description
* Specify the format of the input date. Essentially it's a
* default and saves you from specifying a format in every
* element. Overridden by element attr. Null by default.
*/
format: null
})

/**
Expand Down Expand Up @@ -91,7 +104,7 @@
return function (scope, element, attr) {
var activeTimeout = null;
var currentValue;
var currentFormat;
var currentFormat = angularMomentConfig.format;
var withoutSuffix = amTimeAgoConfig.withoutSuffix;
var preprocess = angularMomentConfig.preprocess;

Expand Down Expand Up @@ -152,8 +165,10 @@
}

attr.$observe('amFormat', function (format) {
currentFormat = format;
updateMoment();
if(typeof format !== 'undefined') {
currentFormat = format;
updateMoment();
}
});

attr.$observe('amPreprocess', function (newValue) {
Expand Down Expand Up @@ -333,3 +348,4 @@
angularMoment(angular, window.moment);
}
})();

2 changes: 1 addition & 1 deletion angular-moment.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion angular-moment.min.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,28 @@ describe('module angularMoment', function () {
expect(element.text()).toBe('a month ago');
});
});

describe('format config property', function () {
it('should be used when no `am-format` attribute is found', function () {
angularMomentConfig.format = 'MM@YYYY@DD';
var today = new Date();
$rootScope.testDate = today.getMonth() + '@' + today.getFullYear() + '@' + today.getDate();
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a month ago');
});

it('should be overridable by `am-format` attribute', function () {
angularMomentConfig.format = 'YYYY@MM@@DD';
var today = new Date();
$rootScope.testDate = today.getMonth() + '@' + today.getFullYear() + '@' + today.getDate();
var element = angular.element('<span am-format="MM@YYYY@DD" am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a month ago');
});
});
});

describe('amCalendar filter', function () {
Expand Down Expand Up @@ -413,7 +435,7 @@ describe('module angularMoment', function () {
amMoment.preprocessors.foobar = function (value) {
return moment(value.date);
};

expect(amMoment.preprocessDate(meeting, 'foobar').valueOf()).toEqual(testDate.getTime());
});

Expand Down

0 comments on commit f53e86f

Please sign in to comment.