Compare commits

...

5 Commits

@ -9,7 +9,7 @@ If you want to split an array into *more than two* arrays, you'll want to use [`
```js
"use strict";
const splitFilter = require("./");
const splitFilter = require("split-filter");
function isEven(number) {
return Math.abs(number) % 2 === 0;
@ -30,7 +30,9 @@ console.log(odd); // [ 1, 3, 5, 7 ]
Splits the `array` into a truthy and a falsy side, based on what the `predicate` function returns. Note that like `Array#map`, whether a value 'matches' depends on whether the returned value is [__truthy__](https://developer.mozilla.org/en-US/docs/Glossary/Truthy), not just a literal `true`.
- __array:__ The array to split.
- __predicate:__ The function that determines which side to sort the value into.
- __predicate:__ The function that determines which side to sort the value into. This predicate function receives two arguments, of which you'll usually only need the first one:
- __value:__ The value itself.
- __index:__ The index of the value in the array you've provided.
Returns an array with two elements:
@ -38,3 +40,17 @@ Returns an array with two elements:
- __1:__ The elements that __did not match__ the predicate function (ie. returned a falsy value).
It's strongly recommended to use [array destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Array_destructuring) in handling the returned array (like in the example), as this will greatly improve readability of your code.
## Changelog
# 1.1.1 (August 26, 2019)
- Fixed incorrect `require` in documentation example.
# 1.1.0 (August 19, 2019)
- __New:__ Predicate function now also receives the index of the value as an argument, not just the value itself.
# 1.0.0 (August 19, 2019)
Initial release.

@ -4,8 +4,8 @@ module.exports = function splitFilter(array, predicate) {
let truthy = [];
let falsy = [];
for (let item of array) {
if (predicate(item)) {
for (let [ i, item ] of array.entries()) {
if (predicate(item, i)) {
truthy.push(item);
} else {
falsy.push(item);

@ -1,10 +1,14 @@
{
"name": "split-filter",
"description": "Like Array#filter, but you get to keep both pieces",
"version": "1.0.0",
"version": "1.1.1",
"main": "index.js",
"repository": "git@git.cryto.net:joepie91/node-split-filter.git",
"author": "Sven Slootweg <admin@cryto.net>",
"license": "WTFPL OR CC0-1.0",
"keywords": ["array", "filter", "split"]
"keywords": [
"array",
"filter",
"split"
]
}

Loading…
Cancel
Save