UNPKG

1.11 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-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.
18 *
19 * This is used to conserve memory by avoiding array allocations.
20 *
21 * @return {*|array<*>} An accumulation of items.
22 */
23function accumulate(current, next) {
24 !(next != null) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'accumulate(...): Accumulated items must be not be null or undefined.') : _prodInvariant('29') : void 0;
25
26 if (current == null) {
27 return next;
28 }
29
30 // Both are not empty. Warning: Never call x.concat(y) when you are not
31 // certain that x is an Array (x could be a string with concat method).
32 if (Array.isArray(current)) {
33 return current.concat(next);
34 }
35
36 if (Array.isArray(next)) {
37 return [current].concat(next);
38 }
39
40 return [current, next];
41}
42
43module.exports = accumulate;
\No newline at end of file