/** * Copyright (c) 2013-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule insertIntoList * @format * @flow */ 'use strict'; import type { List } from 'immutable'; /** * Maintain persistence for target list when appending and prepending. */ function insertIntoList(targetList: List, toInsert: List, offset: number): List { if (offset === targetList.count()) { toInsert.forEach(c => { targetList = targetList.push(c); }); } else if (offset === 0) { toInsert.reverse().forEach(c => { targetList = targetList.unshift(c); }); } else { var head = targetList.slice(0, offset); var tail = targetList.slice(offset); targetList = head.concat(toInsert, tail).toList(); } return targetList; } module.exports = insertIntoList;