forked from bcherny/format-as-currency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-as-currency.js
229 lines (188 loc) · 7.68 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
angular
.module('jmanoto/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', function ($filter, $locale, formatAsCurrencyUtilities) {
var CURRENCY_SYMBOL = $locale.NUMBER_FORMATS.CURRENCY_SYM
var util = formatAsCurrencyUtilities;
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, element, attrs, ngModel) {
var isAngular13 = (angular.version.major === 1 && angular.version.minor <= 3);
var lastChar = null;
ngModel.$formatters.push(function (value) {
if (value === 0.0 || isNaN(value)) {
if (lastChar === ZERO_ALPHA || lastChar === ZERO_NUMERIC) {
return '$0';
} else if (lastChar === PERIOD_ALPHA || lastChar === PERIOD_NUMERIC){
return '$0.';
} else {
return '$';
}
} else {
var formatted = $filter('currency')(value);
// console.log("formatting", formatted, value);
if (formatted.indexOf('.00') >= 0 && (lastChar != PERIOD_ALPHA && lastChar != PERIOD_NUMERIC)) {
// console.log("Removing cents");
formatted = formatted.substring(0, formatted.indexOf('.'));
}
return formatted;
}
})
ngModel.$parsers.push(function (value) {
// console.log("parsing", value);
var number = util
.toFloat(value)
.toFixed(2)
var isValueNumber = isAngular13 ? !isNaN(number) : ngModel.$validators.currency(number);
// console.log('parser', ngModel.$validators.currency(number), isValueNumber, number);
if (isValueNumber) {
// if (isValueNumber) {
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
});
if (formatted === '$0.00') { formatted = '$' }
if (formatted.indexOf('.00') > 0 && (lastChar != PERIOD_ALPHA && lastChar != PERIOD_NUMERIC)) {
formatted = formatted.substring(0, formatted.indexOf('.'));
}
// 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)
('setSelectionRange' in element[0]) && element[0].setSelectionRange(selectonRange[0], selectonRange[1])
}
return number
});
var BACKSPACE = 8;
var DELETE = 46;
var PERIOD_ALPHA = 190;
var PERIOD_NUMERIC = 110;
var ZERO_ALPHA = 48;
var ZERO_NUMERIC = 96;
// Handle keyboard presses
element.on('keydown', function(event) {
var charCode = event.which || event.keyCode;
lastChar = charCode;
// console.log("Last Char: ", lastChar);
switch (charCode) {
case BACKSPACE: // Backspace
var pointIndex = element[0].value.indexOf('.');
if (element[0].selectionStart === element[0].selectionEnd && element[0].selectionStart === pointIndex + 1) {
// console.log("Backspacing Point", pointIndex, element[0].selectionStart);
element[0].value = element[0].value.substring(0, pointIndex);
ngModel.$setViewValue(element[0].value);
return false;
}
break;
case DELETE: // Delete
var pointIndex = element[0].value.indexOf('.');
if (element[0].selectionStart === element[0].selectionEnd && element[0].selectionStart === pointIndex) {
// console.log("Deleting Point", pointIndex, element[0].selectionStart);
element[0].value = element[0].value.substring(0, pointIndex);
ngModel.$setViewValue(element[0].value);
return false;
}
break;
case PERIOD_NUMERIC:
case PERIOD_ALPHA:
if (element[0].value.indexOf('.') < 0) {
// Check if a zero should be prepended
if (element[0].value === '$') {
element[0].value = '$0';
}
// No period existing
element[0].value = element[0].value.substring(0, element[0].selectionStart) + '.00';
element[0].selectionStart = element[0].selectionEnd = element[0].value.length - '00'.length;
return false;
} else {
// Already has period
element[0].value = element[0].value.substring(0, element[0].selectionStart) + '.00';
element[0].selectionStart = element[0].selectionEnd = element[0].value.length - '00'.length;
return true;
}
break;
case ZERO_NUMERIC:
case ZERO_ALPHA:
if (element[0].value.indexOf('.') >= 0) {
if (element[0].selectionStart > element[0].value.indexOf('.')) {
element[0].selectionStart = element[0].selectionStart + 1;
return false;
}
}
break;
}
return true;
});
// Version switch
if (angular.version.major === 1 && angular.version.minor <= 2) {
// Old version (angular 1.2)
scope.$watch(function() {
// console.log("watching", ngModel.$modelValue);
return !isNaN(ngModel.$modelValue);
}, function(validity) {
// console.log("setting validity", validity)
ngModel.$setValidity('formatAsCurrency', validity);
});
} else {
// Modern Angular version
ngModel.$validators.currency = function (modelValue) {
return !isNaN(modelValue)
}
}
}
}
})