Prechádzať zdrojové kódy

[js] add iterator structure and add missing methods to Set/Map

Dan Korostelev 7 rokov pred
rodič
commit
c046acb743
3 zmenil súbory, kde vykonal 29 pridanie a 2 odobranie
  1. 12 0
      std/js/JsIterator.hx
  2. 11 1
      std/js/Map.hx
  3. 6 1
      std/js/Set.hx

+ 12 - 0
std/js/JsIterator.hx

@@ -0,0 +1,12 @@
+package js;
+
+// prefixed with Js to avoid name clashes with standard Iterator structure
+
+typedef JsIterator<T> = {
+	function next():JsIteratorStep<T>;
+}
+
+typedef JsIteratorStep<T> = {
+	done:Bool,
+	?value:T
+}

+ 11 - 1
std/js/Map.hx

@@ -24,11 +24,21 @@ package js;
 @:native("Map")
 extern class Map<K,V> {
 	var size(default,null):Int;
-	@:pure function new();
+	@:pure function new(?iterable:Any);
 	@:pure function has(key:K):Bool;
 	@:pure function get(key:K):Null<V>;
 	function set(key:K, value:V):Map<K,V>;
 	function delete(key:K):Bool;
 	function clear():Void;
 	function forEach(callback:(value:V, key:K, map:Map<K,V>)->Void, ?thisArg:Any):Void;
+	function keys():JsIterator<K>;
+	function values():JsIterator<V>;
+	function entries():JsIterator<MapEntry<K,V>>;
+}
+
+abstract MapEntry<K,V>(Array<Any>) {
+	public var key(get,never):K;
+	public var value(get,never):V;
+	inline function get_key():K return this[0];
+	inline function get_value():V return this[1];
 }

+ 6 - 1
std/js/Set.hx

@@ -21,13 +21,18 @@
  */
 package js;
 
+import js.Map.MapEntry;
+
 @:native("Set")
 extern class Set<T> {
 	var size(default,null):Int;
-	@:pure function new();
+	@:pure function new(?iterable:Any);
 	@:pure function has(value:T):Bool;
 	function add(value:T):Set<T>;
 	function delete(value:T):Bool;
 	function clear():Void;
 	function forEach(callback:(value:T, key:T, set:Set<T>)->Void, ?thisArg:Any):Void;
+	function keys():JsIterator<T>;
+	function values():JsIterator<T>;
+	function entries():JsIterator<MapEntry<T,T>>;
 }