Clarify ensureCategories with extra example

master
Sven Slootweg 5 years ago
parent 27ff4bc35f
commit fdba274354

@ -64,4 +64,16 @@ Since it's not possible in JS for a function to detect what variables it will be
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.
Example:
```js
let { low, medium, high } = splitFilterN(values, [ "low", "medium", "high" ], predicate);
for (let mediumValue of medium) {
// some more logic...
}
```
Now the `for` statement will always work.
__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).

Loading…
Cancel
Save