Commit a8f73cd4 authored by Zéfling's avatar Zéfling 🎨
Browse files

More possibilities for Number.prototype._format (0.1e)

- more comments translated in english
- more tests added
parent 46eb5fe3
var baku = {};
baku.version = '0.1d';
baku.version = '0.1e';
/**
* test if it's a string
......@@ -27,7 +27,7 @@ baku.isArray = function(array) {
* @return merged object
*/
baku.extend = function (base, add) {
if (typeof(base) === 'object' && !(base instanceof Array)) {
if (typeof(base) === 'object' && !(base instanceof Array) && add) {
for(var i in add) {
base[i] = baku.extend(base[i], add[i]);
}
......
baku.number = {
/** défaut pattern for formatting */
/** default pattern for formatting */
formatDefautPattern :'#,###',
/**
* parse the number formatter pattern
* @param pattern formatter pattern (default: '#,###')
* @param params params
* - dot : decimal separator (default: empty)
* - space : digit grouping separator (ou autre) (défaut: empty)
* - lg : formatage languga (defaut: web browser language)
* @return la chaine formatée
* @return formatted string number
*/
parse : function (pattern) {
var groupingSize = 0,
......@@ -49,77 +45,89 @@ baku.number = {
/**
* formatage par pattern
* @param pattern de formatage (défaut : '#,###')
* @param pattern formatter pattern (default: '#,###')
* @param params params
* - dot : forme du séparateur de décimales (défaut : rien)
* - space : sparateur de millier (ou autre) (défaut : rien)
* - lg : langue de formatage (défaut : langue du navigateur)
* @return la chaine formatée
* - dot : symbol of decimal separator (default: empty)
* - space : symbol of grouping digit separator (default: empty)
* - lg : defaults parameters defind by the language (défaut : langue du navigateur)
* @return formatted string number
*/
Number.prototype._formatByPattern = function(pattern, params) {
var params = typeof(params) === 'object' ? params : {},
format = baku.number.parse(pattern || baku.number.formatDefautPattern),
lg = params.lg || navigator.language;
format.dot = params.dot || baku.lg(lg ,'number.dot');
format.dot = params.dot || baku.lg(lg ,'number.dot');
format.space = params.space || baku.lg(lg, 'number.space');
return this._format(format);
};
/**
* formatage suivant paramètre
* @param format
* - groupingSize : séparateur de lisiblité (0 = aucun)
* - zeroDigitSize : nombre de chiffres minimums
* - decimalSize : nombre de décimales affichées
* - decimalZeroSize : nombre de chiffres minimums en décimal
* - dot : forme du séparateur de décimales (défaut : '.')
* - space : spérateur de millier (ou autre) (défaut : '')
* - unit : rien ou %
* - groupingSize : grouping digit size (0 <= not)
* - zeroDigitSize : minimum number of digits
* - decimalSize : number of decimal displayed
* - decimalZeroSize : number of minimum digits in decimal
* - dot : symbol of decimal separator (default: '.')
* - space : symbol of grouping digit separator (default: '')
* - unit : nothing or % (% → val×100, ex.: 1.5 → 150%)
* - lg : defaults parameters defind by the language (only if language is defined)
* @return la chaine formatée
*/
Number.prototype._format = function(format) {
var val = this,
lg = format.lg,
valueAsStr,
format = !format ? {} : format,
unit = format.unit||'',
decimalSize = format.decimalSize > 0 ? format.decimalSize : 0;
format = !format ? {} : format,
unit = format.unit || '',
decimalSize = Math.max(0, format.decimalSize) | 0,
groupingSize= Number.isFinite(format.groupingSize) ? Math.max(0, format.groupingSize) : (lg ? baku.lg(lg, 'number.groupingSize') : 3),
dot = format.dot || (lg ? baku.lg(lg, 'number.dot') : '.'),
space = format.space || (lg ? baku.lg(lg, 'number.space') : ''),
zeroDigitSize = format.zeroDigitSize | 0,
decimalZeroSize = format.decimalZeroSize | 0;
if (unit.trim() === '%') {
val = val * 100;
}
// parsing : number & decimal separation
valueAsStr = new String(Math._roundDecimal(val, decimalSize)).match(/(-|)(\d*)(?:.(\d*))?/);
if (format.zeroDigitSize > 0) {
valueAsStr[2] = valueAsStr[2]._padLeft(format.zeroDigitSize, '0');
if (zeroDigitSize > 0) {
valueAsStr[2] = valueAsStr[2]._padLeft(zeroDigitSize, '0');
}
// ajoute des espaces
var entier = format.space !== undefined && format.groupingSize && format.groupingSize > 0
? valueAsStr[2].replace(new RegExp('(?=(?:\\d{' + format.groupingSize + '})+$)(?!^)', 'g'), format.space)
// groupinp separator
var entier = space !== undefined && groupingSize && groupingSize > 0
? valueAsStr[2].replace(new RegExp('(?=(?:\\d{' + groupingSize + '})+$)(?!^)', 'g'), space)
: valueAsStr[2];
// formatage des décimales
// decimal formating
var decimal = '';
if(format.decimalSize > 0) {
decimal = (valueAsStr[3]||'0').substring(0, format.decimalSize);
if(decimalSize > 0) {
decimal = (valueAsStr[3]||'0').substring(0, decimalSize);
if (decimal === '0') {
decimal = '';
}
if (format.decimalZeroSize > 0) {
decimal = decimal._padRight(format.decimalZeroSize, '0');
if (decimalZeroSize > 0) {
decimal = decimal._padRight(decimalZeroSize, '0');
}
if (decimal !== '') {
decimal = (format.dot||'.') + decimal;
decimal = (dot||'.') + decimal;
}
}
return valueAsStr[1] + entier + decimal + unit;
};
/**
* parse a string for extract a number (if possible)
* parse a number into string:
* - if the string contains no numeric, it will return an error.
* - if it contains a digital, it will extract the numerical first part. For ex.: 'a1b2c' become 1.
* @param format
* - dot : decimal separator (default: empty)
* - space : digit grouping separator (ou autre) (défaut: empty)
* - lg : formatage languga (defaut: web browser language)
* - space : digit grouping separator (ou autre) (default: empty)
* - lg : defaults parameters defind by the language (default: web browser language)
* @return a number
*/
Number._parse = function (string, params) {
......
......@@ -10,4 +10,4 @@ elif [ "$1" = 'dom' ]; then
else
cat baku.js baku.math.js baku.number.js baku.date.js baku.regexp.js baku.string.js baku.string.formater.js | closure-compiler --js_output_file="baku$name.min.js"
fi
sed -i '1i/*! BakuJs v0.1d | (c) Zefling | license, see: github.com/Zefling/BakuJS */' "baku$name.min.js"
\ No newline at end of file
sed -i '1i/*! BakuJs v0.1e | (c) Zefling | license, see: github.com/Zefling/BakuJS */' "baku$name.min.js"
\ No newline at end of file
baku.lg.add('en', {
monthNames : [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
dayNames : [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ],
number : {dot : '.', space : ","}
number : {dot : '.', space : ",", groupingSize : 3}
});
baku.lg.add('fr', {
monthNames : ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'],
dayNames : ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi'],
number : {dot : ',', space : "\u00A0"}
number : {dot : ',', space : "\u00A0", groupingSize : 3}
});
baku.lg.add('ja', {
monthNames : [ '1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月' ],
dayNames : [ '日曜日', '月曜日', '火曜日', '水曜日', '木曜日', '金曜日', '土曜日' ],
number : {dot : '.', space : ","}
number : {dot : '.', space : ",", groupingSize : 3}
});
......@@ -176,7 +176,6 @@ window.onload = function(){
Test.equals("new Number(1.1)._formatByPattern('#,###.00%', {lg : 'fr'})", '110,00%', '1.1 + #,###.00% → 110,00%');
Test.equals("new Number(1.1)._formatByPattern('0,000.00%', {lg : 'en'})", '0,110.00%', '1.1 + #,###.00% → 0,110.00%');
Test.equals("new Number(1.1111)._formatByPattern('0,000.00%', {lg : 'en'})", '0,111.11%', '1.1111 + #,###.00% → 0,111.11%');
Test.equals("new Number(5.5)._formatByPattern('#', {lg : 'fr'})", '6', '5.5 + # → 6');
Test.error("new Number(123.103)._formatByPattern('0#000.##', {lg : 'fr'})");
Test.error("new Number(123.103)._formatByPattern('0000.#0', {lg : 'fr'})");
Test.error("new Number(123.103)._formatByPattern('0000.#0#', {lg : 'fr'})");
......@@ -185,20 +184,28 @@ window.onload = function(){
Test.title("Number.prototype._format()");
Test.equals("new Number(2591.5978)._format({groupingSize : 3, decimalSize : 2, dot : ',', space : ' '})", '2 591,6', "2591.5978 + {groupingSize : 3, decimalSize : 2, dot : ',', space : ' '} → '2 591,6'");
Test.equals("new Number(2591.5178)._format({groupingSize : 3, decimalSize : 1, decimalZeroSize : 2, dot : ',', space : ' '})", '2 591,50', "2591.5178 + {groupingSize : 3, decimalSize : 2, dot : ',', space : ' '} → '2 591,50'"); // cas à con à revoir
Test.equals("new Number(2591.5178)._format({groupingSize : 3, decimalSize : 1, decimalZeroSize : 2, dot : ',', space : ' '})", '2 591,50', "2591.5178 + {groupingSize : 3, decimalSize : 1, decimalZeroSize : 2, dot : ',', space : ' '} → '2 591,50'"); // cas à con à revoir
Test.equals("new Number(2591.5178)._format({groupingSize : 2, decimalSize : 3, decimalZeroSize : 3, dot : '.', space : '_'})", '25_91.518', "2591.5178 + {groupingSize : 2, decimalSize : 3, decimalZeroSize : 3, dot : '.', space : '_'} → '25_91.518'");
Test.equals("new Number(2591.5178)._format({groupingSize : -1, dot : '.', space : ','})", '2592', "2591.5178 + {groupingSize : -1, dot : '.', space : ','} → '2592'");
Test.equals("new Number(2591.5178)._format({lg : 'fr'})", '2\u00A0592', "2591.5178 + {lg : 'fr'} → '2\u00A0591'");
Test.equals("new Number(2591.5178)._format({lg : 'en'})", '2,592', "2591.5178 + {lg : 'en'} → '2,591'");
Test.title("Number._parse()");
Test.equals("Number._parse('1')", '1', "1 → 1");
Test.equals("Number._parse('1,2', {lg : 'fr'})", '1.2', "1.2 → 1.2");
Test.equals("Number._parse('1,2%', {lg : 'fr'})", '1.2', "1.2% → 1.2");
Test.equals("Number._parse('a1b2c', {lg : 'fr'})", '1', "a1b2c → 1");
Test.equals("Number._parse('1,2 avions', {lg : 'fr'})", '1.2', "1.2 avions → 1.2");
Test.equals("Number._parse('1,000.2', {lg : 'en'})", '1000.2', "1,000.2 → 1000.2");
Test.equals("Number._parse('1\u00A0000', {lg : 'fr'})", '1000', "1,000 → 1000");
Test.equals("Number._parse('1 000', {space : ' '})", '1000', "1 000 → 1000");
Test.equals("Number._parse('1 000\u00A0000', {space : [' ', '\u00A0']})", '1000000', "1 000\u00A0000 → 1000000");
Test.equals("Number._parse('1 000\u00A0000', {space : [' ', '\u00A0']})", '1000000', "1 000\u00A0000 → 1000000");
Test.equals("Number._parse('1 000\u00A0000', {space : ' '})", '1000', "1 000\u00A0000 → 1000");
Test.equals("Number._parse('1.2', {dot : '.'})", '1.2', "1.2 → 1.2");
Test.equals("Number._parse('1,2', {dot : ['.', ',']})", '1.2', "1,2 → 1.2");
Test.title("String.prototype._padLeft()");
Test.equals("'1'._padLeft(1, '.')", '1', "1 + left(1, '') → 1");
Test.equals("'1'._padLeft(2, '.')", '.1', "1 + left(1, '.') → .1");
......@@ -261,7 +268,7 @@ window.onload = function(){
Test.equals("'{0, number, #,###.00:en}'._format(1000.10)", '1,000.10', '1000.10 + {0, number, #,###.00:en} → 1,000.10');
Test.equals("'{0, number, {1}}'._format([1, '#,###.00:fr'])", '1,00', '[1, #,###.00:fr] + {0, number, {1}} → 1,00');
Test.equals("'{0, number, {1}}'._format([1, '#,###.00%:fr'])",'100,00%', '[1, #,###.00%:fr] + {0, number, {1}} → 100,00%');
Test.equals("'{0, number, {1}}'._format(['1a', '#,###'])", '1', '[a, #,###.00%:fr] + {0, number, {1}} → 100,00%');
Test.equals("'{0, number, {1}}'._format(['1a', '#,###'])", '1', '[1a, #,###.00%:fr] + {0, number, {1}} → 100,00%');
Test.error("'{0, number, {1}}'._format(['a', '#,###.00%:fr'])");
Test.title("String.prototype._format() + choice");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment