react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / utils / accumulate.js
81152 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule accumulate9*/1011"use strict";1213var invariant = require('invariant');1415/**16* Accumulates items that must not be null or undefined.17*18* This is used to conserve memory by avoiding array allocations.19*20* @return {*|array<*>} An accumulation of items.21*/22function accumulate(current, next) {23invariant(24next != null,25'accumulate(...): Accumulated items must be not be null or undefined.'26);27if (current == null) {28return next;29} else {30// Both are not empty. Warning: Never call x.concat(y) when you are not31// certain that x is an Array (x could be a string with concat method).32var currentIsArray = Array.isArray(current);33var nextIsArray = Array.isArray(next);34if (currentIsArray) {35return current.concat(next);36} else {37if (nextIsArray) {38return [current].concat(next);39} else {40return [current, next];41}42}43}44}4546module.exports = accumulate;474849