Initial commit

master
Sven Slootweg 5 years ago
commit 27ff4bc35f

@ -0,0 +1,67 @@
# 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`](https://www.npmjs.com/package/split-filter) instead.
## Example
```js
"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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring), like so:
```js
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).

@ -0,0 +1,23 @@
"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' ]
}
*/

@ -0,0 +1,25 @@
"use strict";
module.exports = function splitFilter(array, ensureCategories, predicate) {
let results = {};
if (ensureCategories != null) {
for (let category of ensureCategories) {
results[category] = [];
}
}
for (let item of array) {
let category = predicate(item);
if (category != null) {
if (results[category] == null) {
results[category] = [];
}
results[category].push(item);
}
}
return results;
};

@ -0,0 +1,10 @@
{
"name": "split-filter-n",
"description": "Like Array#filter, but lets you categorize an array into any amount of smaller arrays",
"version": "1.0.0",
"main": "index.js",
"repository": "git@git.cryto.net:joepie91/node-split-filter-n.git",
"author": "Sven Slootweg <admin@cryto.net>",
"license": "WTFPL OR CC0-1.0",
"keywords": ["array", "filter", "split", "multiple"]
}
Loading…
Cancel
Save