Library that lets you split an array into any amount of smaller arrays, based on a predicate
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
Sven Slootweg 27ff4bc35f Initial commit 5 years ago
README.md Initial commit 5 years ago
example.js Initial commit 5 years ago
index.js Initial commit 5 years ago
package.json Initial commit 5 years ago

README.md

split-filter-n

Like Array#filter, but lets you categorize an array of items into many arrays, based on a predicate function.

If you just want to split an array into two parts (eg. based on a true/false result), you'll probably want to use split-filter instead.

Example

"use strict";

const splitFilterN = require("./");

let words = [
	"apple",
	"Banana",
	"strawberry",
	"Avocado"
];

let letterIndex = splitFilterN(words, ["A", "B", "C"], (word) => word[0].toUpperCase());

console.log(letterIndex);

/*
{
	A: [ 'apple', 'Avocado' ]
	B: [ 'Banana' ],
	C: [],
	S: [ 'strawberry' ]
}
*/

API

splitFilterN(array, ensureCategories, predicate)

Splits the array into an object of arrays, keyed by whatever the predicate function returned for each value.

  • array: The array to split.
  • ensureCategories: An array of strings, indicating which categories should always exist on the result object (explained further below). Set this to an empty array ([]) if you don't need this feature.
  • predicate: The function that determines which category to sort the value into. This function should return a string, indicating the desired category for the value.

Returns an object - one key for each category, with the corresponding value being an array of items that were sorted into that category.

Note that the keys on the object come from two sources: - Each unique string returned from the predicate function will become a category key. - Each string in the ensureCategories array will also become a category key.

The reason for the ensureCategories option is that you'll often want to use object destructuring, like so:

let { low, medium, high } = splitFilterN(values, [], predicate);

for (let mediumValue of medium) {
	// some more logic...
}

Now, what if there were no values in values that returned "medium" from the predicate function? The medium variable would be set to undefined, and the for statement would produce an error, as you can't iterate over undefined. This is of course very impractical if you can't predict what values you're going to encounter.

Since it's not possible in JS for a function to detect what variables it will be destructured into, you'll have to explicitly tell the splitFilterN function what categories you want to always exist - and you do that through the ensureCategories argument. Whatever categories are listed in that argument, will always be an array in the result object, even if no items matched it.

Using the ensureCategories argument still lets you create undeclared categories through the predicate return value, of course - it just ensures that a particular set of categories is always there no matter what, so that you can safely destructure the result.

If it's an optional feature, why does it come before the required predicate? Because a predicate function will very often need to be more than one line, and having it at the end makes it easier to format your code well, regardless of what code style you use. Setting it to [] if you don't need it is pretty unobtrusive, and means that this library doesn't need to support overloaded syntax (which would make it unnecessarily fragile).