projects/wms-framework/src/lib/baseframework/ArrayHelper.ts
Array Helpers class
Methods |
|
Static copy | ||||||||||||||||||||||||
copy(sourceArray: any, sourceIndex: number, destinationArray: any, destinationIndex: number, length: number)
|
||||||||||||||||||||||||
Copies a range of elements from an Array starting at the specified index and pastes them to another Array starting at the specified index in the destination array.
Parameters :
Returns :
void
|
export class ArrayHelper {
/**
* Copies a range of elements from an Array starting at the specified index
* and pastes them to another Array starting at the specified index in the destination array.
*
* @static
* @memberof ArrayHelper
* @param sourceArray Array that contains the data to copy
* @param sourceIndex The index of the source Array where we want to start copying
* @param destinationArray The Array that will receive the new data
* @param destinationIndex The index to start copying the data in the destination array
* @param length The number of elements to copy from the source array
* @wMethod Copy
* @wIgnore
*/
public static copy(
sourceArray: any,
sourceIndex: number,
destinationArray: any,
destinationIndex: number,
length: number
) {
if (!sourceArray) {
throw new Error('sourceArray is null or undefined');
}
if (!destinationArray) {
throw new Error('destinationArray is null or undefined');
}
let enabledSpace = destinationArray.length - destinationIndex;
if (length > enabledSpace) {
throw new Error('Destination array was not long enough');
}
if (sourceArray.length < sourceIndex + length) {
throw new Error('Source array was not long enough');
}
let tempArrayElementsToCopy = sourceArray.slice(
sourceIndex,
sourceIndex + length
);
destinationArray.splice(
destinationIndex,
length,
...tempArrayElementsToCopy
);
}
}