-
Notifications
You must be signed in to change notification settings - Fork 3
/
vue-units.js
66 lines (57 loc) · 1.78 KB
/
vue-units.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
(function () {
/**
* Install plugin
* @param Vue
* @param options
*/
function plugin(Vue, options) {
/*
* Check if plugin is already installed
*/
if (plugin.installed) {
console.warn('It seems like you\'re trying to install vue-units twice.')
return;
}
plugin.installed = true;
/*
* Override the convert-units plugin, if another is defined
*/
var units = options && options.units ? options.units : require('convert-units');
/*
* Make the instance of convert-units available on the vue instance
* using this.$units.
*/
Object.defineProperties(Vue.prototype, {
$units: {
get: function () {
return units;
}
}
});
/*
* Make the instance of convert units available on the global
* Vue instance.
*/
Vue.units = units;
/*
* Add a simple filter for converting units
*/
Vue.filter('units', function (value, from, to, includeLabel) {
try {
return units(value).from(from).to(to) + (includeLabel ? ' ' + to : '');
} catch (err) {
// Prevent Vue from crashing if incorrect metrics are provided
// and simply return the original value instead.
console.error(err);
return value + (includeLabel ? ' ' + from : '');
}
});
}
if (typeof exports == "object") {
module.exports = plugin
} else if (typeof define == "function" && define.amd) {
define([], function(){ return plugin })
} else if (window.Vue) {
Vue.use(plugin)
}
})();