accumulateInto

accumulateInto(a,b)用于合并a,b两元素,构成新的数组项后返回。

 

'use strict';

var _prodInvariant = require('./reactProdInvariant');

var invariant = require('fbjs/lib/invariant');

// 合并,保留使用
// 当首参为数组,次参为数组时,次参数组拼接到首参数组后
// 当首参为数组,次参不是数组时,次参作为数组项添加到首参数组中
// 当首参不是数组,次参是数组,首参转化为数组首项,并拼接次参数组
function accumulateInto(current, next) {
  !(next != null) ? 
    process.env.NODE_ENV !== 'production' ? 
      invariant(false, 'accumulateInto(...): Accumulated items must not be null or undefined.') 
      : _prodInvariant('30') 
    : void 0;

  if (current == null) {
    return next;
  }

  // Both are not empty. Warning: Never call x.concat(y) when you are not
  // certain that x is an Array (x could be a string with concat method).
  if (Array.isArray(current)) {
    if (Array.isArray(next)) {
      current.push.apply(current, next);
      return current;
    }
    current.push(next);
    return current;
  }

  if (Array.isArray(next)) {
    return [current].concat(next);
  }

  return [current, next];
}

module.exports = accumulateInto;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值