Skip to content

Commit

Permalink
FlxStringUtil: add getEnumName()
Browse files Browse the repository at this point in the history
  • Loading branch information
Gama11 committed Sep 3, 2017
1 parent 82dee56 commit 9561538
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
46 changes: 32 additions & 14 deletions flixel/util/FlxStringUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import flixel.FlxG;
import flixel.math.FlxMath;
import flixel.system.FlxAssets;
import flixel.util.FlxDestroyUtil.IFlxDestroyable;
import flixel.util.typeLimit.OneOfTwo;
using StringTools;

#if flash
Expand Down Expand Up @@ -231,30 +232,47 @@ class FlxStringUtil
* Get the string name of any class or class instance. Wraps `Type.getClassName()`.
*
* @param objectOrClass The class or class instance in question.
* @param simple Returns only the class name, not the package or packages.
* @param simple Returns only the type name, without package(s).
* @return The name of the class as a string.
*/
public static function getClassName(objectOrClass:Dynamic, simple:Bool = false):String
{
var cl:Class<Dynamic>;
if (Std.is(objectOrClass, Class))
{
cl = cast objectOrClass;
}
else
{
cl = Type.getClass(objectOrClass);
}

var s:String = Type.getClassName(cl);
if (s != null)
{
s = s.replace("::", ".");
if (simple)
{
s = s.substr(s.lastIndexOf(".") + 1);
}
}
return formatPackage(Type.getClassName(cl), simple);
}

/**
* Get the string name of any enum or enum value. Wraps `Type.getEnumName()`.
*
* @param enumValueOrEnum The enum value or enum in question.
* @param simple Returns only the type name, without package(s).
* @return The name of the enum as a string.
* @since 4.4.0
*/
public static function getEnumName(enumValueOrEnum:OneOfTwo<EnumValue, Enum<Dynamic>>, simple:Bool = false):String
{
var e:Enum<Dynamic>;
if (Std.is(enumValueOrEnum, Enum))
e = cast enumValueOrEnum;
else
e = Type.getEnum(enumValueOrEnum);

return formatPackage(Type.getEnumName(e), simple);
}

private static function formatPackage(s:String, simple:Bool):String
{
if (s == null)
return null;

s = s.replace("::", ".");
if (simple)
s = s.substr(s.lastIndexOf(".") + 1);
return s;
}

Expand Down
27 changes: 23 additions & 4 deletions tests/unit/src/flixel/util/FlxStringUtilTest.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flixel.util;

import flash.display.BitmapData;
import flixel.system.debug.FlxDebugger.FlxDebuggerLayout;
import massive.munit.Assert;

class FlxStringUtilTest
Expand Down Expand Up @@ -195,9 +196,27 @@ class FlxStringUtilTest
function test(value:Dynamic, simple:Bool, expected:String)
Assert.areEqual(FlxStringUtil.getClassName(value, simple), expected);

test(FlxSprite, false, "flixel.FlxSprite");
test(FlxSprite, true, "FlxSprite");
test(new FlxSprite(), false, "flixel.FlxSprite");
test(new FlxSprite(), true, "FlxSprite");
var longName = "flixel.FlxSprite";
var shortName = "FlxSprite";

test(FlxSprite, false, longName);
test(FlxSprite, true, shortName);
test(new FlxSprite(), false, longName);
test(new FlxSprite(), true, shortName);
}

@Test
function testGetEnumName()
{
function test(value, simple:Bool, expected:String)
Assert.areEqual(FlxStringUtil.getEnumName(value, simple), expected);

var longName = "flixel.system.debug.FlxDebuggerLayout";
var shortName = "FlxDebuggerLayout";

test(FlxDebuggerLayout, false, longName);
test(FlxDebuggerLayout, true, shortName);
test(FlxDebuggerLayout.BIG, false, longName);
test(FlxDebuggerLayout.BIG, true, shortName);
}
}

0 comments on commit 9561538

Please sign in to comment.