1.0.0 • Published 7 years ago

interleaving v1.0.0

Weekly downloads
263
License
MIT
Repository
github
Last release
7 years ago

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' ]