-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue-mny.js
80 lines (74 loc) · 2.16 KB
/
vue-mny.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
module.exports = {
mny: function(optionsUser) {
const extend = function() {
let out = {};
for (let i = 0, len = arguments.length; i < len; ++i) {
let obj = arguments[i];
if (!obj) continue;
for (let key in obj) {
if (!obj.hasOwnProperty(key) || !obj[key]) continue;
if (Object.prototype.toString.call(obj[key]) === "[object Object]") {
out[key] = extend(out[key], obj[key]);
continue;
}
out[key] = obj[key];
}
}
return out;
};
const optionsGlobal = extend(
{
locale: "en-US",
style: "currency",
currency: "USD",
currencyDisplay: "symbol",
},
optionsUser
);
const moneyfy = (r, options) => {
let n = Number(r);
if (isNaN(n) || !isFinite(n)) return '[invalid]';
return new Intl.NumberFormat(options.locale, {
style: options.style,
currency: options.currency,
currencyDisplay: options.currencyDisplay
}).format(n);
};
const modsToOpts = mods => {
let r = {};
let y = {
symbol: "currencyDisplay",
code: "currencyDisplay"
};
for (let i in mods)
if (y.hasOwnProperty(mods[i])) r[y[mods[i]]] = mods[i];
return r;
};
return (str, optionsFilter = {}, mods = {}) => {
return moneyfy(
str,
extend(optionsGlobal, optionsFilter, modsToOpts(mods))
);
};
},
install: function(Vue, optionsUser) {
Vue.directive("mny", (el, binding) => {
let mny = module.exports.mny(optionsUser);
let mods = Object.keys(binding.modifiers).filter(function(k) {
return binding.modifiers[k];
});
let bval = binding.value;
let returnString = "[invalid]";
if (
typeof bval == "number" ||
(typeof bval == "object" && bval instanceof Number) ||
typeof bval == "string" ||
typeof bval == "integer"
)
returnString = mny(bval, {}, mods);
if (typeof bval == "object" && bval.hasOwnProperty("input"))
returnString = mny(bval.input, bval, mods);
el.innerHTML = returnString;
});
}
};