kmaida / ng-searchfiltersort

NPM package providing a service for searching, filtering, and sorting arrays of objects in Angular
MIT License
3 stars 0 forks source link
angular filter npm npm-package orderby search sort sorting

ng-searchfiltersort

This is an Angular library that provides a service for searching, filtering, and sorting arrays of objects. Angular does not support FilterPipe or OrderByPipe like AngularJS does. Instead, the docs declare:

"Any capabilities that you would have put in a pipe and shared across the app can be written in a filtering/sorting service and injected into the component."

-Angular Pipes: No FilterPipe or OrderByPipe

The ng-searchfiltersort library fulfills that need for searching, filtering, and sorting arrays containing objects. It can search, filter, and sort objects with properties that are strings, booleans, numbers, and dates.

Installation

To install ng-searchfiltersort, run:

$ npm install ng-searchfiltersort --save

You can check out / download the service source code here: search-filter-sort.service.ts.

Setup

The SearchFilterSortService is intended to be used as a singleton. You will need to provide the module and call the forRoot() method, as well as provide the service.

Add to Module

In the file containing the @NgModule where you wish to inject module and service (for many projects, this will be app.module.ts), do the following:

// src/app/app.module.ts
...
// Import SearchFilterSort module and service
import { SearchFilterSortModule } from 'ng-searchfiltersort';
import { SearchFilterSortService } from 'ng-searchfiltersort';

@NgModule({
  ...,
  imports: [
    ...,
    SearchFilterSortModule.forRoot()
  ],
  providers: [SearchFilterSortService],
  ...
})
export class AppModule { }

Implement in Components

In components that should use the SearchFilterSortService, be sure to import the service and add it to the component's constructor like so:

...
import { SearchFilterSortService } from 'ng-searchfiltersort';

@Component({
  ...
})
export class MyComponent {
  constructor(private sfs: SearchFilterSortService) {}
  ...

Methods

The following methods are provided by SearchFilterSortService:

search(array, query, excludeProps?, dateFormat?)

The search() method filters an array of objects and returns only items that contain values matching the specified query. It accepts the following arguments:

Note: If you are passing a dateFormat but don't wish to exclude properties from search, the excludeProps argument must be explicitly passed as a falsey value such as false, null, or undefined.

filter(array, property, value)

The filter() method can be used to filter an array of objects, returning only items with specified key/value pairs. It accepts the following arguments:

orderBy(array, prop, reverse?)

The orderBy() method sorts an array of objects by a specified property. Property values can be of type string, number, or boolean (but are expected to be consistent types across all objects in the array). If other value types are found instead, the original array will be returned unsorted. The method accepts the following arguments:

orderByDate(array, prop, reverse?)

The orderByDate() method specifically sorts by dates.

Demo

You can check out a demo Angular app showing how to use the ng-searchfiltersort library in the demo folder in this repository. See the folder's README for installation instructions.

License

MIT © Kim Maida