react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / utils / accumulateInto.js
81152 views/**1* Copyright 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 accumulateInto9*/1011"use strict";1213var invariant = require('invariant');1415/**16*17* Accumulates items that must not be null or undefined into the first one. This18* is used to conserve memory by avoiding array allocations, and thus sacrifices19* API cleanness. Since `current` can be null before being passed in and not20* 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*/2829function accumulateInto(current, next) {30invariant(31next != null,32'accumulateInto(...): Accumulated items must not be null or undefined.'33);34if (current == null) {35return next;36}3738// Both are not empty. Warning: Never call x.concat(y) when you are not39// certain that x is an Array (x could be a string with concat method).40var currentIsArray = Array.isArray(current);41var nextIsArray = Array.isArray(next);4243if (currentIsArray && nextIsArray) {44current.push.apply(current, next);45return current;46}4748if (currentIsArray) {49current.push(next);50return current;51}5253if (nextIsArray) {54// A bit too dangerous to mutate `next`.55return [current].concat(next);56}5758return [current, next];59}6061module.exports = accumulateInto;626364