interleaving

1.0.0 • Public • Published

interleaving

JavaScript functions to combine N arrays of any size, distributing items evenly

The interleaving module exposes two functions:

interleaving.justify(array1, array2, ...)

Distributes items based on their distance from the final item of the source array. Items from the longest arrays will both appear and finish first, while items from the shortest arrays will both appear and finish last.

Points of equal distance will be placed by the order the arrays are specified, so it is recommended to sort longest arrays first if you wish for this principle to remain consistent.

> console.log(interleaving.justify(
>>   ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
>>   ['1', '2', '3', '4'],
>>   ['A', 'B', 'C']));

[ 'a', 'b', '1', 'A', 'c', 'd', '2', 'e', 'B', 'f', '3', 'g', 'h', '4', 'C' ]

interleaving.center(array1, array2, ...)

Distributes items based on their distance along the length of the source array. Items from the longest arrays will appear first and finish last, while items from the shortest arrays will appear last and finish first.

Points of equal distance will be placed by the order the arrays are specified.

> console.log(interleaving.center(
>>   ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
>>   ['1', '2', '3', '4'],
>>   ['A', 'B', 'C']));

[ 'a', '1', 'b', 'A', 'c', '2', 'd', 'B', 'e', '3', 'f', 'C', 'g', '4', 'h' ]

Package Sidebar

Install

npm i interleaving

Weekly Downloads

2

Version

1.0.0

License

MIT

Last publish

Collaborators

  • stuartpb