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

Add display format option for duration + add phantomJS to dev-dep #199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Note: To use `amFromUnix`, install angular-moment version 1.0.0-beta.3 or newer

### 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 @@ -134,7 +134,7 @@ Note: To use `amUtcOffset`, install angular-moment version 1.0.0-beta.3 or newer

### 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 @@ -195,8 +195,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 @@ -207,6 +207,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
13 changes: 11 additions & 2 deletions angular-moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,12 +593,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.