Skip to content

Commit

Permalink
feat(#26): Add support for boolean values in filterBy
Browse files Browse the repository at this point in the history
  • Loading branch information
danrevah committed Feb 9, 2017
1 parent 1e709bd commit 085d164
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-pipes",
"version": "1.4.1",
"version": "1.4.2",
"author": "Dan Revah",
"description": "Useful angular2 pipes",
"license": "MIT",
Expand Down
16 changes: 12 additions & 4 deletions src/app/pipes/array/filter-by.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ describe('FilterByPipe', () => {
let pipe: FilterByPipe;

const users = [
{id: 1, first_name: 'John', last_name: 'Doe', work: { title: 'Software Engineer', company: 'Foo Tech', previous_company: 'Unknown' }},
{id: 2, first_name: 'Jane', last_name: 'West', work: { title: 'Designer', company: 'AAA Solutions', previous_company: 'Unknown' }},
{id: 3, first_name: 'Bruce', last_name: 'John', work: { title: 'Software Engineer', company: 'Bar Tech', previous_company: 'Unknown' }},
{id: 4, first_name: 'William', last_name: 'Cent', work: { title: 'Designer', company: 'Foo Tech', previous_company: 'Bar Tech' }}
{id: 1, first_name: 'John', last_name: 'Doe', work: { title: 'Software Engineer', company: 'Foo Tech', previous_company: 'Unknown' }, current_employed: true},
{id: 2, first_name: 'Jane', last_name: 'West', work: { title: 'Designer', company: 'AAA Solutions', previous_company: 'Unknown' }, current_employed: false},
{id: 3, first_name: 'Bruce', last_name: 'John', work: { title: 'Software Engineer', company: 'Bar Tech', previous_company: 'Unknown' }, current_employed: true},
{id: 4, first_name: 'William', last_name: 'Cent', work: { title: 'Designer', company: 'Foo Tech', previous_company: 'Bar Tech' }, current_employed: false}
];

beforeEach(() => {
Expand All @@ -29,6 +29,14 @@ describe('FilterByPipe', () => {
expect(filtered[0]).toEqual(users[0]);
});

it('should filter by boolean value', () => {
const filtered = pipe.transform(users, ['current_employed'], true);

expect(filtered.length).toEqual(2);
expect(filtered[0]).toEqual(users[0]);
expect(filtered[1]).toEqual(users[2]);
});

it('should filter by multiple fields with a two result', () => {
const filtered = pipe.transform(users, ['first_name', 'last_name'], 'John');

Expand Down
4 changes: 2 additions & 2 deletions src/app/pipes/array/filter-by.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {PipeTransform, Pipe} from '@angular/core';
import {isString, extractDeepPropertyByMapKey, isNumberFinite} from '../helpers/helpers';
import {isString, extractDeepPropertyByMapKey, isNumberFinite, isBoolean} from '../helpers/helpers';

@Pipe({name: 'filterBy'})
export class FilterByPipe implements PipeTransform {

transform(input: any, props: Array<string>, search: any, strict: boolean = false): any[] {
if (!Array.isArray(input) || (!isString(search) && !isNumberFinite(search))) {
if (!Array.isArray(input) || (!isString(search) && !isNumberFinite(search) && !isBoolean(search))) {
return input;
}

Expand Down
4 changes: 4 additions & 0 deletions src/app/pipes/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export function isString(value: any) {
return typeof value === 'string';
}

export function isBoolean(value: any) {
return typeof value === 'boolean';
}

export function isObject(value: any) {
return value !== null && typeof value === 'object';
}
Expand Down

0 comments on commit 085d164

Please sign in to comment.