1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. import isArray from 'shared/isArray';
    
  11. 
    
  12. /**
    
  13.  * Accumulates items that must not be null or undefined into the first one. This
    
  14.  * is used to conserve memory by avoiding array allocations, and thus sacrifices
    
  15.  * API cleanness. Since `current` can be null before being passed in and not
    
  16.  * null after this function, make sure to assign it back to `current`:
    
  17.  *
    
  18.  * `a = accumulateInto(a, b);`
    
  19.  *
    
  20.  * This API should be sparingly used. Try `accumulate` for something cleaner.
    
  21.  *
    
  22.  * @return {*|array<*>} An accumulation of items.
    
  23.  */
    
  24. 
    
  25. function accumulateInto<T>(
    
  26.   current: ?(Array<T> | T),
    
  27.   next: T | Array<T>,
    
  28. ): T | Array<T> {
    
  29.   if (next == null) {
    
  30.     throw new Error(
    
  31.       'accumulateInto(...): Accumulated items must not be null or undefined.',
    
  32.     );
    
  33.   }
    
  34. 
    
  35.   if (current == null) {
    
  36.     return next;
    
  37.   }
    
  38. 
    
  39.   // Both are not empty. Warning: Never call x.concat(y) when you are not
    
  40.   // certain that x is an Array (x could be a string with concat method).
    
  41.   if (isArray(current)) {
    
  42.     if (isArray(next)) {
    
  43.       // $FlowFixMe[prop-missing] `isArray` does not ensure array is mutable
    
  44.       // $FlowFixMe[method-unbinding]
    
  45.       current.push.apply(current, next);
    
  46.       return current;
    
  47.     }
    
  48.     // $FlowFixMe[prop-missing] `isArray` does not ensure array is mutable
    
  49.     current.push(next);
    
  50.     return current;
    
  51.   }
    
  52. 
    
  53.   if (isArray(next)) {
    
  54.     // A bit too dangerous to mutate `next`.
    
  55.     /* $FlowFixMe[incompatible-return] unsound if `next` is `T` and `T` an array,
    
  56.      * `isArray` might refine to the array element type of `T` */
    
  57.     return [current].concat(next);
    
  58.   }
    
  59. 
    
  60.   return [current, next];
    
  61. }
    
  62. 
    
  63. export default accumulateInto;