-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-moment-duration-format.js
146 lines (130 loc) · 4.33 KB
/
angular-moment-duration-format.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* angular-moment-duration-format.js / v0.1.0 / (c) 2017 Vincenzo Carrese / MIT Licence */
'format amd';
/* global define */
(function () {
'use strict';
function requireMoment() {
try {
return require('moment'); // Using nw.js or browserify?
} catch (e) {
throw new Error('Please install moment via npm. Please reference to: https://github.com/vin-car/angular-moment-duration-format#Installation');
}
}
function angularDurationFormat(angular, moment) {
if(typeof moment === 'undefined') {
if(typeof require === 'function') {
moment = requireMoment();
}else{
throw new Error('Moment cannot be found by angular-moment! Please reference to: https://github.com/vin-car/angular-moment-duration-format#Installation');
}
}
function createDuration(input, unit) {
var duration;
if( moment.isDuration(input) ){
duration = input;
} else {
duration = moment.duration(input, unit);
}
return duration;
}
/**
* @ngdoc overview
* @name angularDurationFormat
*
* @description
* angularDurationFormat module provides moment-duration-format functionality for angular.js apps.
*/
angular.module('angularDurationFormat', [])
.filter('amdCreate', function() {
/**
* @param {(number|Object|string)} input - value from wich create duration
* @param {string} [unit] - unit of measurement for input parameter
*/
return function(input, unit) {
return createDuration(input, unit).toISOString();
};
})
/**
* @ngdoc filter
* @name angularDurationFormat.filter:amdAdd
* @module angularDurationFormat
* @function
*/
.filter('amdAdd', function() {
/**
* @param {(number|Object|string)} value - value to add to input duration
* @param {string} [unit] - unit of measurement for value parameter
*/
return function(input, value, unit) {
var duration = createDuration(input);
var out = duration.add(value, unit);
return out.toISOString();
};
})
/**
* @ngdoc filter
* @name angularDurationFormat.filter:amdSubtract
* @module angularDurationFormat
* @function
*/
.filter('amdSubtract', function() {
/**
* @param {(number|Object|string)} value - value to subtract to input duration
* @param {string} [unit] - unit of measurement for value parameter
*/
return function(input, value, unit) {
var duration = createDuration(input);
var out = duration.subtract(value, unit);
return out.toISOString();
};
})
/**
* @ngdoc filter
* @name angularDurationFormat.filter:amdHumanize
* @module angularDurationFormat
* @function
*/
.filter('amdHumanize', function() {
/**
* @param {string} [unit] - unit of measurement for input parameter
*/
return function(input, unit) {
var duration = createDuration(input, unit);
var out = duration.humanize();
return out;
};
})
/**
* @ngdoc filter
* @name angularDurationFormat.filter:amdFormat
* @module angularDurationFormat
* @function
*/
.filter('amdFormat', function() {
/**
* @param {string} [template] - Output template for duration
* @param {number} [precision] - number of decimal digits to include after the decimal point
* @param {Object} [settings] - settings object
* template optional
*/
return function(input, template, precision, settings) {
var out = '';
if( !input ){
return out;
}
var duration = createDuration(input);
out = duration.format(template, precision, settings);
return out;
};
});
return 'angularDurationFormat';
}
var isElectron = window && window.process && window.process.type;
if (typeof define === 'function' && define.amd) {
define(['angular', 'moment'], angularDurationFormat);
} else if (typeof module !== 'undefined' && module && module.exports && (typeof require === 'function') && !isElectron) {
module.exports = angularDurationFormat(require('angular'), require('moment'));
} else {
angularDurationFormat(angular, (typeof global !== 'undefined' ? global : window).moment);
}
})();