diff --git a/package.json b/package.json index 7e6ff1e4..0a1a5dee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ngx-pipes", - "version": "1.4.1", + "version": "1.4.2", "author": "Dan Revah", "description": "Useful angular2 pipes", "license": "MIT", diff --git a/src/app/pipes/array/filter-by.spec.ts b/src/app/pipes/array/filter-by.spec.ts index 8964af64..1eb2e80c 100644 --- a/src/app/pipes/array/filter-by.spec.ts +++ b/src/app/pipes/array/filter-by.spec.ts @@ -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(() => { @@ -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'); diff --git a/src/app/pipes/array/filter-by.ts b/src/app/pipes/array/filter-by.ts index 26d91175..f993ebc1 100644 --- a/src/app/pipes/array/filter-by.ts +++ b/src/app/pipes/array/filter-by.ts @@ -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, 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; } diff --git a/src/app/pipes/helpers/helpers.ts b/src/app/pipes/helpers/helpers.ts index e7773dcf..223c1a44 100644 --- a/src/app/pipes/helpers/helpers.ts +++ b/src/app/pipes/helpers/helpers.ts @@ -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'; }