Skip to content

Repository files navigation

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
  • Compare with variables (e.g., VAR1 = VAR2).
  • Condition priority for complex expressions.

Installation

Install the library using npm:

npm install js-array-filter

Usage

Creating a Filter instance

importFilterfrom'js-array-filter';constcolumns=[{name: 'AGE',dataType: 'number'},{name: 'SEX',dataType: 'string'}];constfilter=newFilter('parsed',columns,{conditions: [{variable: 'AGE',operator: 'gt',value: 80},{variable: 'SEX',operator: 'eq',value: 'M'}],connectors: ['and']});

Creating a Filter instance from a string

importFilterfrom'js-array-filter';constcolumns=[{name: 'AGE',dataType: 'number'},{name: 'SEX',dataType: 'string'}];constfilterString="AGE gt 80 and SEX eq 'M'";constfilter=newFilter('parsed',columns,filterString);

Applying the Filter

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

Applying the Filter to a Dataframe

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

Updating the Filter

filter.update({conditions: [{variable: 'AGE',operator: 'lt',value: 75}],connectors: []});constnewFilteredData=data.filter(row=>filter.filterRow(row));console.log(newFilteredData);// Output: [[70, 'F']]

Converting Filter to String

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

Validating Filter String

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

Comparing with a variable

importFilterfrom'js-array-filter';constcolumns=[{name: 'AVAL',dataType: 'number'},{name: 'BASE',dataType: 'number'}];// AVAL > BASEconstfilter=newFilter('parsed',columns,{conditions: [{variable: 'AVAL',operator: 'gt',value: null,compareVariable: 'BASE'},],connectors: []});

Condition priority

// (AGE > 80 and SEX = "M") or (AGE > 60 and SEX = "F")constfilter=newFilter('parsed',columns,{conditions: [{variable: 'AGE',operator: 'gt',value: 80},{variable: 'SEX',operator: 'eq',value: 'M'},{variable: 'AGE',operator: 'gt',value: 60},{variable: 'SEX',operator: 'eq',value: 'F'}],connectors: ['and','or','and'],connectorPriorities: [1,0,1]});

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.

About

Filter implementation for an array

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages