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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jehlicot07/qfilter

v0.7.6

Published

Query library designed for advanced filtering, crafted with ❤ using TypeScript and React. ⚛

Downloads

18

Readme

QFilter

Query library designed for advanced filtering, crafted with ❤ using TypeScript and React. ⚛

logo

Table of Contents

Installation

Install the library using the followind commands:

npm

npm i @jehlicot07/qfilter

Usage

Basic Usage

To start using QFilter, import the necessary components and create filters using the QFilterBuilder class:

import { QFilterBuilder } from "@jehlicot07/qfilter";

const users = [
  { name: "Jhael", age: 20, city: "DN" },
  { name: "Jhael", age: 21, city: "Santiago" },
  { name: "Galva", age: 26, city: "SD" },
  { name: "Galva", age: 26, city: "SDE" },
  { name: "Thomas", age: 20, city: "SDN" },
  { name: "Sthifer", age: 25, city: "SDN" },
  { name: "Enmanuel", age: 19, city: "SDO" },
];

const builder = new QFilterBuilder()
  .condition("name", "Contains", "e")
  .and()
  .condition("age", "GreaterThan", 20);

const QFilter = builder.build();
const filteredUsers = QFilter.filter(users);

console.log(filteredUsers);
// Output: [
//   { name: 'Jhael', age: 21, city: 'Santiago' },
//   { name: 'Sthifer', age: 25, city: 'SDN' }
// ]

Advanced Usage

You can use logical operators and groups to create more complex filters:

import { QFilterBuilder, condition, and, or, not, group } from "@jehlicot07/qfilter";

const builder = new QFilterBuilder()
  .condition("name", "Contains", "e")
  .and()
  .group([condition("age", "GreaterThan", 20), or(), not(condition("city", "Equal", "SD"))]);

const QFilter = builder.build();
const filteredUsers = QFilter.filter(users);

console.log(filteredUsers);
/*  
  OUTPUT:
  { name: "Jhael", age: 20, city: "DN" },
  { name: "Jhael", age: 21, city: "Santiago" },
  { name: "Galva", age: 26, city: "SD" },
  { name: "Galva", age: 26, city: "SDE" },
  { name: "Thomas", age: 20, city: "SDN" },
  { name: "Sthifer", age: 25, city: "SDN" },
   */

UI Usage

Integrate QFilter in your React components for a more interactive experience:

/* eslint-disable @typescript-eslint/no-explicit-any */
import QFilterComponent from "@jehlicot07/qfilter";

type User = {
  name: string;
  age: number;
  company?: { name: string; subgroup?: { subname: string } };
  a?: FilterGroup[];
};

const App = () => {
  const users: User[] = [
    {
      name: "jhael",
      age: 20,
      company: {
        name: "FMP",
      },
      a: [],
    },
    {
      name: "Miguel",
      age: 26,
      company: {
        name: "FMP",
        subgroup: {
          subname: "Company 2",
        },
      },
    },
  ];

  return (
    <QFilterComponent
      dataSource={users}
      onFilter={(data) => {
        const result = data.filter(users);
        console.log(result);
      }}
      columns={[
        { label: "Name", value: "name", type: "text" },
        { label: "Company Name", value: "company?.name", type: "text" },
        {
          label: "Age",
          value: "age",
          type: "number",
        },
      ]}
    />
  );
};

export default App;

UI VIEW

tool

API

QFilterBuilder

| Method Signature | Params | Description | | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- | | condition | field: Join<T>operator: OPvalue: number | string | booleanid: string | numberparentId: string | number | null | Adds a comparison filter. | | group | filters:Array<GroupCondition<T> | Array<GroupCondition<T>>> | Creates a group of filters. | | add | id: string | numberfiltersToAdd: Array<FiltersType<T>>position: "after" | "before"filtersArr?: Array<FiltersType<T>> | undefined | Adds filters at a specified position. | | remove | id: string | numberfilters?: Array<FiltersType<T>> | Removes filters by ID. | | update | id:string | numberfilter: FiltersType<T> filters?: Array<FiltersType<T>> | Updates a filter by ID. | | and | | Adds a logical AND operator. | | or | | Adds a logical OR operator. | | not | | Adds a logical NOT operator. | | build | | Builds and returns a QFilter instance. |

QFilter

filter(dataSource: T[]): readonly T[]

Applies the filters to the given data source and returns the filtered data.

Utilities for group filter

| Method Signature | Description | | ------------------------------------------------- | ----------------------------- | | generateUID() | Generates a random UID. | | condition(field, operator, value, id, parentId) | Creates a condition filter. | | group(filters) | Creates a group of filters. | | and() | Creates a logical AND filter. | | or() | Creates a logical OR filter. | | not() | Creates a logical NOT filter. |

Example

.group([
  condition("age", "GreaterThan", 20),
  or(),
  not(condition("city", "Equal", "SD"), and(), group([condition("age", "GreaterThan", 20)])),
]);

Types Definitions

OP

type OP =
  | "Equals"
  | "NotEquals"
  | "LessThan"
  | "GreaterThan"
  | "GreaterThanOrEqual"
  | "LessThanOrEqual"
  | "Contains"
  | "NotContains"
  | "StartsWith"
  | "NotStartsWith"
  | "EndsWith"
  | "NotEndsWith"
  | ComparisonOperator;

FilterType

type FilterType = "group" | "logicalOperator" | "comparisonOperator";

FilterGroup

type FilterGroup = "(" | ")";

LogicalOperator

type LogicalOperator = "&&" | "||" | "!";

ComparisonOperator

type ComparisonOperator = "===" | "!==" | ">" | "<" | ">=" | "<=";

commonFilterProps<T>

type commonFilterProps<T> = {
  id: string | number;
  parentId?: string | number | null;
  type: FilterType;
  children?: Array<GroupCondition<T>>;
};

FilterLogicalOperator<T>

type FilterLogicalOperator<T> = {
  operator: LogicalOperator;
} & commonFilterProps<T>;

FilterGroupOperator<T>

type FilterGroupOperator<T> = {
  operator: FilterGroup;
} & commonFilterProps<T>;

FilterOperator<T>

type FilterOperator<T> = {
  operator: OP;
  value: string | number | boolean;
  field: Join<T>;
} & commonFilterProps<T>;

FilterBuild<T>

type FilterBuild<T> = FilterGroupOperator<T> | FilterLogicalOperator<T> | FilterOperator<T>;

AddFilterFn<T>

type AddFilterFn<T> = (
  id: string | number,
  field: Join<T>,

  operator: OP,
  value: string | number | boolean,
  filters: Array<FiltersType<T>>,
  parentId: string | number | null
) => Array<FiltersType<T>>;

GroupCondition<T>

type GroupCondition<T> = FilterBuild<T> | AddFilterFn<T>;

FiltersType<T>

type FiltersType<T> =
  | FilterBuild<T>
  | FilterLogicalOperator<T>
  | FilterGroupOperator<T>
  | FilterOperator<T>
  | GroupCondition<T>
  | AddFilterFn<T>;

SelectOption

type SelectOption = {
  label: string;
  value: string | number | boolean;
};

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License.