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.
- 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.
Install the library using npm:
npm install js-array-filterimportFilterfrom'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']});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);constdata=[[85,'M'],[70,'F'],[90,'M']];constfilteredData=data.filter(row=>filter.filterRow(row));console.log(filteredData);// Output: [[85, 'M'], [90, 'M']]constdata=[[85,'M'],[70,'F'],[90,'M']];constfilteredData=filter.filterDataframe(data);console.log(filteredData);// Output: [[85, 'M'], [90, 'M']]filter.update({conditions: [{variable: 'AGE',operator: 'lt',value: 75}],connectors: []});constnewFilteredData=data.filter(row=>filter.filterRow(row));console.log(newFilteredData);// Output: [[70, 'F']]constfilterString=filter.toString();console.log(filterString);constisValid=filter.validateFilterString(filterString);console.log(isValid);// Output: true or falseimportFilterfrom'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: []});// (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]});lt: Less thanle: Less than or equal togt: Greater thange: Greater than or equal toin: In arraynotin: Not in arrayeq: Equal tone: Not equal tostarts: Starts withends: Ends withcontains: Containsnotcontains: Does not containregex: Matches regular expressionnotMissing: Not missing (not null or empty)missing: Missing (null or empty)
lt: Less thanle: Less than or equal togt: Greater thange: Greater than or equal toin: In arraynotin: Not in arrayeq: Equal tone: Not equal tonotMissing: Not missing (not null or empty)missing: Missing (null or empty)
eq: Equal tone: Not equal tonotMissing: Not missing (not null or empty)missing: Missing (null or empty)
Updates the filter with new filter and columns.
filter(BasicFilter | string): The new filter object or filter string.columns(ColumnMetadata[], optional): The new column metadata.
Applies the filter to a single row of data.
row(ItemDataArray): The row of data to filter.
boolean: True if the row passes the filter, false otherwise.
Applies the filter to a dataframe (array of rows).
data(ItemDataArray[]): The dataframe to filter.
ItemDataArray[]: The filtered dataframe.
Converts the filter to a string representation.
string: The string representation of the filter.
Validates a filter string.
filterString(string): The filter string to validate.
boolean: True if the filter string is valid, false otherwise.
Run the tests using Jest:
npm testThis project is licensed under the MIT License. See the LICENSE file for details.
Dmitry Kolosov
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.