Ad2ffc6b05fb4390643f36a258b86362

I had to do this on my current project, i finally managed to do it, but it doesn't look very good i think and i'm sure it could be made much faster so i thought maybe you guys could help me! :)

It's in actionscript 3, but it's ecmascript too so...

Actionscript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function toUpperCase( str : String ):String {
	var avoid : Array = [ "ß" ]; // Characters to avoid uppercasing
	var pattern : RegExp = new RegExp( avoid.join( "|" ) , "gm" );
	var results : Array = new Array();
	var result:Array = pattern.exec(str);
	while (result != null) {
		results.push( result );		
		result = pattern.exec(str);
	}
	str = str.toUpperCase();
	var a : Array = str.split("");
	while( results.length > 0 ) {
		var r : Object = results.shift();
		a[r.index] = r;
	}
	return a.join("");
}

Refactorings

No refactoring yet !

34a6415cf2b612639b56a71a119f36fb

Tisho Georgiev

November 4, 2007, November 04, 2007 14:06, permalink

No rating. Login to rate!

I'm not sure if it's faster, but it's certainly more concise. I replaced the "avoid" array with a string (by the way, isn't this better off as a parameter to the function?), and then used a bracketed RegExp to avoid any array operations. Then I removed the first while loop, because it just seemed unnecessary.

Actionscript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function toUpperCase( str : String ):String {
	var avoid : String = "ig"; // Characters to avoid uppercasing
	var pattern : RegExp = new RegExp( "["+avoid+"]" , "gm" );
        var upperCaseString : Array = str.toUpperCase().split("");
	
	var match : Object;
	while( match = pattern.exec(str) ) {
		upperCaseString[match.index] = match.toString();
	}
	return upperCaseString.join("");
}

toUpperCase('string'); // result: STRiNg
Avatar

Emmett

November 5, 2007, November 05, 2007 07:37, permalink

1 rating. Login to rate!

you can do this as a one liner...for clarity, I've broken it up into three lines.

1
2
3
4
5
6
7
function toUpperCase( str : String ):String {
  var uppercase_chars : RegExp = /[^ß]/g;
  var uppercase : Function = function(letter) { 
    return letter.toUpperCase();
  }
  return str.replace(uppercase_chars, uppercase);
}

Your refactoring





Format Copy from initial code

or Cancel