npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

js-array-filter

v0.1.6

Published

Apply filter to an array

Readme

js-array-filter

js-array-filter is a TypeScript library for filtering arrays based on conditions. It provides functionalities to define filters and apply them to arrays of data.

Features

  • Define filters with multiple conditions and connectors
  • Apply filters to arrays of data
  • Support for various data types and operators

Installation

Install the library using npm:

npm install js-array-filter

Usage

Creating a Filter instance

import Filter from 'js-array-filter';

const columns = [
    { name: 'AGE', dataType: 'number' },
    { name: 'SEX', dataType: 'string' }
];

const filter = new Filter('parsed', columns, {
    conditions: [
        { variable: 'AGE', operator: 'gt', value: 80 },
        { variable: 'SEX', operator: 'eq', value: 'M' }
    ],
    connectors: ['and']
});

Creating a Filter instance from a string

import Filter from 'js-array-filter';

const columns = [
    { name: 'AGE', dataType: 'number' },
    { name: 'SEX', dataType: 'string' }
];

const filterString = "AGE gt 80 and SEX eq 'M'";
const filter = new Filter('parsed', columns, filterString);

Applying the Filter

const data = [
    [85, 'M'],
    [70, 'F'],
    [90, 'M']
];

const filteredData = data.filter(row => filter.filterRow(row));
console.log(filteredData); // Output: [[85, 'M'], [90, 'M']]

Applying the Filter to a Dataframe

const data = [
    [85, 'M'],
    [70, 'F'],
    [90, 'M']
];

const filteredData = filter.filterDataframe(data);
console.log(filteredData); // Output: [[85, 'M'], [90, 'M']]

Updating the Filter

filter.update({
    conditions: [
        { variable: 'AGE', operator: 'lt', value: 75 }
    ],
    connectors: []
});

const newFilteredData = data.filter(row => filter.filterRow(row));
console.log(newFilteredData); // Output: [[70, 'F']]

Converting Filter to String

const filterString = filter.toString();
console.log(filterString);

Validating Filter String

const isValid = filter.validateFilterString(filterString);
console.log(isValid); // Output: true or false

Operators

String Operators

  • lt: Less than
  • le: Less than or equal to
  • gt: Greater than
  • ge: Greater than or equal to
  • in: In array
  • notin: Not in array
  • eq: Equal to
  • ne: Not equal to
  • starts: Starts with
  • ends: Ends with
  • contains: Contains
  • notcontains: Does not contain
  • regex: Matches regular expression
  • notMissing: Not missing (not null or empty)
  • missing: Missing (null or empty)

Number Operators

  • lt: Less than
  • le: Less than or equal to
  • gt: Greater than
  • ge: Greater than or equal to
  • in: In array
  • notin: Not in array
  • eq: Equal to
  • ne: Not equal to
  • notMissing: Not missing (not null or empty)
  • missing: Missing (null or empty)

Boolean Operators

  • eq: Equal to
  • ne: Not equal to
  • notMissing: Not missing (not null or empty)
  • missing: Missing (null or empty)

Methods

update

Updates the filter with new filter and columns.

Parameters

  • filter (BasicFilter | string): The new filter object or filter string.
  • columns (ColumnMetadata[], optional): The new column metadata.

filterRow

Applies the filter to a single row of data.

Parameters

  • row (ItemDataArray): The row of data to filter.

Returns

  • boolean: True if the row passes the filter, false otherwise.

filterDataframe

Applies the filter to a dataframe (array of rows).

Parameters

  • data (ItemDataArray[]): The dataframe to filter.

Returns

  • ItemDataArray[]: The filtered dataframe.

toString

Converts the filter to a string representation.

Returns

  • string: The string representation of the filter.

validateFilterString

Validates a filter string.

Parameters

  • filterString (string): The filter string to validate.

Returns

  • boolean: True if the filter string is valid, false otherwise.

Running Tests

Run the tests using Jest:

npm test

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author

Dmitry Kolosov

Contributing

Open an issue or submit a pull request for any improvements or bug fixes.

For more details, refer to the source code and the documentation.