File

projects/wms-framework/src/lib/baseframework/ArrayHelper.ts

Description

Array Helpers class

Index

Methods

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 :
Name Type Optional Description
sourceArray any No

Array that contains the data to copy

sourceIndex number No

The index of the source Array where we want to start copying

destinationArray any No

The Array that will receive the new data

destinationIndex number No

The index to start copying the data in the destination array

length number No

The number of elements to copy from the source array

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
    );
  }
}

result-matching ""

    No results matching ""