projects/i-components/src/lib/pipes/textsearchmode.pipe.ts
Pipe created for the RadComboBox Component
It searches text elements in an array
Name | textSearchModePipe |
containsCase |
containsCase(displayMemberPath: string, collection: any, term: string)
|
If the TextSearchMode property is Contains, it will execute the respective filtering
Returns :
any[]
|
containsCaseSensitiveCase |
containsCaseSensitiveCase(displayMemberPath: string, collection: any, term: string)
|
If the ContainsCaseSensitive property is Contains, it will execute the respective filtering
Returns :
any[]
|
exact |
exact(displayMemberPath: string, collection: any, term: string)
|
Returns the members of collection which are exactly equal to the given term.
Returns :
any[]
{any[]} |
filterCollection | |||||||||||||||
filterCollection(collection: any, term: string, filterMode: string | TextSearchMode, displayMemberPath: string)
|
|||||||||||||||
Filters the collection according to the specified filter mode.
Parameters :
Returns :
any[]
{any[]} |
startsWithCase |
startsWithCase(displayMemberPath: string, collection: any, term: string)
|
If the TextSearchMode property is StartWith, it will execute the respective filtering
Returns :
any[]
|
startsWithCaseSensitiveCase |
startsWithCaseSensitiveCase(displayMemberPath: string, collection: any, term: string)
|
If the TextSearchMode property is StartsWithCaseSensitive, it will execute the respective filtering
Returns :
any[]
|
transform | ||||||||||||||||||
transform(collection: any, term: string, filterMode: string | TextSearchMode, enabled: boolean, displayMemberPath: string)
|
||||||||||||||||||
Parameters :
Returns :
any
|
import { Pipe, PipeTransform } from '@angular/core';
import { TextSearchMode, ItemsCollectionModel } from '@mobilize/wms-framework';
/**
* Pipe created for the RadComboBox Component
*
* It searches text elements in an array
*
* @export
* @class TextSearchModePipe
* @implements {PipeTransform}
*/
@Pipe({
name: 'textSearchModePipe',
pure: false,
})
export class TextSearchModePipe implements PipeTransform {
transform(
collection: any,
term: string,
filterMode: string | TextSearchMode,
enabled: boolean,
displayMemberPath: string
): any {
term = term || '';
if (enabled) {
/* istanbul ignore else */
if (collection instanceof ItemsCollectionModel) {
collection = collection.internalArray;
}
return this.filterCollection(
collection,
term,
filterMode,
displayMemberPath
);
} else {
return collection;
}
}
/**
* Filters the collection according to the specified filter mode.
*
* @param {*} collection
* @param {string} term
* @param {(string | TextSearchMode)} filterMode
* @param {string} displayMemberPath
* @return {*} {any[]}
* @memberof TextSearchModePipe
*/
filterCollection(
collection: any,
term: string,
filterMode: string | TextSearchMode,
displayMemberPath: string
): any[] {
let arr: any[];
if (
filterMode === TextSearchMode.StartsWith ||
filterMode === 'StartsWith'
) {
arr = this.startsWithCase(displayMemberPath, collection, term);
} else if (
filterMode === TextSearchMode.StartsWithCaseSensitive ||
filterMode === 'StartsWithCaseSensitive'
) {
arr = this.startsWithCaseSensitiveCase(
displayMemberPath,
collection,
term
);
} else if (
filterMode === TextSearchMode.Contains ||
filterMode === 'Contains'
) {
arr = this.containsCase(displayMemberPath, collection, term);
} else if (
filterMode === TextSearchMode.ContainsCaseSensitive ||
filterMode === 'ContainsCaseSensitive'
) {
arr = this.containsCaseSensitiveCase(displayMemberPath, collection, term);
} else if (filterMode === 'Exact') {
arr = this.exact(displayMemberPath, collection, term);
} else {
arr = collection;
}
return arr;
}
/**
* If the TextSearchMode property is StartWith, it will execute the respective filtering
*
* @returns {any[]}
* @memberof TextSearchModePipe
*/
startsWithCase(
displayMemberPath: string,
collection: any,
term: string
): any[] {
if (displayMemberPath) {
return collection.filter((item) =>
item?.[displayMemberPath]
?.toString()
.toLowerCase()
.startsWith(term.toString().toLowerCase())
);
} else {
return collection.filter((item) =>
item?.toString().toLowerCase().startsWith(term.toString().toLowerCase())
);
}
}
/**
* If the TextSearchMode property is StartsWithCaseSensitive, it will execute the respective filtering
*
* @returns {any[]}
* @memberof TextSearchModePipe
*/
startsWithCaseSensitiveCase(
displayMemberPath: string,
collection: any,
term: string
): any[] {
if (displayMemberPath) {
return collection.filter((item) =>
item?.[displayMemberPath]?.toString().startsWith(term.toString())
);
} else {
return collection.filter((item) =>
item?.toString().startsWith(term.toString())
);
}
}
/**
* If the TextSearchMode property is Contains, it will execute the respective filtering
*
* @returns {any[]}
* @memberof TextSearchModePipe
*/
containsCase(
displayMemberPath: string,
collection: any,
term: string
): any[] {
if (displayMemberPath) {
return collection.filter((item) =>
item?.[displayMemberPath]
?.toString()
.toLowerCase()
.includes(term.toString().toLowerCase())
);
} else {
return collection.filter((item) =>
item?.toString().toLowerCase().includes(term.toString().toLowerCase())
);
}
}
/**
* If the ContainsCaseSensitive property is Contains, it will execute the respective filtering
*
* @returns {any[]}
* @memberof TextSearchModePipe
*/
containsCaseSensitiveCase(
displayMemberPath: string,
collection: any,
term: string
): any[] {
if (displayMemberPath) {
return collection.filter((item) =>
item?.[displayMemberPath]?.toString().includes(term.toString())
);
} else {
return collection.filter((item) =>
item?.toString().includes(term.toString())
);
}
}
/**
* Returns the members of collection which are exactly equal to the given term.
*
* @param {string} displayMemberPath
* @param {*} collection
* @param {string} term
* @return {*} {any[]}
* @memberof TextSearchModePipe
*/
exact(displayMemberPath: string, collection: any, term: string): any[] {
if (displayMemberPath) {
return collection.filter(
(item) => item?.[displayMemberPath]?.toString() === term.toString()
);
} else {
return collection.filter((item) => item?.toString() === term.toString());
}
}
}