Proposal: GitHub - hotequil/proposal-filter-groups: TC39 proposal to implement the Array.prototype.filterGroups.
Hi, guys!
I've created this proposal to implement Array.prototype.filterGroups
. I believe this is a simple and useful method that would be great to have in JavaScript. While working on a feature, I needed to apply multiple filters or install a library just to filter arrays in a grouped way. So I decided to contribute to TC39 by proposing this new method.
I've set up a GitHub repository and published the polyfill on npm. I’d like to find a champion for this proposal. Feel free to engage with it, suggest new features, and read the full README.md.
How it works
- Parameters: One or more functions (callbacks) that return a boolean (similar to filter);
- Return: A parent array containing filtered arrays.
Success cases
// Imports omitted…
export const users: User[] = [
{ name: "Oliver", status: { name: "active" } },
{ name: "Henry", status: { name: "inactive" } },
{ name: "Thomas", status: { name: "active" } }
];
const [activeUsers, inactiveUsers] = users.filterGroups(
user => user.status.name === "active"
);
// [
// [
// { name: "Oliver", status: { name: "active" } },
// { name: "Thomas", status: { name: "active" } }
// ],
// [
// { name: "Henry", status: { name: "inactive" } }
// ]
// ]
export const vehicles: Vehicle[] = [
{ name: "Toyota Corolla", type: "sedan" },
{ name: "Honda Fit", type: "hatch" },
{ name: "Honda Civic", type: "sedan" },
{ name: "Honda CRV", type: "suv" },
{ name: "Toyota Etios", type: "hatch" },
{ name: "Honda Odyssey", type: "van" },
{ name: "Toyota Dyna", type: "truck" },
{ name: "Toyota SW4", type: "suv" }
];
const [sedanVehicles, hatchVehicles, suvVehicles, otherVehicles] =
vehicles.filterGroups(
// You can use the index and array parameters too, it helps to mix many conditions
({ type }, _index, _array) => type === "sedan",
vehicle => vehicle.type === "hatch",
vehicle => vehicle.type === "suv"
);
// [
// [
// { name: "Toyota Corolla", type: "sedan" },
// { name: "Honda Civic", type: "sedan" }
// ],
// [
// { name: "Honda Fit", type: "hatch" },
// { name: "Toyota Etios", type: "hatch" }
// ],
// [
// { name: "Honda CRV", type: "suv" },
// { name: "Toyota SW4", type: "suv" }
// ],
// [
// { name: "Honda Odyssey", type: "van" },
// { name: "Toyota Dyna", type: "truck" }
// ]
// ]
// The first callbacks have preference
const [vehiclesWithName, vehiclesWithType] =
vehicles.filterGroups(
({ name }) => name.length > 0,
({ type }) => type.length > 0
);
// [
// [
// { name: "Toyota Corolla", type: "sedan" },
// { name: "Honda Fit", type: "hatch" },
// { name: "Honda Civic", type: "sedan" },
// { name: "Honda CRV", type: "suv" },
// { name: "Toyota Etios", type: "hatch" },
// { name: "Honda Odyssey", type: "van" },
// { name: "Toyota Dyna", type: "truck" },
// { name: "Toyota SW4", type: "suv" }
// ],
// []
// ]
Error cases
// Imports omitted…
// It'll throw a TypeError, it isn't allowed use without callbacks
vehicles.filterGroups()
// It'll throw a TypeError, it isn't allowed use numbers in callbacks
vehicles.filterGroups(
() => true,
() => true,
1,
() => true
)
// It'll throw a TypeError, it isn't allowed use strings in callbacks
vehicles.filterGroups(
() => true,
() => true,
"type",
() => true
)
// It'll throw a TypeError, it isn't allowed use booleans in callbacks
vehicles.filterGroups(
() => true,
() => true,
true,
() => true
)
// It'll throw a TypeError, it isn't allowed use objects in callbacks
vehicles.filterGroups(
() => true,
() => true,
{},
() => true
)
Performance
const numbers = Array.from(
{ length: 1_000_000 },
(_, index) => index - 500_000
);
// 25.043ms
const [_negatives, _evens, _odds] = numbers.filterGroups(
number => number < 0,
number => number % 2 === 0,
number => number % 2 !== 0
);
// 29.505ms
const _negatives2 = numbers.filter(number => number < 0);
const _evens2 = numbers.filter(number => number >= 0 && number % 2 === 0);
const _odds2 = numbers.filter(number => number >= 0 && number % 2 !== 0);
Install the polyfill
// Use your favorite package manager
npm install @hotequil/proposal-filter-groups
Import the polyfill
// Import it in your index
import "@hotequil/proposal-filter-groups"