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

More tests and controls (0.1g)

parent 0756ecc6
var baku = {};
baku.version = '0.1f';
baku.version = '0.1g';
/**
* test if it's a string
......
......@@ -8,6 +8,9 @@ baku.number = {
* @return formatted string number
*/
parse : function (pattern) {
if (!baku.isString(pattern)) {
throw 'patten error : is not a string';
}
var groupingSize = 0,
zeroDigitSize = 0,
decimalSize = 0,
......@@ -131,17 +134,22 @@ Number.prototype._format = function(format) {
* @return a number
*/
Number._parse = function (string, params) {
var number,
if (!baku.isString(string)) {
throw 'not a string';
}
var number = '',
params = typeof(params) === 'object' ? params : {},
lg = params.lg || navigator.language,
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(new RegExp(dot), '.'));
}
else {
throw 'parsing error: '+string;
if (match) {
if (match[0]) {
number = +(match[0].replace(new RegExp(space, 'g'), '').replace(new RegExp(dot), '.'));
}
else {
throw 'parsing error: '+string;
}
}
return number;
......
......@@ -55,8 +55,9 @@ baku.string.formatter.number = function (val, vals, arg) {
* @required ext.date.js
*/
baku.string.formatter.date = function (val, vals, arg) {
if(arg && arg.indexOf('{') > -1) {
if(arg !== "undefined" && arg.indexOf('{') > -1) {
arg = arg._format(vals);
}
return arg !== undefined ? new Date(val)._toStringFormat(arg) : '';
var date = new Date(val);
return arg !== "undefined" && !isNaN(date.getTime()) ? date._toStringFormat(arg) : '';
};
......@@ -67,7 +67,10 @@ baku.string.formatter._parse = function (str, func) {
*/
String.prototype._format = function (){
var args = arguments, str;
if (typeof args[0] === 'array' || typeof args[0] === 'object') {
if(args.length === 0) {
return '';
}
else if (typeof args[0] === 'array' || typeof args[0] === 'object') {
args = args[0];
}
......@@ -91,14 +94,17 @@ String.prototype._format = function (){
/**
* ajout d'une chaine répétée au début jusqu'à la longeur désirée
* @param n taille de chaîne désirée
* @param str chaîne à répété
* @param str chaîne à répété (défaut : ' ')
* @param mode mode de répétition
* @return chaîne modifiée
*/
String.prototype._padLeft = function(n, str, mode) {
if (!n || n <= 0 || this.length >= n || str instanceof String) {
if (isNaN(parseInt(n)) || n <= 0 || this.length >= n || str === '') {
return this;
}
}
if (str === undefined) {
str = ' ';
}
if (str.length === 1) {
mode = '';
}
......@@ -120,15 +126,19 @@ String.prototype._padLeft = function(n, str, mode) {
/**
* ajout d'une chaine répétée à la fin jusqu'à la longeur désirée
* @param n taille de chaîne désirée
* @param str chaîne à répété
* @param str chaîne à répété (défaut : ' ')
* @param mode mode de répétition
* @return chaîne modifiée
*/
String.prototype._padRight = function(n, str, mode) {
if (!n || n <= 0 || this.length >= n || str instanceof String) {
if (isNaN(parseInt(n)) || n <= 0 || this.length >= n || str === '') {
return this;
}
if (str.length === 1) {
if (str === undefined) {
str = ' ';
}
if (str.length <= 1) {
mode = '';
}
switch (mode) {
......
......@@ -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.1f | (c) Zefling | license, see: github.com/Zefling/BakuJS */' "baku$name.min.js"
\ No newline at end of file
sed -i '1i/*! BakuJs v0.1g | (c) Zefling | license, see: github.com/Zefling/BakuJS */' "baku$name.min.js"
\ No newline at end of file
......@@ -208,6 +208,10 @@ window.onload = function(){
Test.equals("Number._parse('-1\u00A0234,5', {lg : 'fr'})", '-1234.5', "-1\u00A0234,5 → -1234.5");
Test.equals("Number._parse('a -1\u00A0234,5 b',{lg : 'fr'})", '-1234.5', "a -1\u00A0234,5 b → -1234.5");
Test.equals("Number._parse('a-1\u00A0234,5b', {lg : 'fr'})", '-1234.5', "a-1\u00A0234,5b → -1234.5");
Test.equals("Number._parse('')", '', "'' → ''");
Test.error("Number._parse()");
Test.error("Number._parse(null)");
Test.error("Number._parse({})");
Test.title("String.prototype._padLeft()");
......@@ -232,24 +236,33 @@ window.onload = function(){
Test.equals("'12345'._padLeft(4, 'ABCD')", '12345', "12345 + left(4, 'ABCD') → 12345");
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.equals("'12345'._padLeft(10,'ABCD')", 'DABCD12345', "12345 + left(10, 'ABCD') → DABCD12345");
Test.equals("'12345'._padLeft(10)", ' 12345', "12345 + left(10) → ' 12345'");
Test.equals("'12345'._padLeft()", '12345', "12345 + left() → '12345'");
Test.equals("'12345'._padLeft({})", '12345', "12345 + left({}) → '12345'");
Test.equals("'12345'._padLeft(1, {})", '12345', "12345 + left(1, {}) → '12345'");
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........");
Test.equals("'1'._padRight(2, 'ABCD')", '1A', "1 + left(2, 'ABCD') → 1A");
Test.equals("'1'._padRight(2, 'ABCD')", '1A', "1 + right(2, 'ABCD') → 1A");
Test.equals("'1'._padRight(10, 'ABCD')", '1ABCDABCDA', "1 + right(10, 'ABCD') → 1ABCDABCDA");
Test.equals("'1'._padRight(2, 'ABCD', 'r')", '1B', "1 + left(2, 'ABCD', 'r') → 1B");
Test.equals("'1'._padRight(2, 'ABCD', 'r')", '1B', "1 + right(2, 'ABCD', 'r') → 1B");
Test.equals("'1'._padRight(10, 'ABCD', 'r')", '1BCDABCDAB', "1 + right(10, 'ABCD', 'r') → 1BCDABCDAB");
Test.equals("'1'._padRight(2, 'ABCD', 'l')", '1D', "1 + left(2, 'ABCD', 'l') → 1D");
Test.equals("'1'._padRight(2, 'ABCD', 'l')", '1D', "1 + right(2, 'ABCD', 'l') → 1D");
Test.equals("'1'._padRight(10, 'ABCD', 'l')", '1DABCDABCD', "1 + right(10, 'ABCD', 'l') → 1DABCDABCD");
Test.equals("'12345'._padRight(4, 'ABCD')", '12345', "12345 + left(4, 'ABCD') → 12345");
Test.equals("'12345'._padRight(6, 'ABCD')", '12345A', "12345 + left(6, 'ABCD') → 12345A");
Test.equals("'12345'._padRight(10,'ABCD')", '12345ABCDA', "12345 + left(10, 'ABCD') → 12345ABCDA");
Test.equals("'12345'._padRight(6, 'ABCD', 'r')",'12345B', "12345 + left(6, 'ABCD', 'r') → 12345B");
Test.equals("'12345'._padRight(10,'ABCD', 'r')",'12345BCDAB', "12345 + left(10, 'ABCD', 'r') → 12345BCDAB");
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.equals("'12345'._padRight(4, 'ABCD')", '12345', "12345 + right(4, 'ABCD') → 12345");
Test.equals("'12345'._padRight(6, 'ABCD')", '12345A', "12345 + right(6, 'ABCD') → 12345A");
Test.equals("'12345'._padRight(10,'ABCD')", '12345ABCDA', "12345 + right(10, 'ABCD') → 12345ABCDA");
Test.equals("'12345'._padRight(6, 'ABCD', 'r')",'12345B', "12345 + right(6, 'ABCD', 'r') → 12345B");
Test.equals("'12345'._padRight(10,'ABCD', 'r')",'12345BCDAB', "12345 + right(10, 'ABCD', 'r') → 12345BCDAB");
Test.equals("'12345'._padRight(6, 'ABCD', 'l')",'12345D', "12345 + right(6, 'ABCD', 'l') → 12345D");
Test.equals("'12345'._padRight(10,'ABCD', 'l')",'12345DABCD', "12345 + right(10, 'ABCD', 'l') → 12345DABCD");
Test.equals("'12345'._padRight(10)", '12345 ', "12345 + right(10) → '12345 '");
Test.equals("'12345'._padRight()", '12345', "12345 + right() → '12345'");
Test.equals("'12345'._padRight({})", '12345', "12345 + right({}) → '12345'");
Test.equals("'12345'._padRight(1, {})", '12345', "12345 + right(1, {}) → '12345'");
Test.title("String.prototype._format()");
Test.equals("'a'._format(1)", 'a', '1 + a → a');
......@@ -277,13 +290,14 @@ window.onload = function(){
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# 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');
Test.equals("'{0, choice, 1# false|2# true}'._format([2])", 'true', '[2] + {0, choice, 1# false|2# true} → true');
Test.equals("'{0, choice, this# true|other false}'._format(['this'])",'true', '[\'get\'] + {0, choice, get# true|other false} → true');
Test.equals("'{0, choice, this# true|other false}'._format(['this'])",'true', '[\'get\'] + {0, choice, get# true|other false} → true');
Test.equals("'{val, choice, this# true|other false}'._format({val : 'this'})", 'true', '{val : \'this\'} + {val, choice, get# true|other false} → true');
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.equals("'{0, choice, 1#{1}|2#{2}}'._format('a')", '', '"a" + {0, choice, 1#{1}|2#{2}} → ""');
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');
......@@ -291,5 +305,7 @@ window.onload = function(){
Test.equals("'{0, date, w-yyyy}'._format('2015-10-20')", '43-2015', '2015-10-20 + {0, date, w-yyyy} → 43-2015');
Test.equals("'{0, date, \\\\w\\\\e\\\\e\\\\kw\\\\\\\\yyyy}'._format('2015-10-20')", 'week43\\2015', '2015-10-20 + {0, date, \\w\\e\\e\\kw\\\\yyyy} → week43\\2015');
Test.equals("'{0, date, {1}}'._format(['2015-10-20', 'dd/MM/yyyy'])", '20/10/2015', '[2015-10-20, dd/MM/yyyy] + {0, date, {1}} → 20/10/2015');
Test.equals("'{0, date, {1}}'._format(['a', 'dd/MM/yyyy'])", '', '[a, dd/MM/yyyy] + {0, date, {1}} → ""');
Test.equals("'{0, date, {1}}'._format(['2015-10-20'])", '', '[20/10/2015] + {0, date, {1}} → ""');
};
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