Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 views
1
/**
2
* Copyright 2013-2014, 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
* @providesModule accumulate
10
*/
11
12
"use strict";
13
14
var invariant = require('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
*/
23
function accumulate(current, next) {
24
invariant(
25
next != null,
26
'accumulate(...): Accumulated items must be not be null or undefined.'
27
);
28
if (current == null) {
29
return next;
30
} else {
31
// Both are not empty. Warning: Never call x.concat(y) when you are not
32
// certain that x is an Array (x could be a string with concat method).
33
var currentIsArray = Array.isArray(current);
34
var nextIsArray = Array.isArray(next);
35
if (currentIsArray) {
36
return current.concat(next);
37
} else {
38
if (nextIsArray) {
39
return [current].concat(next);
40
} else {
41
return [current, next];
42
}
43
}
44
}
45
}
46
47
module.exports = accumulate;
48
49