Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@antoniovdlc/filter
Advanced tools
Custom filter functions for arrays.
This package is distributed via npm:
npm install @antoniovdlc/filter
Filtering arrays is a common operation in JavaScript, so this library provides some common custom compare functions to have a more declarative way of filtering arrays.
You can use this library either as an ES module or a CommonJS package:
import { hasValue, equal, match, lesserThan } from "@antoniovdlc/filter";
- or -
const { hasValue, equal, match, lesserThan } = require("@antoniovdlc/filter");
All filter functions can be used out of the box for filtering as follows:
import { lesserThan } from "@antoniovdlc/filter";
const arr = [1, 2, 2, 23, 30, 4];
arr.filter(lesserThan(5)); // [1, 2, 2, 4]
You can revert every filtering functions by appending .not
:
import { lesserThan } from "@antoniovdlc/filter";
const arr = [1, 2, 2, 23, 30, 4];
arr.filter(lesserThan(5).not); // [23, 30]
Finally, all filter functions provide a .on("key")
function which allows to sort arrays of objects by nested fields:
const arr = [
{ name: "Bob", age: 23 },
{ name: "Alice", age: 32 },
{ name: "Tom", age: 60 },
{ name: "Candice", age: 45 },
];
arr.filter(greaterThanOrEqual(40).on("age"));
/*
[
{ name: "Tom", age: 60 },
{ name: "Candice", age: 45 },
]
*/
The same .not
function can be used for filtering arrays of objects:
const arr = [
{ name: "Bob", age: 23 },
{ name: "Alice", age: 32 },
{ name: "Tom", age: 60 },
{ name: "Candice", age: 45 },
];
arr.filter(greaterThanOrEqual(40).on("age").not);
/*
[
{ name: "Bob", age: 23 },
{ name: "Alice", age: 32 },
]
*/
Here is a list of provided compare functions:
Removes null
values from an array.
Removes falsy values from an array (except 0).
Keeps all values that are strictly equal to value
.
Uses lodash.isequal
Filters on numerical or date values applying the appropriate comparaison function.
Keeps all values that match the provided pattern
.
You can create your own filtering functions by using the createFilterFunction()
function:
import { createFilterFunction } from "@antoniovdlc/filter";
const contains = createFilterFunction(
(item, value) => Array.isArray(item) && item.includes(value)
);
const arr = [
{ name: "Bob", age: 23, values: [1, 2, 5] },
{ name: "Alice", age: 32, values: [4, 3] },
{ name: "Tom", age: 60, values: [8] },
{ name: "Candice", age: 45, values: [1, 2, 4, 8] },
];
arr.filter(contains(8).on("values"));
/*
[
{ name: "Tom", age: 60, values: [8] },
{ name: "Candice", age: 45, values: [1, 2, 4, 8] },
]
*/
import { createFilterFunction } from "@antoniovdlc/filter";
const isBob = createFilterFunction((item, value) => item === value)("Bob");
const arr = ["Bob", "Alice", "Tom", "Candice"];
arr.filter(isBob); // ["Bob"]
arr.filter(isBob.not); // ["Alice", "Tom", "Candice"]
Out of the box, your custom filtering functions have the same attributes and methods as the default filtering functions (such as .not
or .on()
)!
You can also combine multiple filtering functions.
Let's say that for example, you need to filter an array of users first by name matching a pattern, and then by age lower than 40 and higher than 30, or age equal to 45. You can achieve that as follows:
import { combine } from "@antoniovdlc/filter";
const arr = [
{ name: "Bob", age: 23 },
{ name: "Alice", age: 32 },
{ name: "Tom", age: 60 },
{ name: "Candice", age: 45 },
{ name: "Alice", age: 28 },
];
arr.filter(
combine({
operator: "and",
filters: [
match(/ice$/).on("name"),
{
operator: "or",
filters: [
{
operator: "and",
filters: [lesserThan(40).on("age"), greaterThan(30).on("age")],
},
equal(45).on("age"),
],
},
],
})
);
/*
[
{ name: "Alice", age: 32 },
{ name: "Candice", age: 45 },
]
*/
MIT
FAQs
Custom filter functions for arrays
We found that @antoniovdlc/filter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.