Skip to content

Instantly share code, notes, and snippets.

@DavisDevelopment
Created January 9, 2015 05:38
Show Gist options
  • Save DavisDevelopment/3e8dd93fcec98184ec32 to your computer and use it in GitHub Desktop.
Save DavisDevelopment/3e8dd93fcec98184ec32 to your computer and use it in GitHub Desktop.
Collection of Useful Stand-Alone Haxe Abstracts
import js.JQuery;
private typedef Property = {name:String, value:Dynamic};
abstract HashWrap (Dynamic) from Dynamic {
public inline function new(o : Dynamic):Void {
this = o;
}
private var self(get, never):HashWrap;
private inline function get_self():HashWrap {
return (cast this);
}
public function keys():List<String> {
var names = Reflect.fields( this );
var keyList:List<String> = new List();
for (n in names) keyList.add( n );
return keyList;
}
public function iterator():Iterator<Property> {
var keys = self.keys();
return {
'next': function():Property {
var k:String = keys.pop();
return {
'name': k,
'value': self.get( k )
};
},
'hasNext': function():Bool {
return (!keys.isEmpty());
}
};
}
public inline function exists(key : String):Bool {
return (Reflect.getProperty(this, key) != null);
}
@:arrayAccess
public inline function get <T> (key : String):Null<T> {
return (Reflect.getProperty(this, key));
}
@:arrayAccess
public inline function set <T> (key:String, value:T):T {
return untyped (Reflect.setProperty(this, key, value));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment