From fdba274354195d1ec59da9f9b4e825e1aaebbc92 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Mon, 19 Aug 2019 22:03:09 +0200 Subject: [PATCH] Clarify ensureCategories with extra example --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 7f73e9f..b11c7eb 100644 --- a/README.md +++ b/README.md @@ -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).