PageSourceSearch

https://www.larec-skazok.ru/vendor/jquery.formatCurrency.js

js larec-skazok.ru collected 2026-10-02 10:26:51 UTC 9,008 bytes, 244 lines download raw bytes

1//  This file is part of the jQuery formatCurrency Plugin.
2//
3//    The jQuery formatCurrency Plugin is free software: you can redistribute it
4//    and/or modify it under the terms of the GNU General Public License as published 
5//    by the Free Software Foundation, either version 3 of the License, or
6//    (at your option) any later version.
7
8//    The jQuery formatCurrency Plugin is distributed in the hope that it will
9//    be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
10//    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11//    GNU General Public License for more details.
12//
13//    You should have received a copy of the GNU General Public License along with 
14//    the jQuery formatCurrency Plugin.  If not, see <http://www.gnu.org/licenses/>.
15
16(function($) {
17
18    $.formatCurrency = {};
19
20    $.formatCurrency.regions = [];
21
22    // default Region is en
23    $.formatCurrency.regions[''] = {
24        symbol: '$',
25        positiveFormat: '%s%n',
26        negativeFormat: '(%s%n)',
27        decimalSymbol: '.',
28        digitGroupSymbol: ',',
29        groupDigits: true
30    };
31
32    $.fn.formatCurrency = function(destination, settings) {
33
34        if (arguments.length == 1 && typeof destination !== "string") {
35            settings = destination;
36            destination = false;
37        }
38
39        // initialize defaults
40        var defaults = {
41            name: "formatCurrency",
42            colorize: false,
43            region: '',
44            global: true,
45            roundToDecimalPlace: 2, // roundToDecimalPlace: -1; for no rounding; 0 to round to the dollar; 1 for one digit cents; 2 for two digit cents; 3 for three digit cents; ...
46            eventOnDecimalsEntered: false
47        };
48        // initialize default region
49        defaults = $.extend(defaults, $.formatCurrency.regions['']);
50        // override defaults with settings passed in
51        settings = $.extend(defaults, settings);
52
53        // check for region setting
54        if (settings.region.length > 0) {
55            settings = $.extend(settings, getRegionOrCulture(settings.region));
56        }
57        settings.regex = generateRegex(settings);
58
59        return this.each(function() {
60            $this = $(this);
61
62            // get number
63            var num = '0';
64            num = $this[$this.is('input, select, textarea') ? 'val' : 'html']();
65
66            //identify '(123)' as a negative number
67            if (num.search('\\(') >= 0) {
68                num = '-' + num;
69            }
70
71            if (num === '' || (num === '-' && settings.roundToDecimalPlace === -1)) {
72                return;
73            }
74
75            // if the number is valid use it, otherwise clean it
76            if (isNaN(num)) {
77                // clean number
78                num = num.replace(settings.regex, '');
79                
80                if (num === '' || (num === '-' && settings.roundToDecimalPlace === -1)) {
81                    return;
82                }
83                
84                if (settings.decimalSymbol != '.') {
85                    num = num.replace(settings.decimalSymbol, '.');  // reset to US decimal for arithmetic
86                }
87                if (isNaN(num)) {
88                    num = '0';
89                }
90            }
91            
92            // evalutate number input
93            var numParts = String(num).split('.');
94            var isPositive = (num == Math.abs(num));
95            var hasDecimals = (numParts.length > 1);
96            var decimals = (hasDecimals ? numParts[1].toString() : '0');
97            var originalDecimals = decimals;
98            
99            // format number
100            num = Math.abs(numParts[0]);
101            num = isNaN(num) ? 0 : num;
102            if (settings.roundToDecimalPlace >= 0) {
103                decimals = parseFloat('1.' + decimals); // prepend "0."; (IE does NOT round 0.50.toFixed(0) up, but (1+0.50).toFixed(0)-1
104                decimals = decimals.toFixed(settings.roundToDecimalPlace); // round
105                if (decimals.substring(0, 1) == '2') {
106                    num = Number(num) + 1;
107                }
108                decimals = decimals.substring(2); // remove "0."
109            }
110            num = String(num);
111
112            if (settings.groupDigits) {
113                for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
114                    num = num.substring(0, num.length - (4 * i + 3)) + settings.digitGroupSymbol + num.substring(num.length - (4 * i + 3));
115                }
116            }
117
118            if ((hasDecimals && settings.roundToDecimalPlace == -1) || settings.roundToDecimalPlace > 0) {
119                num += settings.decimalSymbol + decimals;
120            }
121
122            // format symbol/negative
123            var format = isPositive ? settings.positiveFormat : settings.negativeFormat;
124            var money = format.replace(/%s/g, settings.symbol);
125            money = money.replace(/%n/g, num);
126
127            // setup destination
128            var $destination = $([]);
129            if (!destination) {
130                $destination = $this;
131            } else {
132                $destination = $(destination);
133            }
134            // set destination
135            $destination[$destination.is('input, select, textarea') ? 'val' : 'html'](money);
136
137            if (
138                hasDecimals && 
139                settings.eventOnDecimalsEntered && 
140                originalDecimals.length > settings.roundToDecimalPlace
141            ) {
142                $destination.trigger('decimalsEntered', originalDecimals);
143            }
144
145            // colorize
146            if (settings.colorize) {
147                $destination.css('color', isPositive ? 'black' : 'red');
148            }
149        });
150    };
151
152    // Remove all non numbers from text
153    $.fn.toNumber = function(settings) {
154        var defaults = $.extend({
155            name: "toNumber",
156            region: '',
157            global: true
158        }, $.formatCurrency.regions['']);
159
160        settings = jQuery.extend(defaults, settings);
161        if (settings.region.length > 0) {
162            settings = $.extend(settings, getRegionOrCulture(settings.region));
163        }
164        settings.regex = generateRegex(settings);
165
166        return this.each(function() {
167            var method = $(this).is('input, select, textarea') ? 'val' : 'html';
168            $(this)[method]($(this)[method]().replace('(', '(-').replace(settings.regex, ''));
169        });
170    };
171
172    // returns the value from the first element as a number
173    $.fn.asNumber = function(settings) {
174        var defaults = $.extend({
175            name: "asNumber",
176            region: '',
177            parse: true,
178            parseType: 'Float',
179            global: true
180        }, $.formatCurrency.regions['']);
181        settings = jQuery.extend(defaults, settings);
182        if (settings.region.length > 0) {
183            settings = $.extend(settings, getRegionOrCulture(settings.region));
184        }
185        settings.regex = generateRegex(settings);
186        settings.parseType = validateParseType(settings.parseType);
187
188        var method = $(this).is('input, select, textarea') ? 'val' : 'html';
189        var num = $(this)[method]();
190        num = num ? num : "";
191        num = num.replace('(', '(-');
192        num = num.replace(settings.regex, '');
193        if (!settings.parse) {
194            return num;
195        }
196
197        if (num.length == 0) {
198            num = '0';
199        }
200
201        if (settings.decimalSymbol != '.') {
202            num = num.replace(settings.decimalSymbol, '.');  // reset to US decimal for arthmetic
203        }
204
205        return window['parse' + settings.parseType](num);
206    };
207
208    function getRegionOrCulture(region) {
209        var regionInfo = $.formatCurrency.regions[region];
210        if (regionInfo) {
211            return regionInfo;
212        }
213        else {
214            if (/(\w+)-(\w+)/g.test(region)) {
215                var culture = region.replace(/(\w+)-(\w+)/g, "$1");
216                return $.formatCurrency.regions[culture];
217            }
218        }
219        // fallback to extend(null) (i.e. nothing)
220        return null;
221    }
222
223    function validateParseType(parseType) {
224        switch (parseType.toLowerCase()) {
225            case 'int':
226                return 'Int';
227            case 'float':
228                return 'Float';
229            default:
230                throw 'invalid parseType';
231        }
232    }
233    
234    function generateRegex(settings) {
235        if (settings.symbol === '') {
236            return new RegExp("[^\\d" + settings.decimalSymbol + "-]", "g");
237        }
238        else {
239            var symbol = settings.symbol.replace('$', '\\$').replace('.', '\\.');       
240            return new RegExp(symbol + "|[^\\d" + settings.decimalSymbol + "-]", "g");
241        }   
242    }
243
244})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.