Extensible Array Filter

Installation

Install the package using npm:

npm install @anthonykgross/extensible-array-filter

or

yarn add @anthonykgross/extensible-array-filter

Import the library in your TypeScript project:

import '@anthonykgross/extensible-array-filter';

Declare an interface for your data:

interface Example {
id: number;
name: string;
tags: string[];
}

Create a sample data array:

const data: Example[] = [
{ id: 1, name: 'Example 1', tags: ['tag1', 'tag2'] },
{ id: 2, name: 'Example 2', tags: ['tag2', 'tag3'] },
{ id: 3, name: 'Example 3', tags: ['tag1', 'tag3'] },
];

Apply filters using the .where(...) method:

const result = data.where([
{ field: 'id', operator: '>', value: 1 },
{ field: 'name', operator: 'contains', value: 2 },
]);
// Expected output: [
// { id: 2, name: 'Example 2', tags: ['tag2', 'tag3'] },
// ]

Apply filters using the .orWhere(...) method:

const result = data.orWhere([
{ field: 'id', operator: '==', value: 1 },
{ field: 'id', operator: '==', value: 2 },
]);
// Expected output: [
// { id: 1, name: 'Example 1', tags: ['tag1', 'tag2'] },
// { id: 2, name: 'Example 2', tags: ['tag2', 'tag3'] },
// ]
Operator Description Filter values
Generic
< less Date \ Number
<= less or equal Date \ Number
== equal Date \ Number
!= not equal Date \ Number
> greater Date \ Number
>= greater or equal Date \ Number
Boolean
boolean-== equal Boolean
boolean-!= not equal Boolean
Date
date-< less Date
date-<= less or equal Date
date-== equal Date
date-!= not equal Date
date-> greater Date
date->= greater or equal Date
Number
number-< less Number
number-<= less or equal Number
number-== equal Number
number-!= not equal Number
number-> greater Number
number->= greater or equal Number
String
string-< less String
string-<= less or equal String
string-== equal String
string-!= not equal String
string-> greater String
string->= greater or equal String
contains equal String
strictly-contains equal String
Any
regex equal Any
custom - Any
const fn = (item: Example, value: any) => {
// where id === 1 or id === 2
return item.id === value || item.id === value+1
}

const result = data.where([
{ field: '?', operator: 'custom', value: 1 , test: fn },
]);
// Expected output: [
// { id: 1, name: 'Example 1', tags: ['tag1', 'tag2'] },
// { id: 2, name: 'Example 2', tags: ['tag2', 'tag3'] },
// ]