array.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * DevExtreme (core/utils/array.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var isDefined = require("./type").isDefined;
  11. var each = require("./iterator").each;
  12. var objectUtils = require("./object");
  13. var config = require("../config");
  14. var isEmpty = function(entity) {
  15. return Array.isArray(entity) && !entity.length
  16. };
  17. var wrapToArray = function(entity) {
  18. return Array.isArray(entity) ? entity : [entity]
  19. };
  20. var intersection = function(a, b) {
  21. if (!Array.isArray(a) || 0 === a.length || !Array.isArray(b) || 0 === b.length) {
  22. return []
  23. }
  24. var result = [];
  25. each(a, function(_, value) {
  26. var index = inArray(value, b);
  27. if (index !== -1) {
  28. result.push(value)
  29. }
  30. });
  31. return result
  32. };
  33. var removeDuplicates = function(from, what) {
  34. if (!Array.isArray(from) || 0 === from.length) {
  35. return []
  36. }
  37. if (!Array.isArray(what) || 0 === what.length) {
  38. return from.slice()
  39. }
  40. var result = [];
  41. each(from, function(_, value) {
  42. var index = inArray(value, what);
  43. if (index === -1) {
  44. result.push(value)
  45. }
  46. });
  47. return result
  48. };
  49. var normalizeIndexes = function(items, indexParameterName, currentItem, needIndexCallback) {
  50. var indexedItems = {};
  51. var parameterIndex = 0;
  52. var useLegacyVisibleIndex = config().useLegacyVisibleIndex;
  53. each(items, function(index, item) {
  54. index = item[indexParameterName];
  55. if (index >= 0) {
  56. indexedItems[index] = indexedItems[index] || [];
  57. if (item === currentItem) {
  58. indexedItems[index].unshift(item)
  59. } else {
  60. indexedItems[index].push(item)
  61. }
  62. } else {
  63. item[indexParameterName] = void 0
  64. }
  65. });
  66. if (!useLegacyVisibleIndex) {
  67. each(items, function() {
  68. if (!isDefined(this[indexParameterName]) && (!needIndexCallback || needIndexCallback(this))) {
  69. while (indexedItems[parameterIndex]) {
  70. parameterIndex++
  71. }
  72. indexedItems[parameterIndex] = [this];
  73. parameterIndex++
  74. }
  75. })
  76. }
  77. parameterIndex = 0;
  78. objectUtils.orderEach(indexedItems, function(index, items) {
  79. each(items, function() {
  80. if (index >= 0) {
  81. this[indexParameterName] = parameterIndex++
  82. }
  83. })
  84. });
  85. if (useLegacyVisibleIndex) {
  86. each(items, function() {
  87. if (!isDefined(this[indexParameterName]) && (!needIndexCallback || needIndexCallback(this))) {
  88. this[indexParameterName] = parameterIndex++
  89. }
  90. })
  91. }
  92. return parameterIndex
  93. };
  94. var inArray = function(value, object) {
  95. if (!object) {
  96. return -1
  97. }
  98. var array = Array.isArray(object) ? object : object.toArray();
  99. return array.indexOf(value)
  100. };
  101. var merge = function(array1, array2) {
  102. for (var i = 0; i < array2.length; i++) {
  103. array1[array1.length] = array2[i]
  104. }
  105. return array1
  106. };
  107. var find = function(array, condition) {
  108. for (var i = 0; i < array.length; i++) {
  109. if (condition(array[i])) {
  110. return array[i]
  111. }
  112. }
  113. };
  114. exports.isEmpty = isEmpty;
  115. exports.wrapToArray = wrapToArray;
  116. exports.intersection = intersection;
  117. exports.removeDuplicates = removeDuplicates;
  118. exports.normalizeIndexes = normalizeIndexes;
  119. exports.inArray = inArray;
  120. exports.merge = merge;
  121. exports.find = find;