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

Commit

Permalink
Add display format option for duration + add phantomJS to dev-dep
Browse files Browse the repository at this point in the history
  • Loading branch information
pdesgarets committed Nov 9, 2015
1 parent e2a17cb commit fe563f4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Note: To use `amFromUnix`, install angular-moment version 1.0.0-beta.2

### amUtc filter

Create / switch the current moment object into UTC mode. For example, given a date object in `message.date`,
Create / switch the current moment object into UTC mode. For example, given a date object in `message.date`,
the following code will display the time in UTC instead of the local timezone:

```html
Expand All @@ -113,7 +113,7 @@ Note: To use `amUtcOffset`, install angular-moment version 1.0.0-beta.2

### amLocal filter

Changes the given moment object to be in the local timezone. Usually used in conjunction with `amUtc` / `amTimezone`
Changes the given moment object to be in the local timezone. Usually used in conjunction with `amUtc` / `amTimezone`
for timezone conversion. For example, the following will convert the given UTC date to local time:

```html
Expand Down Expand Up @@ -174,8 +174,8 @@ For more information about Moment.JS difference function, see the

### amDurationFormat filter

Formats a duration (such as 5 days) in a human readable format. See [Moment.JS documentation](http://momentjs.com/docs/#/durations/creating/)
for a list of supported duration formats, and [`humanize() documentation`](http://momentjs.com/docs/#/durations/humanize/)
Formats a duration (such as 5 days) in a human readable format. If a display format is provided (as third argument), duration is formatted according to this argument instead of being humanized. See [Moment.JS documentation](http://momentjs.com/docs/#/durations/creating/)
for a list of supported duration formats, and [`humanize() documentation`](http://momentjs.com/docs/#/durations/humanize/)
for explanation about the formatting algorithm.

Example:
Expand All @@ -186,6 +186,11 @@ Example:

Will display the age of the message (e.g. 10 minutes, 1 hour, 2 days, etc).

```html
<span>Next train in {{train.nextDuration | amDurationFormat : 'seconds':undefined:'minutes' }} minutes</span>

Will display "Next train in 3 minutes" if train.nextDuration is 190.

### amSubtract filter

Subtract values (hours, minutes, seconds ...) from a specified date.
Expand Down
17 changes: 13 additions & 4 deletions angular-moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
function isUndefinedOrNull(val) {
return angular.isUndefined(val) || val === null;
}

function requireMoment() {
try {
return require('moment'); // Using nw.js or browserify?
Expand All @@ -19,7 +19,7 @@
}

function angularMoment(angular, moment) {

if(typeof moment === 'undefined') {
if(typeof require === 'function') {
moment = requireMoment();
Expand Down Expand Up @@ -583,12 +583,21 @@
* @function
*/
.filter('amDurationFormat', ['moment', 'angularMomentConfig', function (moment, angularMomentConfig) {
function amDurationFormatFilter(value, format, suffix) {
function amDurationFormatFilter(value, format, suffix, displayFormat) {
var units = ['milliseconds', 'seconds', 'minutes', 'hours', 'days', 'months', 'years'];
var asUnits = units.map(function (unit) {
return 'as' + unit.charAt(0).toUpperCase() + unit.slice(1);
});
var validDisplayFormats = units.concat(asUnits);

if (isUndefinedOrNull(value)) {
return '';
}
if (isUndefinedOrNull(displayFormat) || validDisplayFormats.indexOf(displayFormat) < 0) {
return moment.duration(value, format).humanize(suffix);
}

return moment.duration(value, format).humanize(suffix);
return moment.duration(value, format)[displayFormat]();
}

amDurationFormatFilter.$stateful = angularMomentConfig.statefulFilters;
Expand Down
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.

0 comments on commit fe563f4

Please sign in to comment.