Modules: app/util/gapArrayUtils

Utilities to manage "gap arrays". These are arrays of which contain objects and numbers. The objects represent actual data, while the numbers define an amount of 'undefined' items. This allows us to define lists with large gaps in between without having an array with a large length.

For example, the following array: [undefined, undefined, undefined, foo, undefined, undefined, bar] would be stored as a "gap array" like so: [3, foo, 2, bar]

Source:

Methods


<static> gapArraySet(gapArray, data, offset)

Returns a new gap array with the given array of data inserted. The data will replace existing data at the same index.

Parameters:
Name Type Description
gapArray Array.<(Object|number)>

The array to insert into

data Array.<Object>

An array of data objects to insert

offset number

The index at which to start inserting

Source:
Returns:

The resulting array

Type
Array.<(Object|number)>

<static> gapArraySlice(gapArray, begin, end)

Returns a slice of an array with gaps as a regular array.

Parameters:
Name Type Description
gapArray Array.<(Object|number)>

The input array that can contain objects or a number indicating an amount of undefined items.

begin number

The index at which to start the slice

end number

The index at which to end the slice. The item at this index is the first item not included in the returned slice.

Source:
Returns:

A result object containing the following properties:

  • result the resulting slice array
  • beginGapArrayIndex The index of the given 'begin' index in the input array.
  • beginGapOffset If beginGapArrayIndex is a gap, this is set to the index of 'begin' within that gap. Set to null otherwise
  • endGapArrayIndex The index of the given 'end' index in the in the input array. If the 'end' index was beyond the length of the array, this value will be null.
  • endGapOffset If endGapArrayIndex is a gap, this is set to the index of 'end' within that gap. Set to null otherwise
Type
object

<static> toGapArray(target)

Converts all the undefined items in the given array to a number representing the amount of undefined items.

Parameters:
Name Type Description
target Array.<Object>

The input array

Source:
Returns:

The processed array

Type
Array.<(Object|number)>
Example
const input = [foo, undefined, undefined, bar, undefined, foobar];
const gapArray = toGapArray(input); // returns [foo, 2, bar, 1, foobar]