array.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. module.metadata = {
  6. "stability": "experimental"
  7. };
  8. /**
  9. * Returns `true` if given `array` contain given `element` or `false`
  10. * otherwise.
  11. * @param {Array} array
  12. * Target array.
  13. * @param {Object|String|Number|Boolean} element
  14. * Element being looked up.
  15. * @returns {Boolean}
  16. */
  17. var has = exports.has = function has(array, element) {
  18. // shorter and faster equivalent of `array.indexOf(element) >= 0`
  19. return !!~array.indexOf(element);
  20. };
  21. var hasAny = exports.hasAny = function hasAny(array, elements) {
  22. if (arguments.length < 2)
  23. return false;
  24. if (!Array.isArray(elements))
  25. elements = [ elements ];
  26. return array.some(function (element) {
  27. return has(elements, element);
  28. });
  29. };
  30. /**
  31. * Adds given `element` to the given `array` if it does not contain it yet.
  32. * `true` is returned if element was added otherwise `false` is returned.
  33. * @param {Array} array
  34. * Target array.
  35. * @param {Object|String|Number|Boolean} element
  36. * Element to be added.
  37. * @returns {Boolean}
  38. */
  39. var add = exports.add = function add(array, element) {
  40. var result;
  41. if ((result = !has(array, element)))
  42. array.push(element);
  43. return result;
  44. };
  45. /**
  46. * Removes first occurrence of the given `element` from the given `array`. If
  47. * `array` does not contain given `element` `false` is returned otherwise
  48. * `true` is returned.
  49. * @param {Array} array
  50. * Target array.
  51. * @param {Object|String|Number|Boolean} element
  52. * Element to be removed.
  53. * @returns {Boolean}
  54. */
  55. exports.remove = function remove(array, element) {
  56. var result;
  57. if ((result = has(array, element)))
  58. array.splice(array.indexOf(element), 1);
  59. return result;
  60. };
  61. /**
  62. * Produces a duplicate-free version of the given `array`.
  63. * @param {Array} array
  64. * Source array.
  65. * @returns {Array}
  66. */
  67. function unique(array) {
  68. return array.reduce(function(result, item) {
  69. add(result, item);
  70. return result;
  71. }, []);
  72. };
  73. exports.unique = unique;
  74. /**
  75. * Produce an array that contains the union: each distinct element from all
  76. * of the passed-in arrays.
  77. */
  78. function union() {
  79. return unique(Array.concat.apply(null, arguments));
  80. };
  81. exports.union = union;
  82. exports.flatten = function flatten(array){
  83. var flat = [];
  84. for (var i = 0, l = array.length; i < l; i++) {
  85. flat = flat.concat(Array.isArray(array[i]) ? flatten(array[i]) : array[i]);
  86. }
  87. return flat;
  88. };
  89. function fromIterator(iterator) {
  90. let array = [];
  91. if (iterator.__iterator__) {
  92. for each (let item in iterator)
  93. array.push(item);
  94. }
  95. else {
  96. for (let item of iterator)
  97. array.push(item);
  98. }
  99. return array;
  100. }
  101. exports.fromIterator = fromIterator;
  102. function find(array, predicate, fallback) {
  103. var index = 0;
  104. var count = array.length;
  105. while (index < count) {
  106. var value = array[index];
  107. if (predicate(value)) return value;
  108. else index = index + 1;
  109. }
  110. return fallback;
  111. }
  112. exports.find = find;