-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathangular-units.js
More file actions
37 lines (37 loc) · 1.06 KB
/
angular-units.js
File metadata and controls
37 lines (37 loc) · 1.06 KB
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
angular.module('angularUnits', [])
.service('unitsConverter', [function() {
var conversionMultipliers = {
'mm': 1.0,
'cm': 0.1,
'm': 0.001,
'km': 0.000001,
'in': 0.0393701,
'ft': 0.00328084,
'yd': 0.00109361,
'mi': 0.000000621371
};
var service = {
convert: function (value, fromUnit, toUnit) {
if (toUnit === undefined) {
toUnit = fromUnit;
fromUnit = 'mm';
}
var fromMultipler = conversionMultipliers[fromUnit];
if (fromMultipler === undefined) {
throw "The unit '" + fromUnit + "' is not recognized.";
}
var toMultipler = conversionMultipliers[toUnit];
if (toMultipler === undefined) {
throw "The unit '" + toUnit + "' is not recognized.";
}
var convertMultiplier = toMultipler / fromMultipler;
return value * convertMultiplier;
}
};
return service;
}])
.filter('units', ['unitsConverter', function (unitsConverter) {
return function (input, desiredUnits) {
return unitsConverter.convert(input, desiredUnits).toFixed(2) + ' ' + desiredUnits;
};
}]);