38 lines
1.0 KiB
Plaintext
38 lines
1.0 KiB
Plaintext
/**
|
|
* 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<T>(targetList: List<T>, toInsert: List<T>, offset: number): List<T> {
|
|
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; |