Map.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /**
  24. The (native) JavaScript Map object holds key-value pairs.
  25. Any value (both objects and primitive values) may be used as either a key
  26. or a value.
  27. 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/).
  28. **/
  29. @:native("Map")
  30. extern class Map<K, V> {
  31. /**
  32. The number of key/value pairs in the `js.Map` object.
  33. **/
  34. var size(default, null):Int;
  35. /**
  36. An Array or other iterable object whose elements are key-value pairs
  37. (arrays with two elements, e.g. `[[ 1, 'one' ],[ 2, 'two' ]]`).
  38. Each key-value pair is added to the new `js.Map`;
  39. null values are treated as undefined.
  40. **/
  41. @:pure function new(?iterable:Any);
  42. /**
  43. A boolean asserting whether a value has been associated to the key in
  44. the `js.Map` object or not.
  45. **/
  46. @:pure function has(key:K):Bool;
  47. /**
  48. The value associated to the key, or `null` if there is none.
  49. **/
  50. @:pure function get(key:K):Null<V>;
  51. /**
  52. Sets the value for the key in the Map object.
  53. Returns the `js.Map` object.
  54. **/
  55. function set(key:K, value:V):Map<K, V>;
  56. /**
  57. Returns `true` if an element in the `js.Map` object existed and has been
  58. removed, or `false` if the element does not exist.
  59. `has(key)` will return `false` afterwards.
  60. **/
  61. function delete(key:K):Bool;
  62. /**
  63. Removes all key/value pairs from the Map object.
  64. **/
  65. function clear():Void;
  66. /**
  67. Calls `callback` once for each key-value pair present in the `js.Map`
  68. object, in insertion order.
  69. If a `thisArg` parameter is provided to forEach, it will be used as the
  70. `this` value for each callback.
  71. **/
  72. function forEach(callback:(value:V, key:K, map:Map<K, V>) -> Void, ?thisArg:Any):Void;
  73. /**
  74. Returns a new `Iterator` object that contains the keys for each element
  75. in the `js.Map` object in insertion order.
  76. **/
  77. function keys():js.lib.Iterator<K>;
  78. /**
  79. Returns a new `Iterator` object that contains the values for each
  80. element in the `js.Map` object in insertion order.
  81. **/
  82. function values():js.lib.Iterator<V>;
  83. /**
  84. Returns a new `Iterator` object that contains an array of `KeyValue`
  85. for each element in the `js.Map` object in insertion order.
  86. **/
  87. function entries():js.lib.Iterator<KeyValue<K, V>>;
  88. inline function iterator():js.lib.HaxeIterator<V> {
  89. return new HaxeIterator(this.values());
  90. }
  91. inline function keyValueIterator():HaxeIterator<KeyValue<K, V>> {
  92. return new HaxeIterator(this.entries());
  93. }
  94. }
  95. @:deprecated typedef MapEntry<K, V> = KeyValue<K, V>;