Map.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package js.lib;
  23. import js.lib.Iterator;
  24. /**
  25. The (native) JavaScript Map object holds key-value pairs.
  26. Any value (both objects and primitive values) may be used as either a key
  27. or a value.
  28. Documentation [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) by [Mozilla Contributors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map$history), licensed under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/).
  29. **/
  30. @:native("Map")
  31. extern class Map<K, V> {
  32. /**
  33. The number of key/value pairs in the `js.Map` object.
  34. **/
  35. var size(default, null):Int;
  36. /**
  37. An Array or other iterable object whose elements are key-value pairs
  38. (arrays with two elements, e.g. `[[ 1, 'one' ],[ 2, 'two' ]]`).
  39. Each key-value pair is added to the new `js.Map`;
  40. null values are treated as undefined.
  41. **/
  42. @:pure function new(?iterable:Any);
  43. /**
  44. A boolean asserting whether a value has been associated to the key in
  45. the `js.Map` object or not.
  46. **/
  47. @:pure function has(key:K):Bool;
  48. /**
  49. The value associated to the key, or `null` if there is none.
  50. **/
  51. @:pure function get(key:K):Null<V>;
  52. /**
  53. Sets the value for the key in the Map object.
  54. Returns the `js.Map` object.
  55. **/
  56. function set(key:K, value:V):Map<K, V>;
  57. /**
  58. Returns `true` if an element in the `js.Map` object existed and has been
  59. removed, or `false` if the element does not exist.
  60. `has(key)` will return `false` afterwards.
  61. **/
  62. function delete(key:K):Bool;
  63. /**
  64. Removes all key/value pairs from the Map object.
  65. **/
  66. function clear():Void;
  67. /**
  68. Calls `callback` once for each key-value pair present in the `js.Map`
  69. object, in insertion order.
  70. If a `thisArg` parameter is provided to forEach, it will be used as the
  71. `this` value for each callback.
  72. **/
  73. function forEach(callback:(value:V, key:K, map:Map<K, V>) -> Void, ?thisArg:Any):Void;
  74. /**
  75. Returns a new `Iterator` object that contains the keys for each element
  76. in the `js.Map` object in insertion order.
  77. **/
  78. function keys():Iterator<K>;
  79. /**
  80. Returns a new `Iterator` object that contains the values for each
  81. element in the `js.Map` object in insertion order.
  82. **/
  83. function values():Iterator<V>;
  84. /**
  85. Returns a new `Iterator` object that contains an array of `MapEntry`
  86. for each element in the `js.Map` object in insertion order.
  87. **/
  88. function entries():Iterator<MapEntry<K, V>>;
  89. }
  90. /**
  91. Key/value access helper for `js.Map.entries()` and `js.Set.entries()`.
  92. **/
  93. abstract MapEntry<K, V>(Array<Any>) {
  94. public var key(get, never):K;
  95. public var value(get, never):V;
  96. inline function get_key():K
  97. return this[0];
  98. inline function get_value():V
  99. return this[1];
  100. }