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

Add Number._parse (0.1c)

- rewritte a part of number for extract the parser patern
- replace the Formatter object by a subobject of baku.string
parent d5455501
var baku = {};
baku.version = '0.1b';
baku.version = '0.1c';
/**
* get message by language
......
baku.number = {
/** défaut 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
*/
parse : function (pattern) {
var groupingSize = 0,
zeroDigitSize = 0,
decimalSize = 0,
decimalZeroSize = 0,
match = pattern.match(/\s*(?:((?:[#,]*)(?:[0,]*))(?:(?:\.([0 ]*#*))|))((?:\s*\%)|)\s*$/),
unit;
if(match && match[0] === pattern) {
var number = match[1],
decimal = match[2] ? match[2].replace(/\s/, '').match(/^(0*)#*/) : null;
unit = match[3];
// digit grouping
groupingSize = number.match(/,?([#]*[0]*)$/)[1].length;
zeroDigitSize = number.match(/[,0]*$/)[0].replace(/,/g, '').length;
// decimal
if (decimal) {
decimalSize = decimal[0].length;
decimalZeroSize = decimal[1].length;
}
}
else {
throw 'patten error: '+pattern;
}
return {
groupingSize : groupingSize,
zeroDigitSize : zeroDigitSize,
decimalSize : decimalSize,
decimalZeroSize : decimalZeroSize,
unit : unit,
};
}
};
/**
* formatage par pattern
* @param pattern de formatage (défaut : '#,###')
......@@ -9,46 +58,10 @@
*/
Number.prototype._formatByPattern = function(pattern, params) {
var params = typeof(params) === 'object' ? params : {},
format = {
groupingSize : 0,
zeroDigitSize : 0,
decimalSize : 0,
decimalZeroSize : 0,
unit : undefined,
dot : params.dot,
space : params.space
},
lg = params.lg || navigator.language;
if(!pattern) {
// partern by default
pattern = '#,###';
}
var match = pattern.match(/\s*(?:((?:[#,]*)(?:[0,]*))(?:(?:\.([0 ]*#*))|))((?:\s*\%)|)\s*$/);
if(match && match[0] === pattern) {
var number = match[1],
decimal = match[2] ? match[2].replace(/\s/, '').match(/^(0*)#*/) : null;
format.unit = match[3];
// digit grouping
format.groupingSize = number.match(/,?([#]*[0]*)$/)[1].length;
format.zeroDigitSize = number.match(/[,0]*$/)[0].replace(/,/g, '').length;
// decimal
if (decimal) {
format.decimalSize = decimal[0].length;
format.decimalZeroSize = decimal[1].length;
}
}
else {
throw 'patten error : '+pattern;
}
if (format.dot === undefined) {
format.dot = baku.lg(lg ,'number.dot');
}
if (format.space === undefined) {
format.space = baku.lg(lg, 'number.space');
}
format = baku.number.parse(pattern || baku.number.formatDefautPattern),
lg = params.lg || navigator.language;
format.dot = params.dot || baku.lg(lg ,'number.dot');
format.space = params.space || baku.lg(lg, 'number.space');
return this._format(format);
};
......@@ -81,8 +94,8 @@ Number.prototype._format = function(format) {
}
// ajoute des espaces
var entier = format.space !== undefined && format.groupingSize && format.groupingSize > 0
? valueAsStr[2].replace(new RegExp('(?=(?:\\d{' + format.groupingSize + '})+$)(?!^)', 'g'), format.space)
: valueAsStr[2];
? valueAsStr[2].replace(new RegExp('(?=(?:\\d{' + format.groupingSize + '})+$)(?!^)', 'g'), format.space)
: valueAsStr[2];
// formatage des décimales
var decimal = '';
......@@ -100,3 +113,29 @@ Number.prototype._format = function(format) {
}
return valueAsStr[1] + entier + decimal + unit;
};
/**
* parse a string for extract a number (if possible)
* @param format
* - dot : decimal separator (default: empty)
* - space : digit grouping separator (ou autre) (défaut: empty)
* - lg : formatage languga (defaut: web browser language)
* @return a number
*/
Number._parse = function (string, params) {
var number,
params = typeof(params) === 'object' ? params : {},
lg = params.lg || navigator.language,
dot = params.dot || baku.lg(lg, 'number.dot'),
space = params.space || baku.lg(lg, 'number.space'),
match = string.match(new RegExp('(\\d*((' + space + ')\\d*)*\\d)((' + dot + ')(\\d*))?', 'g') );
if (match[0]) {
number = +(match[0].replace(new RegExp(space, 'g'), '').replace(dot, '.'));
}
else {
throw 'parsing error: '+string;
}
return number;
};
/**
* {0,choice,1# an|1< ans}
*/
Formatter.choice = function (val, vals, arg) {
baku.string.formatter.choice = function (val, vals, arg) {
var c,
val = typeof(val) === 'string' ? val.trim() : val,
choicesRegex = /\s*([-\da-zA-Z]+)\s*(#|[><]=?)\s*((?:\\.|[^|])+)/g;
......@@ -26,7 +26,7 @@ Formatter.choice = function (val, vals, arg) {
* {0, number, #,##0}
* @required ext.number.js
*/
Formatter.number = function (val, vals, arg) {
baku.string.formatter.number = function (val, vals, arg) {
if(arg === undefined) {
arg = '';
}
......@@ -50,7 +50,7 @@ Formatter.number = function (val, vals, arg) {
* {0,date, dd/MM/yyyy}
* @required ext.date.js
*/
Formatter.date = function (val, vals, arg) {
baku.string.formatter.date = function (val, vals, arg) {
if(arg && arg.indexOf('{') > -1) {
arg = arg._format(vals);
}
......
/** formateur (object avec des fonctions : "func(mixed value):string" ou "func(mixed value, string params):string" */
var Formatter = {};
baku.string = {
parsePattern : /^\s*([^,]+)\s*(?:,\s*([^,]+)\s*(?:,\s*(.*)\s*)?)?$/,
/** formateur (object avec des fonctions : "func(mixed value):string" ou "func(mixed value, string params):string" */
formatter : {}
};
Formatter._parsePattern = /^\s*([^,]+)\s*(?:,\s*([^,]+)\s*(?:,\s*(.*)\s*)?)?$/;
/**
* fonction pour simuler le regex : /(|\\){\s*([^,{}]+)\s*(?:,\s*([^,}]+)\s*)?(?:,\s*((?:(?R)|\\.|[^}])+)\s*)?}/g
* @param str la chaîne à parser
* @param func la function à exécuter sur les balises
* @return la chaine parsée
*/
Formatter._parse = function (str, func) {
baku.string.formatter._parse = function (str, func) {
var text = "", i = 0, openTagCount = 0, closeTagCount = 0, fragment = {};
while ((i = str.indexOf('{' , i)) > -1) {
if (i === 0 || str[i-1] !== '\\') {
......@@ -35,8 +38,8 @@ Formatter._parse = function (str, func) {
}
else if (fragment[position] === -1 && fragment[position] + ct === 0) {
tmp = str.substring(mrq + 1, position);
if (tmp.match(Formatter._parsePattern)) {
text += tmp.replace(Formatter._parsePattern, func);
if (tmp.match(baku.string.parsePattern)) {
text += tmp.replace(baku.string.parsePattern, func);
}
else {
throw 'pattern error';
......@@ -58,7 +61,7 @@ Formatter._parse = function (str, func) {
/**
* formatage par function et paramètres :
* - {key, function, params}
* les functions sont des méthodes de Formatter
* les functions sont des méthodes de baku.string.formatter
* @param liste d'arguments, array ou object
* @return string
*/
......@@ -68,18 +71,18 @@ String.prototype._format = function (){
args = args[0];
}
return Formatter._parse(this,
return baku.string.formatter._parse(this,
/**
* remplace the tag by a formated string
* @param base compled string respect the replace pattern (not used) (see : Formatter._parsePattern )
* @param base compled string respect the replace pattern (not used) (see : baku.string.formatter._parsePattern )
* @param key the key tag (ex. 0 for {0})
* @param func name of function (optional)
* @param params a paramter object (optional)
* @return string
*/
function (base, key, func, params) {
return (func !== undefined && typeof Formatter[func] === 'function')
? ( params !== undefined ? Formatter[func](args[key], args, params) : Formatter[func](args[key], args) )
return (func !== undefined && typeof baku.string.formatter[func] === 'function')
? ( params !== undefined ? baku.string.formatter[func](args[key], args, params) : baku.string.formatter[func](args[key], args) )
: args[key];
}).replace('\\}', '}').replace('\\{', '{');
};
......
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