forked from bcherny/format-as-currency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-as-currency.js
118 lines (91 loc) · 3.5 KB
/
format-as-currency.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
angular
.module('bcherny/formatAsCurrency', [])
.service('formatAsCurrencyUtilities', function () {
// (haystack: String, needles: Array<String>) => Number
// eg. ('foo', 'o') => 2
this.occurrences = function (haystack, needles) {
if (!angular.isString(haystack)) {
throw new TypeError ('formatAsCurrencyUtilities#occurences expects its 1st argument to be a String, but was given ' + haystack)
}
if (!angular.isArray(needles)) {
throw new TypeError ('formatAsCurrencyUtilities#occurences expects its 2nd argument to be an Array, but was given ' + needles)
}
needles.forEach(function (needle, n) {
if (!angular.isString(needle)) {
throw new TypeError ('formatAsCurrencyUtilities#occurences expects needles to be Strings, but needle #' + n + ' is ' + needle)
}
})
return needles
// get counts
.map(function (needle) {
_needle = needle
.replace(/\[/g, '\\[')
.replace(/\]/g, '\\]')
return (
haystack.match(new RegExp('[' + _needle + ']', 'g')) || []
).length
})
// sum counts
.reduce(function (prev, cur) {
return prev + cur
})
}
// (currencyString: String) => Number
// eg. "$123.00" => 123.00
this.toFloat = function (currencyString) {
if (!angular.isString(currencyString)) {
throw new TypeError ('formatAsCurrencyUtilities#toFloat expects its 1st argument to be a String, but was given ' + currencyString)
}
return parseFloat(currencyString.replace(/(\$|\,)+/g, ''), 10)
}
})
.directive('formatAsCurrency', ['$filter', '$locale', 'formatAsCurrencyUtilities', function ($filter, $locale, formatAsCurrencyUtilities) {
var CURRENCY_SYMBOL = $locale.NUMBER_FORMATS.CURRENCY_SYM
var util = formatAsCurrencyUtilities
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, element, _, ngModel) {
ngModel.$formatters.push(function (value) {
return $filter('currency')(value)
})
ngModel.$parsers.push(function (value) {
// ignore non-numeric characters
value = value.replace(/[a-zA-Z!\?>:;\|<@#%\^&\*\)\(\+\/\\={}\[\]_]/g, '')
var number = util
.toFloat(value)
.toFixed(2)
if (ngModel.$validators.currency(number)) {
var formatted = $filter('currency')(number)
var specialCharacters = [',', CURRENCY_SYMBOL]
// did we add a comma or currency symbol?
var specialCharactersCountChange = [value, formatted]
.map(function (string) {
return util.occurrences(string, specialCharacters)
})
.reduce(function (prev, cur) {
return cur - prev
})
// compute the new selection range, correcting for
// formatting introduced by the currency $filter
var selectonRange = [
element[0].selectionStart,
element[0].selectionEnd
].map(function (position) {
return position + specialCharactersCountChange
})
// set the formatted value in the view
ngModel.$setViewValue(formatted)
ngModel.$render()
// set the cursor back to its expected position
// (since $render resets the cursor the the end)
element[0].setSelectionRange(selectonRange[0], selectonRange[1])
}
return number
})
ngModel.$validators.currency = function (modelValue) {
return !isNaN(modelValue)
}
}
}
}])