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

Code review and increase the parse number possibilities (0.1d)

- add baku.regexp.js file
- add more tests
parent 4e6960f5
......@@ -86,7 +86,7 @@ var _dateFormatters = {
h : [/(^|[^\\])h/ , function (d) {return d.getHours() > 12 ? d.getHours() % 12 : (d.getHours() === 0 ? 12 : d.getHours());}],
mm : [/(^|[^\\])mm/ , function (d) {return new String(d.getMinutes())._padLeft(2, '0');}],
m : [/(^|[^\\])m/ , function (d) {return d.getMinutes();}],
ss : [/(^|[^\\])ss/ , function (d) {return new Sring(d.getSeconds())._padLeft(2, '0');}],
ss : [/(^|[^\\])ss/ , function (d) {return new String(d.getSeconds())._padLeft(2, '0');}],
s : [/(^|[^\\])s/ , function (d) {return d.getSeconds();}],
SSS : [/(^|[^\\])S{3,}/ , function (d) {return new String(d.getMilliseconds())._padLeft(3, '0');}],
S : [/(^|[^\\])S+/ , function (d) {return new String(d.getMilliseconds());}]
......
var baku = {};
baku.version = '0.1c';
baku.version = '0.1d';
/**
* get message by language
* @param lg language id
* @param path path of message
* @return message or 'undefined' if not found
*/
baku.lg = function(lg, path) {
var text;
if (this.lg.lg[lg]) {
text = baku.get(this.lg.lg[lg], path.split('.'));
}
// if not found use the default language
if (text === undefined) {
text = baku.get(this.lg.lg[this.lg.default], path.split('.'));
}
return text;
};
/** language base */
baku.lg.lg = {};
/**
* add or extend a language object
* @param id language id
* @param lg object this language informations
* test if it's a string
* @param string object test
* @return true is string is a string object
*/
baku.lg.add = function (id, lg) {
// first if the defaut language
if (!this.default) {
this.default = id;
}
this.lg[id] = this.lg[id] ? baku.extend(this.lg[id], lg) : lg;
baku.isString = function(string) {
return typeof(string) === 'string' || string instanceof String;
};
/**
* test if it's a string
* @param s object test
* @return true is s is a string object
* test if it's a array
* @param array object test
* @return true is s is a array object
*/
baku.isString= function(s) {
return typeof(s) === 'string' || s instanceof String;
baku.isArray = function(array) {
return typeof(array) === 'object' || array instanceof Array;
};
/**
......@@ -77,4 +52,38 @@ baku.get = function (o, path) {
val = base;
}
return val;
};
\ No newline at end of file
};
/**
* get message by language
* @param lg language id
* @param path path of message
* @return message or 'undefined' if not found
*/
baku.lg = function(lg, path) {
var text;
if (this.lg.lg[lg]) {
text = baku.get(this.lg.lg[lg], path.split('.'));
}
// if not found use the default language
if (text === undefined) {
text = baku.get(this.lg.lg[this.lg['default']], path.split('.'));
}
return text;
};
/** language base */
baku.lg.lg = {};
/**
* add or extend a language object
* @param id language id
* @param lg object this language informations
*/
baku.lg.add = function (id, lg) {
// first if the defaut language
if (!this['default']) {
this['default'] = id;
}
this.lg[id] = this.lg[id] ? baku.extend(this.lg[id], lg) : lg;
};
......@@ -41,7 +41,7 @@ baku.number = {
zeroDigitSize : zeroDigitSize,
decimalSize : decimalSize,
decimalZeroSize : decimalZeroSize,
unit : unit,
unit : unit
};
}
};
......@@ -126,12 +126,11 @@ 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'),
dot = RegExp._toRegexString(params.dot || baku.lg(lg, 'number.dot')),
space = RegExp._toRegexString(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, '.'));
number = +(match[0].replace(new RegExp(space, 'g'), '').replace(new RegExp(dot), '.'));
}
else {
throw 'parsing error: '+string;
......@@ -139,3 +138,14 @@ Number._parse = function (string, params) {
return number;
};
/**
* test if the value is a finite number
* @param value testing value
* @return true if a finite number
*/
if (!Number.isFinite) {
Number.isFinite = function(value) {
return typeof value === "number" && isFinite(value);
}
}
......@@ -36,13 +36,17 @@ baku.string.formatter.number = function (val, vals, arg) {
else if(arg.indexOf('{') > -1) {
arg = arg._format(vals);
}
// recupération de la langue : #,##0:fr
// get the language : #,##0:fr
var params = {},
match = arg.match(/([^:]*)(?::(([a-z]{2})(-[A-Z]{2})?)|)/);
if(match && match[2]) {
params.lg = match[3];
params.local = match[2];
}
// if is not number, transform the val in number
if (!Number.isFinite(val)) {
val = Number._parse(val)
}
return val._formatByPattern(match[1], params);
};
......
......@@ -10,6 +10,7 @@
<script type="text/javascript" src="baku.dom.js"></script>
<script type="text/javascript" src="baku.math.js"></script>
<script type="text/javascript" src="baku.number.js"></script>
<script type="text/javascript" src="baku.regexp.js"></script>
<script type="text/javascript" src="baku.string.js"></script>
<script type="text/javascript" src="baku.string.formater.js"></script>
<script type="text/javascript" src="test.js"></script>
......
......@@ -42,7 +42,7 @@ Test.error = function (test) {
window.onload = function(){
Test.title("Date._getWeek()");
Test.title("Date.prototype._getWeek()");
Test.equals("new Date('2013-12-21')._getWeek()", 51,'week : 2013-12-21 → 51');
Test.equals("new Date('2013-12-22')._getWeek()", 51,'week : 2013-12-22 → 51');
Test.equals("new Date('2013-12-23')._getWeek()", 52,'week : 2013-12-23 → 52');
......@@ -138,10 +138,14 @@ window.onload = function(){
Test.equals("new Date('2016-01-03')._getWeek(0)", 2, 'week : 2016-01-03 → 2');
Test.equals("new Date('2016-01-04')._getWeek(0)", 2, 'week : 2016-01-04 → 2');
Test.title("Date._toStringFormat()");
Test.equals("new Date('2012-12-21')._toStringFormat('MMMM M m', 'fr')", 'décembre 12 0', '2012-12-21 + "MMMM M m" → "décembre 12 0"');
Test.title("Date.prototype._toStringFormat()");
Test.equals("new Date('2012-12-21')._toStringFormat('dd/MM/yyyy', 'fr')", '21/12/2012', '2012-12-21 + "Mdd/MM/yyyy" → "21/12/2012"');
Test.equals("new Date('2012-12-21')._toStringFormat('MMMM M m', 'fr')", 'décembre 12 0', '2012-12-21 + "MMMM M m" → "décembre 12 0"');
Test.equals("new Date('2012-12-19')._toStringFormat('EEEE EEE W', 'en')", 'Wednesday Wed W', '2012-12-21 + "EEEE EEE W" → "Wednesday Wed W"');
Test.equals("new Date('2012-12-21T13:14:15.678')._toStringFormat('HH:mm:ss.SSS', 'fr')", '13:14:15.678', '2012-12-21T13:14:15.678 + "HH:mm:ss.SSS" → "13:14:15.678"');
Test.equals("new Date('2012-12-21T03:04:05.078')._toStringFormat('H:m:s.S', 'fr')", '3:4:5.78', '2012-12-21T03:04:05.078 + "H:m:s.S" → "3:4:5.78"');
Test.title("Number._formatByPattern()");
Test.title("Number.prototype._formatByPattern()");
Test.equals("new Number(1)._formatByPattern('#,###', {lg : 'fr'})", '1', '1 + #,### → 1');
Test.equals("new Number(1)._formatByPattern('#,##0', {lg : 'fr'})", '1', '1 + #,##0 → 1');
Test.equals("new Number(1)._formatByPattern('#,#00', {lg : 'fr'})", '01', '1 + #,#00 → 01');
......@@ -179,11 +183,23 @@ window.onload = function(){
Test.error("new Number(123.103)._formatByPattern('0000.#0%', {lg : 'fr'})");
Test.title("Number._format()");
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.title("String._padLeft()");
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('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.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");
Test.equals("'1'._padLeft(10, '.')", '.........1', "1 + left(10, '.') → ........1");
......@@ -206,7 +222,7 @@ window.onload = function(){
Test.equals("'12345'._padLeft(6, 'ABCD')", 'D12345', "12345 + left(6, 'ABCD') → D12345");
Test.equals("'12345'._padLeft(10,'ABCD')", 'DABCD12345', "12345 + left(10, 'ABCD') → DABCD12345");
Test.title("String._padRight()");
Test.title("String.prototype._padRight()");
Test.equals("'1'._padRight(1, '.')", '1', "1 + right(1, '') → 1");
Test.equals("'1'._padRight(2, '.')", '1.', "1 + right(1, '.') → 1.");
Test.equals("'1'._padRight(10, '.')", '1.........', "1 + right(10, '.') → 1........");
......@@ -224,7 +240,7 @@ window.onload = function(){
Test.equals("'12345'._padRight(6, 'ABCD', 'l')",'12345D', "12345 + left(6, 'ABCD', 'l') → 12345D");
Test.equals("'12345'._padRight(10,'ABCD', 'l')",'12345DABCD', "12345 + left(10, 'ABCD', 'l') → 12345DABCD");
Test.title("String._format()");
Test.title("String.prototype._format()");
Test.equals("'a'._format(1)", 'a', '1 + a → a');
Test.equals("'{0}'._format(1)", '1', '1 + {0} → 1');
Test.equals("'a{0}'._format(1)", 'a1', '1 + a{0} → a1');
......@@ -238,15 +254,17 @@ window.onload = function(){
Test.error("'{0}{0\\\\}{0}'._format(1)");
Test.error("'{0}{0,}{0}'._format(1)");
Test.title("String._format() + number");
Test.title("String.prototype._format() + number");
Test.equals("'{0, number, #,###}'._format(1)", '1', '1 + {0, number, #,###} → 1');
Test.equals("'{0, number, #,###:fr}'._format([1000.10])", '1\u00A0000', '[1000.10] + {0, number, #,###:fr} → 1\u00A0000');
Test.equals("'{0, number, #,###.00:fr}'._format(1000.10)", '1\u00A0000,10', '1000.10 + {0, number, #,###.00:fr} → 1\u00A0000,00');
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([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.error("'{0, number, {1}}'._format(['a', '#,###.00%:fr'])");
Test.title("String._format() + choice");
Test.title("String.prototype._format() + choice");
Test.equals("'{0, choice, 1# true|2# false}'._format(1)", 'true', '1 + {0, choice, 1# true|2# false} → true');
Test.equals("'{0, choice, 1# true\\\\|true|2# false}'._format(1)", 'true|true', '1 + {0, choice, 1# true\\|true|2# false} → true|true');
Test.equals("'{0, choice, 1# false|2# true}'._format(2)", 'true', '2 + {0, choice, 1# false|2# true} → true');
......@@ -256,7 +274,7 @@ window.onload = function(){
Test.equals("'{val, choice, this# \\\\{1\\\\}|other#\\\\{0\\\\}}'._format({val : 'this'})", '{1}', '{val : \'this\'} + {val, choice, this# \\{1\\}|other#\\{0\\}} → true');
Test.equals("'{0, choice, 1#{1}|2#{2}}'._format([1, 2, 3])", '2', '[1, 2, 3] + {0, choice, 1#{1}|2#{2}} → 2');
Test.title("String._format() + date");
Test.title("String.prototype._format() + date");
Test.equals("'{0, date, d/M/yy}'._format('2015-02-08')", '8/2/15', '2015-02-08 + {0, date, d/M/yy} → 8/2/15');
Test.equals("'{0, date, dd/MM/yyyy}'._format('2015-10-20')", '20/10/2015', '2015-10-20 + {0, date, dd/MM/yyyy} → 20/10/2015');
Test.equals("'{0, date, w-yyyy}'._format('2015-10-20')", '43-2015', '2015-10-20 + {0, date, w-yyyy} → 43-2015');
......
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