UNPKG

1.69 kBJavaScriptView Raw
1/**
2 * Copyright 2014-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 *
10 */
11
12'use strict';
13
14var _prodInvariant = require('./reactProdInvariant');
15
16var invariant = require('fbjs/lib/invariant');
17
18/**
19 * Accumulates items that must not be null or undefined into the first one. This
20 * is used to conserve memory by avoiding array allocations, and thus sacrifices
21 * API cleanness. Since `current` can be null before being passed in and not
22 * null after this function, make sure to assign it back to `current`:
23 *
24 * `a = accumulateInto(a, b);`
25 *
26 * This API should be sparingly used. Try `accumulate` for something cleaner.
27 *
28 * @return {*|array<*>} An accumulation of items.
29 */
30
31function accumulateInto(current, next) {
32 !(next != null) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'accumulateInto(...): Accumulated items must not be null or undefined.') : _prodInvariant('30') : void 0;
33
34 if (current == null) {
35 return next;
36 }
37
38 // Both are not empty. Warning: Never call x.concat(y) when you are not
39 // certain that x is an Array (x could be a string with concat method).
40 if (Array.isArray(current)) {
41 if (Array.isArray(next)) {
42 current.push.apply(current, next);
43 return current;
44 }
45 current.push(next);
46 return current;
47 }
48
49 if (Array.isArray(next)) {
50 // A bit too dangerous to mutate `next`.
51 return [current].concat(next);
52 }
53
54 return [current, next];
55}
56
57module.exports = accumulateInto;
\No newline at end of file