Skip to content

Commit

Permalink
isBoolean() usando t/f regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed Dec 20, 2016
1 parent 5cca06e commit 97b1129
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
23 changes: 22 additions & 1 deletion lib/txt-to-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ function evaluateColumn(column, rows, regex) {
}
return true;
}
var booleanRegExps = [
{t:/^1$/, f:/^0$/},
{t:/^1$/, f:/^2$/},
{t:/^t/i, f:/^f/i},
{t:/^[^n]/i, f:/^[n]/i}
];

function isBoolean(column, rows) {
var vals=[];
for(var row=0; row<rows.length; ++row) {
Expand All @@ -42,7 +49,21 @@ function isBoolean(column, rows) {
if(vals.length>2) { return false; }
}
}
return true;
if(vals.length==2) {
return booleanRegExps.some(function(rgx) {
//console.log("vals", vals, '->', rgx)
//console.log(' ',vals[0], vals[0].match(rgx.t)?"OK":"nop")
//console.log(' ',vals[1], vals[1].match(rgx.f)?"OK":"nop")
return (vals[0].match(rgx.t) && vals[1].match(rgx.f))
|| (vals[1].match(rgx.t) && vals[0].match(rgx.f));
});
} else {
return vals.some(function(val) {
return booleanRegExps.some(function(rgx) {
return val.match(rgx.f) || val.match(rgx.t)
});
});
}
}

function isInteger(column, rows) {
Expand Down
13 changes: 10 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,23 @@ describe("datatype validation", function(){
var b1 = txtToSql.typeValidations['boolean'].checkOne;
var b = txtToSql.typeValidations['boolean'].checkArray;
expect(b1('null')).to.be.ok(); // coverage
expect(b1('')).to.not.be.ok(); // coverage
// good
expect(b(0, [['1'],['1'],['0'],['1']])).to.be.ok();
expect(b(0, [['uno']])).to.be.ok();
expect(b(0, [[null],['uno']])).to.be.ok();
expect(b(0, [['1'],['0'],[null],['0'],['1']])).to.be.ok();
expect(b(0, [[null],['1'],['0'],['0'],['1'],[null]])).to.be.ok();
expect(b(0, [[''],['uno']])).to.be.ok();
expect(b(0, [['1'],['0'],[''],['0'],['1']])).to.be.ok();
expect(b(0, [[''],['1'],['0'],['0'],['1'],['']])).to.be.ok();
expect(b(0, [['1'],['2'],['2'],['1'],['']])).to.be.ok();
expect(b(0, [['si'],['si']])).to.be.ok();
expect(b(0, [['no'],['no'],['si'],['no'],['si']])).to.be.ok();
expect(b(0, [['true'],['true'],['true']])).to.be.ok();
expect(b(0, [['true'],['true'],['false']])).to.be.ok();
// bad
expect(b(0, [['3'],['1'],['0']])).to.not.be.ok();
expect(b(0, [['3'],['1'],['3'],['1'],['3'],['1'],['0']])).to.not.be.ok();
expect(b(0, [['tito'],['loncho'],['pepe'],['tito']])).to.not.be.ok();
expect(b(0, [['juan'],['pedro'],['pedro'],['juan']])).to.not.be.ok();
});
it("integer", function(){
var i = txtToSql.typeValidations['integer'].checkOne;
Expand Down
23 changes: 22 additions & 1 deletion web/txt-to-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ function evaluateColumn(column, rows, regex) {
}
return true;
}
var booleanRegExps = [
{t:/^1$/, f:/^0$/},
{t:/^1$/, f:/^2$/},
{t:/^t/i, f:/^f/i},
{t:/^[^n]/i, f:/^[n]/i}
];

function isBoolean(column, rows) {
var vals=[];
for(var row=0; row<rows.length; ++row) {
Expand All @@ -42,7 +49,21 @@ function isBoolean(column, rows) {
if(vals.length>2) { return false; }
}
}
return true;
if(vals.length==2) {
return booleanRegExps.some(function(rgx) {
//console.log("vals", vals, '->', rgx)
//console.log(' ',vals[0], vals[0].match(rgx.t)?"OK":"nop")
//console.log(' ',vals[1], vals[1].match(rgx.f)?"OK":"nop")
return (vals[0].match(rgx.t) && vals[1].match(rgx.f))
|| (vals[1].match(rgx.t) && vals[0].match(rgx.f));
});
} else {
return vals.some(function(val) {
return booleanRegExps.some(function(rgx) {
return val.match(rgx.f) || val.match(rgx.t)
});
});
}
}

function isInteger(column, rows) {
Expand Down

0 comments on commit 97b1129

Please sign in to comment.