UNPKG

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