UNPKG

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