Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion libs/pipe/search.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,68 @@
import {
async, inject
} from '@angular/core/testing';
import { Search } from './search.pipe';
import { Search, SearchModel } from './search.pipe';
import * as moment from 'moment';


describe('Pipe: search', () => {
let ourMoment = moment();
const values = [{
'Name': 'Jon',
'Age': 16,
'Today': ourMoment
}];

it('create an instance', () => {
let pipe = new Search();
expect(pipe).toBeTruthy();
});

it('should not perform a search', () => {
let model = new SearchModel(['Name', 'Age', 'Today'], '');
let pipe = new Search();
let results = pipe.transform(values, model);
expect(results).toEqual(values);
});
it('should not find the search', () => {
let model = new SearchModel(['Name', 'Age', 'Today'], 'Bill');
let pipe = new Search();
let results = pipe.transform(values, model);
expect(results).toEqual([]);
});

it('should find the string value', () => {
let model = new SearchModel(['Name', 'Age', 'Today'], 'Jon');
let pipe = new Search();
let results = pipe.transform(values, model);
expect(results).toEqual(values);
});

it('should find the number value', () => {
let model = new SearchModel(['Name', 'Age', 'Today'], 16);
let pipe = new Search();
let results = pipe.transform(values, model);
expect(results).toEqual(values);
});

it('should find the value using an array of search terms', () => {
let model = new SearchModel(['Name', 'Age', 'Today'], ['Jon', 16]);
let pipe = new Search();
let results = pipe.transform(values, model);
expect(results).toEqual(values);
});

it('should find the Moment DateTime object', () => {
let model = new SearchModel(['Name', 'Age', 'Today'], ourMoment);
let pipe = new Search();
let results = pipe.transform(values, model);
expect(results).toEqual(values);
});

it('should find the Moment DateTime object by string', () => {
let model = new SearchModel(['Name', 'Age', 'Today'], ourMoment.format('MM/DD/YYYY'));
let pipe = new Search();
let results = pipe.transform(values, model);
expect(results).toEqual(values);
});
});
9 changes: 7 additions & 2 deletions libs/pipe/search.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Pipe, PipeTransform, Injectable } from '@angular/core';
import * as _ from 'lodash';
import * as moment from 'moment';

@Injectable()
export class SearchModel {
Expand All @@ -26,10 +27,11 @@ export class Search implements PipeTransform {
for(let get of gets){
let result;
if(typeof(args.matchObject) === 'string'){
if (this.isMoment(get)) { get = get.format('MM/DD/YYYY'); }
result = get.toString().toLowerCase().includes(args.matchObject.toLowerCase());
} else if (args.matchObject instanceof Array) {
result = _.some(args.matchObject, x => x === get);
} else {
result = _.some(args.matchObject, x => x === get);
} else {
result = get === args.matchObject;
}
if( result === true) return result;
Expand All @@ -38,5 +40,8 @@ export class Search implements PipeTransform {
});
return returnVal;
}
private isMoment(val: moment.Moment | string): val is moment.Moment {
return (<moment.Moment>val).format !== undefined;
}

}