Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stuartpb/interleaving
JavaScript functions to combine N arrays of any size, distributing items evenly
https://github.com/stuartpb/interleaving
Last synced: 6 days ago
JSON representation
JavaScript functions to combine N arrays of any size, distributing items evenly
- Host: GitHub
- URL: https://github.com/stuartpb/interleaving
- Owner: stuartpb
- License: mit
- Created: 2016-12-08T12:02:35.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-12T00:25:59.000Z (about 7 years ago)
- Last Synced: 2024-12-01T10:49:30.006Z (about 1 month ago)
- Language: JavaScript
- Size: 3.91 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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'],
>> ['X', 'Y', 'Z']));[ 'a', 'b', '1', 'X', 'c', 'd', '2', 'e', 'Y', 'f', '3', 'g', 'h', '4', 'Z' ]
```## 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'],
>> ['X', 'Y', 'Z']));[ 'a', '1', 'b', 'X', 'c', '2', 'd', 'Y', 'e', '3', 'f', 'Z', 'g', '4', 'h' ]
```