[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 /* 2 YUI 3.17.2 (build 9c3c78e) 3 Copyright 2014 Yahoo! Inc. All rights reserved. 4 Licensed under the BSD License. 5 http://yuilibrary.com/license/ 6 */ 7 8 YUI.add('datatype-number-parse', function (Y, NAME) { 9 10 /** 11 * Parse number submodule. 12 * 13 * @module datatype-number 14 * @submodule datatype-number-parse 15 * @for Number 16 */ 17 18 var safe = Y.Escape.regex, 19 SPACES = '\\s*'; 20 21 Y.mix(Y.namespace("Number"), { 22 /** 23 * Returns a parsing function for the given configuration. 24 * It uses `Y.cached` so it expects the format spec separated into 25 * individual values. 26 * The method further uses closure to put together and save the 27 * regular expresssion just once in the outer function. 28 * 29 * @method _buildParser 30 * @param [prefix] {String} Prefix string to be stripped out. 31 * @param [suffix] {String} Suffix string to be stripped out. 32 * @param [separator] {String} Thousands separator to be stripped out. 33 * @param [decimal] {String} Decimal separator to be replaced by a dot. 34 * @return {Function} Parsing function. 35 * @private 36 */ 37 _buildParser: Y.cached(function (prefix, suffix, separator, decimal) { 38 var regexBits = [], 39 regex; 40 41 if (prefix) { 42 regexBits.push('^' + SPACES + safe(prefix) + SPACES); 43 } 44 if (suffix) { 45 regexBits.push(SPACES + safe(suffix) + SPACES + '$'); 46 } 47 if (separator) { 48 regexBits.push(safe(separator) + '(?=\\d)'); 49 } 50 51 regex = new RegExp('(?:' + regexBits.join('|') + ')', 'g'); 52 53 if (decimal === '.') { 54 decimal = null; 55 } 56 return function (val) { 57 val = val.replace(regex, ''); 58 59 return decimal ? val.replace(decimal, '.') : val; 60 }; 61 }), 62 /** 63 * Converts data to type Number. 64 * If a `config` argument is used, it will strip the `data` of the prefix, 65 * the suffix and the thousands separator, if any of them are found, 66 * replace the decimal separator by a dot and parse the resulting string. 67 * Extra whitespace around the prefix and suffix will be ignored. 68 * 69 * @method parse 70 * @param data {String | Number | Boolean} Data to convert. The following 71 * values return as null: null, undefined, NaN, "". 72 * @param [config] {Object} Optional configuration values, same as for [Y.Date.format](#method_format). 73 * @param [config.prefix] {String} String to be removed from the start, like a currency designator "$" 74 * @param [config.decimalPlaces] {Number} Ignored, it is accepted only for compatibility with [Y.Date.format](#method_format). 75 * @param [config.decimalSeparator] {String} Decimal separator. 76 * @param [config.thousandsSeparator] {String} Thousands separator. 77 * @param [config.suffix] {String} String to be removed from the end of the number, like " items". 78 * @return {Number} A number, or null. 79 */ 80 81 parse: function(data, config) { 82 var parser; 83 84 if (config && typeof data === 'string') { 85 parser = this._buildParser(config.prefix, config.suffix, config.thousandsSeparator, config.decimalSeparator); 86 87 data = parser(data); 88 } 89 90 if (typeof data === 'string' && Y.Lang.trim(data) !== '') { 91 data = +data; 92 } 93 94 // catch NaN and ±Infinity 95 if (typeof data !== 'number' || !isFinite(data)) { 96 data = null; 97 } 98 99 // on the same line to get stripped for raw/min.js by build system 100 if (data === null) { Y.log("Could not parse data to type Number", "warn", "number"); } 101 102 return data; 103 } 104 }); 105 106 // Add Parsers shortcut 107 Y.namespace("Parsers").number = Y.Number.parse; 108 Y.namespace("DataType"); 109 Y.DataType.Number = Y.Number; 110 111 112 }, '3.17.2', {"requires": ["escape"]});
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |