index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * lodash (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modularize exports="npm" -o ./`
  4. * Copyright jQuery Foundation and other contributors <https://jquery.org/>
  5. * Released under MIT license <https://lodash.com/license>
  6. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  7. * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  8. */
  9. /**
  10. * A specialized version of `_.map` for arrays without support for iteratee
  11. * shorthands.
  12. *
  13. * @private
  14. * @param {Array} [array] The array to iterate over.
  15. * @param {Function} iteratee The function invoked per iteration.
  16. * @returns {Array} Returns the new mapped array.
  17. */
  18. function arrayMap(array, iteratee) {
  19. var index = -1,
  20. length = array ? array.length : 0,
  21. result = Array(length);
  22. while (++index < length) {
  23. result[index] = iteratee(array[index], index, array);
  24. }
  25. return result;
  26. }
  27. /**
  28. * The base implementation of `_.findIndex` and `_.findLastIndex` without
  29. * support for iteratee shorthands.
  30. *
  31. * @private
  32. * @param {Array} array The array to search.
  33. * @param {Function} predicate The function invoked per iteration.
  34. * @param {number} fromIndex The index to search from.
  35. * @param {boolean} [fromRight] Specify iterating from right to left.
  36. * @returns {number} Returns the index of the matched value, else `-1`.
  37. */
  38. function baseFindIndex(array, predicate, fromIndex, fromRight) {
  39. var length = array.length,
  40. index = fromIndex + (fromRight ? 1 : -1);
  41. while ((fromRight ? index-- : ++index < length)) {
  42. if (predicate(array[index], index, array)) {
  43. return index;
  44. }
  45. }
  46. return -1;
  47. }
  48. /**
  49. * The base implementation of `_.indexOf` without `fromIndex` bounds checks.
  50. *
  51. * @private
  52. * @param {Array} array The array to search.
  53. * @param {*} value The value to search for.
  54. * @param {number} fromIndex The index to search from.
  55. * @returns {number} Returns the index of the matched value, else `-1`.
  56. */
  57. function baseIndexOf(array, value, fromIndex) {
  58. if (value !== value) {
  59. return baseFindIndex(array, baseIsNaN, fromIndex);
  60. }
  61. var index = fromIndex - 1,
  62. length = array.length;
  63. while (++index < length) {
  64. if (array[index] === value) {
  65. return index;
  66. }
  67. }
  68. return -1;
  69. }
  70. /**
  71. * This function is like `baseIndexOf` except that it accepts a comparator.
  72. *
  73. * @private
  74. * @param {Array} array The array to search.
  75. * @param {*} value The value to search for.
  76. * @param {number} fromIndex The index to search from.
  77. * @param {Function} comparator The comparator invoked per element.
  78. * @returns {number} Returns the index of the matched value, else `-1`.
  79. */
  80. function baseIndexOfWith(array, value, fromIndex, comparator) {
  81. var index = fromIndex - 1,
  82. length = array.length;
  83. while (++index < length) {
  84. if (comparator(array[index], value)) {
  85. return index;
  86. }
  87. }
  88. return -1;
  89. }
  90. /**
  91. * The base implementation of `_.isNaN` without support for number objects.
  92. *
  93. * @private
  94. * @param {*} value The value to check.
  95. * @returns {boolean} Returns `true` if `value` is `NaN`, else `false`.
  96. */
  97. function baseIsNaN(value) {
  98. return value !== value;
  99. }
  100. /**
  101. * The base implementation of `_.unary` without support for storing metadata.
  102. *
  103. * @private
  104. * @param {Function} func The function to cap arguments for.
  105. * @returns {Function} Returns the new capped function.
  106. */
  107. function baseUnary(func) {
  108. return function(value) {
  109. return func(value);
  110. };
  111. }
  112. /** Used for built-in method references. */
  113. var arrayProto = Array.prototype;
  114. /** Built-in value references. */
  115. var splice = arrayProto.splice;
  116. /**
  117. * The base implementation of `_.pullAllBy` without support for iteratee
  118. * shorthands.
  119. *
  120. * @private
  121. * @param {Array} array The array to modify.
  122. * @param {Array} values The values to remove.
  123. * @param {Function} [iteratee] The iteratee invoked per element.
  124. * @param {Function} [comparator] The comparator invoked per element.
  125. * @returns {Array} Returns `array`.
  126. */
  127. function basePullAll(array, values, iteratee, comparator) {
  128. var indexOf = comparator ? baseIndexOfWith : baseIndexOf,
  129. index = -1,
  130. length = values.length,
  131. seen = array;
  132. if (array === values) {
  133. values = copyArray(values);
  134. }
  135. if (iteratee) {
  136. seen = arrayMap(array, baseUnary(iteratee));
  137. }
  138. while (++index < length) {
  139. var fromIndex = 0,
  140. value = values[index],
  141. computed = iteratee ? iteratee(value) : value;
  142. while ((fromIndex = indexOf(seen, computed, fromIndex, comparator)) > -1) {
  143. if (seen !== array) {
  144. splice.call(seen, fromIndex, 1);
  145. }
  146. splice.call(array, fromIndex, 1);
  147. }
  148. }
  149. return array;
  150. }
  151. /**
  152. * Copies the values of `source` to `array`.
  153. *
  154. * @private
  155. * @param {Array} source The array to copy values from.
  156. * @param {Array} [array=[]] The array to copy values to.
  157. * @returns {Array} Returns `array`.
  158. */
  159. function copyArray(source, array) {
  160. var index = -1,
  161. length = source.length;
  162. array || (array = Array(length));
  163. while (++index < length) {
  164. array[index] = source[index];
  165. }
  166. return array;
  167. }
  168. /**
  169. * This method is like `_.pull` except that it accepts an array of values to remove.
  170. *
  171. * **Note:** Unlike `_.difference`, this method mutates `array`.
  172. *
  173. * @static
  174. * @memberOf _
  175. * @since 4.0.0
  176. * @category Array
  177. * @param {Array} array The array to modify.
  178. * @param {Array} values The values to remove.
  179. * @returns {Array} Returns `array`.
  180. * @example
  181. *
  182. * var array = ['a', 'b', 'c', 'a', 'b', 'c'];
  183. *
  184. * _.pullAll(array, ['a', 'c']);
  185. * console.log(array);
  186. * // => ['b', 'b']
  187. */
  188. function pullAll(array, values) {
  189. return (array && array.length && values && values.length)
  190. ? basePullAll(array, values)
  191. : array;
  192. }
  193. module.exports = pullAll;