Skip to content

Commit

Permalink
Corrijo el orden de los números negativos y de los chiquitos.
Browse files Browse the repository at this point in the history
  • Loading branch information
emilioplatzer committed Apr 7, 2017
1 parent d1815c7 commit ed561be
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
24 changes: 19 additions & 5 deletions best-globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ var letterTranslatorRegexp = new RegExp(
']','g'
);

bestGlobals.auxComplementInteger = function auxComplementInteger(integerText){
return integerText.split('').map(function(digitText){ return digitText==='.'?'.':''+(9-digitText); }).join('');
}

bestGlobals.forOrder = function forOrder(text){
if(text==null){
return 'zzz(null)';
Expand All @@ -359,19 +363,29 @@ bestGlobals.forOrder = function forOrder(text){
var coalesce = bestGlobals.coalesce;
var main=[];
var signs=[];
var canBeNegative=true;
var normal=text.toString()
.replace(letterTranslatorRegexp, function(letter){ return letterTranslator[letter]; })
.replace(
/([a-z]+)|(0*([1-9][0-9]*)(\.[0-9]+)?)|([^a-z0-9])/ig,
function(t, letters, nums, integer, decimals, sign){
/([a-z]+)|((-?)0*(0|[1-9][0-9]*)(\.[0-9]+)?)|([^a-z0-9])/ig,
function(t, letters, nums, sign, integer, decimals, others){
if(letters){
main.push(' '+letters.toLowerCase());
}
if(nums){
main.push(' '+String.fromCharCode(64+coalesce(integer,'').length)+coalesce(integer,'')+coalesce(decimals,''));
if(!integer){
integer='0';
}
if(sign && canBeNegative){
integer = bestGlobals.auxComplementInteger(integer||'');
decimals = bestGlobals.auxComplementInteger(decimals||'');
}
main.push(' '+(sign?'A':'')+String.fromCharCode(65+coalesce(integer,'').length)+coalesce(integer,'')+coalesce(decimals,''));
}
if(sign){
signs.push(' , '+sign);
canBeNegative=false;
if(others){
canBeNegative=true;
signs.push(' , '+others);
}
}
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "best-globals",
"description": "common global function and constants - i.e. coalesce",
"version": "0.8.0",
"version": "0.8.1",
"author": "Codenautas <codenautas@googlegroups.com>",
"license": "MIT",
"repository": "codenautas/best-globals",
Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,11 @@ describe('escapeRegExp', function(){
});

describe('ordering', function(){
it("calculates integer complement", function(){
expect(bestGlobals.auxComplementInteger('12')).to.eql('87');
expect(bestGlobals.auxComplementInteger('1002')).to.eql('8997');
expect(bestGlobals.auxComplementInteger('0')).to.eql('9');
});
[
{a:'ABC' , b:'abd' , label:'ignore case '},
{a:'abc' , b:'ABD' , label:'ignore case '},
Expand All @@ -551,6 +556,14 @@ describe('ordering', function(){
{a:' aBc' , b:'abC ' , label:'trailing left signs last '},
{a:'7' , b:'a' , label:'numbers first '},
{a:7 , b:'11' , label:'numbers '},
{a:'-7' , b:'6' , label:'text of negative numbers '},
{a:-7 , b:6 , label:'negative numbers '},
{a:-7 , b:1 , label:'negative numbers 2 '},
{a:'0.2134' , b:1 , label:'small decimals '},
{a:'0.2134' , b:'0.4134' , label:'small decimals 2 '},
{a:'-0.4134' , b:'0.21349' , label:'small decimals neg '},
{a:'a-1' , b:'a-2' , label:'code with number '},
{a:'999-123-456', b:'999-321-456',label:'telephones '},
{a:'other' , b:null , label:'nulls last '},
{a:new Date(2012,9,15,8), b:new Date() , label:'dates '},
].forEach(function(fixture) {
Expand Down

0 comments on commit ed561be

Please sign in to comment.