pile-sort
v1.0.2
Published
sort an array into different piles based on some filtering functions
Downloads
51
Readme
pile-sort
split an array into several chunks by sorting it into "piles".
Example
var pileSort = require('pile-sort')
var records = [
{ name: 'alice', age: 34 },
{ name: 'mix', age: 36 },
{ name: 'ziva', age: 2 },
{ name: 'lev', age: 15 }
]
const piles = pileSort(records, [
(record) => record.age < 13, // isChild
(record) => record.age < 20 // isTeen
])
// [
// [
// { name: 'ziva', age: 2 } // isChild
// ],
// [
// { name: 'lev', age: 13 } // isTeen
// ],
// [
// { name: 'alice', age: 34 }, // remainder
// { name: 'mix', age: 36 }
// ]
// ]
const [ children, teens, remainder ] = piles
API
pileSort(records, filters) => results
where:
records
is an Array of items you want to seperatefilters
is an Array ofn
filters which will be used to sort into piles. These are applied in order till a match is found (returnstrue
). If no match is found, the item will default to a "remainder" pileresults
is an Array ofn + 1
Arrays- n piles for each of the filters,
- 1 pile on the end "remainder" (items which didn't match any of the filters)