String search functionSimplifying linked list “search and delete” functionString search & replace in CJava String search and replaceWildcard string search with captureLaravel 4 search functionBinary search function in C#Search function for booksJavascript/Typescript anagram String searchCase insensitive sub-string searchConvert 'rgb()' string to hex string
How do I hide Chekhov's Gun?
Should we release the security issues we found in our product as CVE or we can just update those on weekly release notes?
Define, (actually define) the "stability" and "energy" of a compound
Existence of subset with given Hausdorff dimension
A link redirect to http instead of https: how critical is it?
Professor being mistaken for a grad student
Are there other languages, besides English, where the indefinite (or definite) article varies based on sound?
My adviser wants to be the first author
What exactly is this small puffer fish doing and how did it manage to accomplish such a feat?
If curse and magic is two sides of the same coin, why the former is forbidden?
What are the naunces between the use of 訊く instead of 聞く in the following sentence?
Why is the President allowed to veto a cancellation of emergency powers?
How to explain that I do not want to visit a country due to personal safety concern?
What is the significance behind "40 days" that often appears in the Bible?
How difficult is it to simply disable/disengage the MCAS on Boeing 737 Max 8 & 9 Aircraft?
What approach do we need to follow for projects without a test environment?
How big is a MODIS 250m pixel in reality?
Why do Australian milk farmers need to protest supermarkets' milk price?
Is it possible to upcast ritual spells?
Why one should not leave fingerprints on bulbs and plugs?
Why does Bach not break the rules here?
How to simplify this time periods definition interface?
Welcoming 2019 Pi day: How to draw the letter π?
How could a scammer know the apps on my phone / iTunes account?
String search function
Simplifying linked list “search and delete” functionString search & replace in CJava String search and replaceWildcard string search with captureLaravel 4 search functionBinary search function in C#Search function for booksJavascript/Typescript anagram String searchCase insensitive sub-string searchConvert 'rgb()' string to hex string
$begingroup$
I'm trying to return a list of objects called orders
based on a string search that the user enters. orders
has a title
and a description
. The function loops through the orders object and tries to find matches between the search string and order titles or descriptions. The search should be able to find matches even if there are words in the string we are comparing to, and then sort the results by how many matches are found for each order.
import EditOrderComponent from './edit-order/edit-order.component';
import DeleteOrderComponent from './delete-order/delete-order.component';
import MatDialog from '@angular/material';
import YearService from './../year.service';
import CreateOrderService from '../order.service';
import Component, OnInit from '@angular/core';
import Subscription from 'rxjs';
import Order from '../order.model';
import ActivatedRoute from '@angular/router';
import AdminService from '../admin.service';
@Component(
selector: 'app-view',
templateUrl: './view.component.html',
styleUrls: ['./view.component.css']
)
export class ViewComponent implements OnInit string;
urlYear: any;
isAdmin: boolean;
isAdminCreate: boolean;
myModel = true;
order = 'DESC';
titles = 'titleDESC';
pub = 'pubDESC';
enteredSearch = '';
misMatchSearch = '';
searching = false;
orderCount = 0;
constructor(public adminService: AdminService, public orderService: CreateOrderService, public yearsService: YearService,
private route: ActivatedRoute, public dialog: MatDialog)
ngOnInit()
this.yearsService.cast$.subscribe(current => (this.currentYear = current));
this.adminService.isAdminLogin();
this.adminService.cast$.subscribe(admin =>
(this.isAdmin = admin)
);
this.adminService.caseSOA$.subscribe(create =>
(
this.isAdminCreate = create
)
);
this.orderService.getOrders('All Years');
this.orderSub = this.orderService
.getOrdersUpdatedListener()
.subscribe((orders: Order[]) =>
this.orders = orders;
this.orderCount = this.orders.length;
);
isOrdersAvailable(): Boolean
if ( this.orderCount > 0)
return true;
return false;
resetSearch()
this.orderService.getOrders('All Years');
this.orderSub = this.orderService
.getOrdersUpdatedListener()
.subscribe((orders: Order[]) =>
this.orders = orders;
this.orderCount = this.orders.length;
);
this.enteredSearch = null;
this.searching = false;
// order
// title: string,
// descrtion: string,
// ...
//
onSearch()
if ( this.enteredSearch && this.enteredSearch.length > 0)
this.orderService.getOrders('All Years');
this.orderSub = this.orderService
.getOrdersUpdatedListener()
.subscribe((orders: Order[]) =>
this.orders = orders;
this.orders = this.searchOrders(this.enteredSearch, this.orders);
if ( this.orders )
this.orderCount = this.orders.length;
if ( this.orderCount === 0)
this.setUnfoundSearch();
);
this.searching = true;
setUnfoundSearch()
this.misMatchSearch = this.enteredSearch;
searchOrders(search: string, orders: Order[]): Order[]
if ( search )
search = search.trim();
// Array passed by reference.
const tempOrders = [...orders];
const unsortedOrders = this.searchDriver(search, tempOrders);
const orderToReturn = this.sortResults(unsortedOrders);
return orderToReturn;
// Builds string[]s to compare and compares.
searchDriver(search: string, orders: Order[]): any[]
const tempOrders = [...orders];
const ascOrders = [];
const searchArray = this.splitString(search);
for (let i = 0; i < tempOrders.length; i++)
// Combine the title and desctiption into one long string[].
const compare = [];
let tempArray = this.splitString(tempOrders[i].title);
for (let j = 0; j < tempArray.length; j++)
compare.push(tempArray[j]);
tempArray = this.splitString(tempOrders[i].description);
for (let k = 0; k < tempArray.length; k++)
compare.push(tempArray[k]);
compare.push(tempOrders[i].orderID);
const amountSame = this.arrayCompare(searchArray, compare);
// If more than one match is found add it to the array.
if (amountSame >= 1)
const anOrder = tempOrders[i];
const feed = amountSame, anOrder ;
ascOrders.push(feed);
return ascOrders;
// Break down strings into individual words to compare.
splitString(stringToSplit: string): string[]
const string = stringToSplit;
let stringArray = [];
stringArray = string.split(' ');
return stringArray;
// compare two arrays search and title/desc values
arrayCompare(searchArray: string[], compareToArray: string[]): number
let counter = 0;
for (let searchIndex = 0; searchIndex < searchArray.length; searchIndex++)
for (let compareToIndex = 0; compareToIndex < compareToArray.length; compareToIndex++)
if (this.compareStrings(searchArray[searchIndex], compareToArray[compareToIndex]))
counter++;
return counter;
/*
// Meat and potatoes of the code, does the word comparisons.
compareStrings(searchFor: string, compareTo: string): boolean
searchFor = searchFor.toLowerCase();
// strips numbers and symbols from a word.
compareTo = compareTo.replace(/W/g, '');
if (searchFor === compareTo.toLowerCase())
return true;
return false;
*/
// Meat and potatoes of the code, does the word comparisons by character values instead of whole words.
compareStrings(searchFor: string, compareTo: string): boolean
searchFor = searchFor.toLowerCase();
// building string to compareTo...
for (let stringIndex = 0; stringIndex <= compareTo.length - searchFor.length; stringIndex++)
let buildingString = '';
for (let searchLength = 0; searchLength < searchFor.length; searchLength++)
buildingString = buildingString + compareTo[stringIndex + searchLength];
// finished string
const builtString = buildingString.toLowerCase();
if (searchFor === builtString)
return true;
return false;
// Sort results in relevenace to how many matches were found to be true.
sortResults(ascOrders: any[]): Order[]
const tempLength = ascOrders.length;
const orderToReturn = [];
for (let j = 0; j < tempLength; j++)
let index = 0;
let high = 0;
for (let i = 0; i < ascOrders.length; i++)
if (high < ascOrders[i].amountSame)
high = ascOrders[i].amountSame;
index = i;
orderToReturn.push(ascOrders[index].anOrder);
ascOrders.splice(index, 1);
return orderToReturn;
orderPub()
if ( this.pub === 'pubDESC' )
this.pub = 'pubASC';
else
this.pub = 'pubDESC';
this.orderService.reOrder(this.pub);
orderTitles()
if (this.titles === 'titleDESC')
this.titles = 'titleASC';
else
this.titles = 'titleDESC';
this.orderService.reOrder(this.titles);
orderNums()
if (this.order === 'DESC')
this.order = 'ASC';
else
this.order = 'DESC';
this.orderService.reOrder(this.order);
openDialog(order: Order): void
const dialogRef = this.dialog.open(DeleteOrderComponent,
data:
anOrder: order
);
dialogRef.afterClosed().subscribe(result =>
if ( result === 'Delete')
this.onDelete(order._id);
);
editOrder(order: Order): void
const dialogRef = this.dialog.open(EditOrderComponent,
data:
anOrder: order
);
dialogRef.afterClosed().subscribe();
orderYearsToShow(order: Order): boolean (!order.isPub && this.isAdmin === true)) && this.currentYear.toString() === 'All Years' )
return true;
else return false;
onDelete(_id: number)
this.orderService.deleteOrder(_id);
An example output would be if someone entered: "Sub" the function would match with "Subpoena". There is an alternative method that would require the entire word to match.
search typescript
$endgroup$
add a comment |
$begingroup$
I'm trying to return a list of objects called orders
based on a string search that the user enters. orders
has a title
and a description
. The function loops through the orders object and tries to find matches between the search string and order titles or descriptions. The search should be able to find matches even if there are words in the string we are comparing to, and then sort the results by how many matches are found for each order.
import EditOrderComponent from './edit-order/edit-order.component';
import DeleteOrderComponent from './delete-order/delete-order.component';
import MatDialog from '@angular/material';
import YearService from './../year.service';
import CreateOrderService from '../order.service';
import Component, OnInit from '@angular/core';
import Subscription from 'rxjs';
import Order from '../order.model';
import ActivatedRoute from '@angular/router';
import AdminService from '../admin.service';
@Component(
selector: 'app-view',
templateUrl: './view.component.html',
styleUrls: ['./view.component.css']
)
export class ViewComponent implements OnInit string;
urlYear: any;
isAdmin: boolean;
isAdminCreate: boolean;
myModel = true;
order = 'DESC';
titles = 'titleDESC';
pub = 'pubDESC';
enteredSearch = '';
misMatchSearch = '';
searching = false;
orderCount = 0;
constructor(public adminService: AdminService, public orderService: CreateOrderService, public yearsService: YearService,
private route: ActivatedRoute, public dialog: MatDialog)
ngOnInit()
this.yearsService.cast$.subscribe(current => (this.currentYear = current));
this.adminService.isAdminLogin();
this.adminService.cast$.subscribe(admin =>
(this.isAdmin = admin)
);
this.adminService.caseSOA$.subscribe(create =>
(
this.isAdminCreate = create
)
);
this.orderService.getOrders('All Years');
this.orderSub = this.orderService
.getOrdersUpdatedListener()
.subscribe((orders: Order[]) =>
this.orders = orders;
this.orderCount = this.orders.length;
);
isOrdersAvailable(): Boolean
if ( this.orderCount > 0)
return true;
return false;
resetSearch()
this.orderService.getOrders('All Years');
this.orderSub = this.orderService
.getOrdersUpdatedListener()
.subscribe((orders: Order[]) =>
this.orders = orders;
this.orderCount = this.orders.length;
);
this.enteredSearch = null;
this.searching = false;
// order
// title: string,
// descrtion: string,
// ...
//
onSearch()
if ( this.enteredSearch && this.enteredSearch.length > 0)
this.orderService.getOrders('All Years');
this.orderSub = this.orderService
.getOrdersUpdatedListener()
.subscribe((orders: Order[]) =>
this.orders = orders;
this.orders = this.searchOrders(this.enteredSearch, this.orders);
if ( this.orders )
this.orderCount = this.orders.length;
if ( this.orderCount === 0)
this.setUnfoundSearch();
);
this.searching = true;
setUnfoundSearch()
this.misMatchSearch = this.enteredSearch;
searchOrders(search: string, orders: Order[]): Order[]
if ( search )
search = search.trim();
// Array passed by reference.
const tempOrders = [...orders];
const unsortedOrders = this.searchDriver(search, tempOrders);
const orderToReturn = this.sortResults(unsortedOrders);
return orderToReturn;
// Builds string[]s to compare and compares.
searchDriver(search: string, orders: Order[]): any[]
const tempOrders = [...orders];
const ascOrders = [];
const searchArray = this.splitString(search);
for (let i = 0; i < tempOrders.length; i++)
// Combine the title and desctiption into one long string[].
const compare = [];
let tempArray = this.splitString(tempOrders[i].title);
for (let j = 0; j < tempArray.length; j++)
compare.push(tempArray[j]);
tempArray = this.splitString(tempOrders[i].description);
for (let k = 0; k < tempArray.length; k++)
compare.push(tempArray[k]);
compare.push(tempOrders[i].orderID);
const amountSame = this.arrayCompare(searchArray, compare);
// If more than one match is found add it to the array.
if (amountSame >= 1)
const anOrder = tempOrders[i];
const feed = amountSame, anOrder ;
ascOrders.push(feed);
return ascOrders;
// Break down strings into individual words to compare.
splitString(stringToSplit: string): string[]
const string = stringToSplit;
let stringArray = [];
stringArray = string.split(' ');
return stringArray;
// compare two arrays search and title/desc values
arrayCompare(searchArray: string[], compareToArray: string[]): number
let counter = 0;
for (let searchIndex = 0; searchIndex < searchArray.length; searchIndex++)
for (let compareToIndex = 0; compareToIndex < compareToArray.length; compareToIndex++)
if (this.compareStrings(searchArray[searchIndex], compareToArray[compareToIndex]))
counter++;
return counter;
/*
// Meat and potatoes of the code, does the word comparisons.
compareStrings(searchFor: string, compareTo: string): boolean
searchFor = searchFor.toLowerCase();
// strips numbers and symbols from a word.
compareTo = compareTo.replace(/W/g, '');
if (searchFor === compareTo.toLowerCase())
return true;
return false;
*/
// Meat and potatoes of the code, does the word comparisons by character values instead of whole words.
compareStrings(searchFor: string, compareTo: string): boolean
searchFor = searchFor.toLowerCase();
// building string to compareTo...
for (let stringIndex = 0; stringIndex <= compareTo.length - searchFor.length; stringIndex++)
let buildingString = '';
for (let searchLength = 0; searchLength < searchFor.length; searchLength++)
buildingString = buildingString + compareTo[stringIndex + searchLength];
// finished string
const builtString = buildingString.toLowerCase();
if (searchFor === builtString)
return true;
return false;
// Sort results in relevenace to how many matches were found to be true.
sortResults(ascOrders: any[]): Order[]
const tempLength = ascOrders.length;
const orderToReturn = [];
for (let j = 0; j < tempLength; j++)
let index = 0;
let high = 0;
for (let i = 0; i < ascOrders.length; i++)
if (high < ascOrders[i].amountSame)
high = ascOrders[i].amountSame;
index = i;
orderToReturn.push(ascOrders[index].anOrder);
ascOrders.splice(index, 1);
return orderToReturn;
orderPub()
if ( this.pub === 'pubDESC' )
this.pub = 'pubASC';
else
this.pub = 'pubDESC';
this.orderService.reOrder(this.pub);
orderTitles()
if (this.titles === 'titleDESC')
this.titles = 'titleASC';
else
this.titles = 'titleDESC';
this.orderService.reOrder(this.titles);
orderNums()
if (this.order === 'DESC')
this.order = 'ASC';
else
this.order = 'DESC';
this.orderService.reOrder(this.order);
openDialog(order: Order): void
const dialogRef = this.dialog.open(DeleteOrderComponent,
data:
anOrder: order
);
dialogRef.afterClosed().subscribe(result =>
if ( result === 'Delete')
this.onDelete(order._id);
);
editOrder(order: Order): void
const dialogRef = this.dialog.open(EditOrderComponent,
data:
anOrder: order
);
dialogRef.afterClosed().subscribe();
orderYearsToShow(order: Order): boolean (!order.isPub && this.isAdmin === true)) && this.currentYear.toString() === 'All Years' )
return true;
else return false;
onDelete(_id: number)
this.orderService.deleteOrder(_id);
An example output would be if someone entered: "Sub" the function would match with "Subpoena". There is an alternative method that would require the entire word to match.
search typescript
$endgroup$
add a comment |
$begingroup$
I'm trying to return a list of objects called orders
based on a string search that the user enters. orders
has a title
and a description
. The function loops through the orders object and tries to find matches between the search string and order titles or descriptions. The search should be able to find matches even if there are words in the string we are comparing to, and then sort the results by how many matches are found for each order.
import EditOrderComponent from './edit-order/edit-order.component';
import DeleteOrderComponent from './delete-order/delete-order.component';
import MatDialog from '@angular/material';
import YearService from './../year.service';
import CreateOrderService from '../order.service';
import Component, OnInit from '@angular/core';
import Subscription from 'rxjs';
import Order from '../order.model';
import ActivatedRoute from '@angular/router';
import AdminService from '../admin.service';
@Component(
selector: 'app-view',
templateUrl: './view.component.html',
styleUrls: ['./view.component.css']
)
export class ViewComponent implements OnInit string;
urlYear: any;
isAdmin: boolean;
isAdminCreate: boolean;
myModel = true;
order = 'DESC';
titles = 'titleDESC';
pub = 'pubDESC';
enteredSearch = '';
misMatchSearch = '';
searching = false;
orderCount = 0;
constructor(public adminService: AdminService, public orderService: CreateOrderService, public yearsService: YearService,
private route: ActivatedRoute, public dialog: MatDialog)
ngOnInit()
this.yearsService.cast$.subscribe(current => (this.currentYear = current));
this.adminService.isAdminLogin();
this.adminService.cast$.subscribe(admin =>
(this.isAdmin = admin)
);
this.adminService.caseSOA$.subscribe(create =>
(
this.isAdminCreate = create
)
);
this.orderService.getOrders('All Years');
this.orderSub = this.orderService
.getOrdersUpdatedListener()
.subscribe((orders: Order[]) =>
this.orders = orders;
this.orderCount = this.orders.length;
);
isOrdersAvailable(): Boolean
if ( this.orderCount > 0)
return true;
return false;
resetSearch()
this.orderService.getOrders('All Years');
this.orderSub = this.orderService
.getOrdersUpdatedListener()
.subscribe((orders: Order[]) =>
this.orders = orders;
this.orderCount = this.orders.length;
);
this.enteredSearch = null;
this.searching = false;
// order
// title: string,
// descrtion: string,
// ...
//
onSearch()
if ( this.enteredSearch && this.enteredSearch.length > 0)
this.orderService.getOrders('All Years');
this.orderSub = this.orderService
.getOrdersUpdatedListener()
.subscribe((orders: Order[]) =>
this.orders = orders;
this.orders = this.searchOrders(this.enteredSearch, this.orders);
if ( this.orders )
this.orderCount = this.orders.length;
if ( this.orderCount === 0)
this.setUnfoundSearch();
);
this.searching = true;
setUnfoundSearch()
this.misMatchSearch = this.enteredSearch;
searchOrders(search: string, orders: Order[]): Order[]
if ( search )
search = search.trim();
// Array passed by reference.
const tempOrders = [...orders];
const unsortedOrders = this.searchDriver(search, tempOrders);
const orderToReturn = this.sortResults(unsortedOrders);
return orderToReturn;
// Builds string[]s to compare and compares.
searchDriver(search: string, orders: Order[]): any[]
const tempOrders = [...orders];
const ascOrders = [];
const searchArray = this.splitString(search);
for (let i = 0; i < tempOrders.length; i++)
// Combine the title and desctiption into one long string[].
const compare = [];
let tempArray = this.splitString(tempOrders[i].title);
for (let j = 0; j < tempArray.length; j++)
compare.push(tempArray[j]);
tempArray = this.splitString(tempOrders[i].description);
for (let k = 0; k < tempArray.length; k++)
compare.push(tempArray[k]);
compare.push(tempOrders[i].orderID);
const amountSame = this.arrayCompare(searchArray, compare);
// If more than one match is found add it to the array.
if (amountSame >= 1)
const anOrder = tempOrders[i];
const feed = amountSame, anOrder ;
ascOrders.push(feed);
return ascOrders;
// Break down strings into individual words to compare.
splitString(stringToSplit: string): string[]
const string = stringToSplit;
let stringArray = [];
stringArray = string.split(' ');
return stringArray;
// compare two arrays search and title/desc values
arrayCompare(searchArray: string[], compareToArray: string[]): number
let counter = 0;
for (let searchIndex = 0; searchIndex < searchArray.length; searchIndex++)
for (let compareToIndex = 0; compareToIndex < compareToArray.length; compareToIndex++)
if (this.compareStrings(searchArray[searchIndex], compareToArray[compareToIndex]))
counter++;
return counter;
/*
// Meat and potatoes of the code, does the word comparisons.
compareStrings(searchFor: string, compareTo: string): boolean
searchFor = searchFor.toLowerCase();
// strips numbers and symbols from a word.
compareTo = compareTo.replace(/W/g, '');
if (searchFor === compareTo.toLowerCase())
return true;
return false;
*/
// Meat and potatoes of the code, does the word comparisons by character values instead of whole words.
compareStrings(searchFor: string, compareTo: string): boolean
searchFor = searchFor.toLowerCase();
// building string to compareTo...
for (let stringIndex = 0; stringIndex <= compareTo.length - searchFor.length; stringIndex++)
let buildingString = '';
for (let searchLength = 0; searchLength < searchFor.length; searchLength++)
buildingString = buildingString + compareTo[stringIndex + searchLength];
// finished string
const builtString = buildingString.toLowerCase();
if (searchFor === builtString)
return true;
return false;
// Sort results in relevenace to how many matches were found to be true.
sortResults(ascOrders: any[]): Order[]
const tempLength = ascOrders.length;
const orderToReturn = [];
for (let j = 0; j < tempLength; j++)
let index = 0;
let high = 0;
for (let i = 0; i < ascOrders.length; i++)
if (high < ascOrders[i].amountSame)
high = ascOrders[i].amountSame;
index = i;
orderToReturn.push(ascOrders[index].anOrder);
ascOrders.splice(index, 1);
return orderToReturn;
orderPub()
if ( this.pub === 'pubDESC' )
this.pub = 'pubASC';
else
this.pub = 'pubDESC';
this.orderService.reOrder(this.pub);
orderTitles()
if (this.titles === 'titleDESC')
this.titles = 'titleASC';
else
this.titles = 'titleDESC';
this.orderService.reOrder(this.titles);
orderNums()
if (this.order === 'DESC')
this.order = 'ASC';
else
this.order = 'DESC';
this.orderService.reOrder(this.order);
openDialog(order: Order): void
const dialogRef = this.dialog.open(DeleteOrderComponent,
data:
anOrder: order
);
dialogRef.afterClosed().subscribe(result =>
if ( result === 'Delete')
this.onDelete(order._id);
);
editOrder(order: Order): void
const dialogRef = this.dialog.open(EditOrderComponent,
data:
anOrder: order
);
dialogRef.afterClosed().subscribe();
orderYearsToShow(order: Order): boolean (!order.isPub && this.isAdmin === true)) && this.currentYear.toString() === 'All Years' )
return true;
else return false;
onDelete(_id: number)
this.orderService.deleteOrder(_id);
An example output would be if someone entered: "Sub" the function would match with "Subpoena". There is an alternative method that would require the entire word to match.
search typescript
$endgroup$
I'm trying to return a list of objects called orders
based on a string search that the user enters. orders
has a title
and a description
. The function loops through the orders object and tries to find matches between the search string and order titles or descriptions. The search should be able to find matches even if there are words in the string we are comparing to, and then sort the results by how many matches are found for each order.
import EditOrderComponent from './edit-order/edit-order.component';
import DeleteOrderComponent from './delete-order/delete-order.component';
import MatDialog from '@angular/material';
import YearService from './../year.service';
import CreateOrderService from '../order.service';
import Component, OnInit from '@angular/core';
import Subscription from 'rxjs';
import Order from '../order.model';
import ActivatedRoute from '@angular/router';
import AdminService from '../admin.service';
@Component(
selector: 'app-view',
templateUrl: './view.component.html',
styleUrls: ['./view.component.css']
)
export class ViewComponent implements OnInit string;
urlYear: any;
isAdmin: boolean;
isAdminCreate: boolean;
myModel = true;
order = 'DESC';
titles = 'titleDESC';
pub = 'pubDESC';
enteredSearch = '';
misMatchSearch = '';
searching = false;
orderCount = 0;
constructor(public adminService: AdminService, public orderService: CreateOrderService, public yearsService: YearService,
private route: ActivatedRoute, public dialog: MatDialog)
ngOnInit()
this.yearsService.cast$.subscribe(current => (this.currentYear = current));
this.adminService.isAdminLogin();
this.adminService.cast$.subscribe(admin =>
(this.isAdmin = admin)
);
this.adminService.caseSOA$.subscribe(create =>
(
this.isAdminCreate = create
)
);
this.orderService.getOrders('All Years');
this.orderSub = this.orderService
.getOrdersUpdatedListener()
.subscribe((orders: Order[]) =>
this.orders = orders;
this.orderCount = this.orders.length;
);
isOrdersAvailable(): Boolean
if ( this.orderCount > 0)
return true;
return false;
resetSearch()
this.orderService.getOrders('All Years');
this.orderSub = this.orderService
.getOrdersUpdatedListener()
.subscribe((orders: Order[]) =>
this.orders = orders;
this.orderCount = this.orders.length;
);
this.enteredSearch = null;
this.searching = false;
// order
// title: string,
// descrtion: string,
// ...
//
onSearch()
if ( this.enteredSearch && this.enteredSearch.length > 0)
this.orderService.getOrders('All Years');
this.orderSub = this.orderService
.getOrdersUpdatedListener()
.subscribe((orders: Order[]) =>
this.orders = orders;
this.orders = this.searchOrders(this.enteredSearch, this.orders);
if ( this.orders )
this.orderCount = this.orders.length;
if ( this.orderCount === 0)
this.setUnfoundSearch();
);
this.searching = true;
setUnfoundSearch()
this.misMatchSearch = this.enteredSearch;
searchOrders(search: string, orders: Order[]): Order[]
if ( search )
search = search.trim();
// Array passed by reference.
const tempOrders = [...orders];
const unsortedOrders = this.searchDriver(search, tempOrders);
const orderToReturn = this.sortResults(unsortedOrders);
return orderToReturn;
// Builds string[]s to compare and compares.
searchDriver(search: string, orders: Order[]): any[]
const tempOrders = [...orders];
const ascOrders = [];
const searchArray = this.splitString(search);
for (let i = 0; i < tempOrders.length; i++)
// Combine the title and desctiption into one long string[].
const compare = [];
let tempArray = this.splitString(tempOrders[i].title);
for (let j = 0; j < tempArray.length; j++)
compare.push(tempArray[j]);
tempArray = this.splitString(tempOrders[i].description);
for (let k = 0; k < tempArray.length; k++)
compare.push(tempArray[k]);
compare.push(tempOrders[i].orderID);
const amountSame = this.arrayCompare(searchArray, compare);
// If more than one match is found add it to the array.
if (amountSame >= 1)
const anOrder = tempOrders[i];
const feed = amountSame, anOrder ;
ascOrders.push(feed);
return ascOrders;
// Break down strings into individual words to compare.
splitString(stringToSplit: string): string[]
const string = stringToSplit;
let stringArray = [];
stringArray = string.split(' ');
return stringArray;
// compare two arrays search and title/desc values
arrayCompare(searchArray: string[], compareToArray: string[]): number
let counter = 0;
for (let searchIndex = 0; searchIndex < searchArray.length; searchIndex++)
for (let compareToIndex = 0; compareToIndex < compareToArray.length; compareToIndex++)
if (this.compareStrings(searchArray[searchIndex], compareToArray[compareToIndex]))
counter++;
return counter;
/*
// Meat and potatoes of the code, does the word comparisons.
compareStrings(searchFor: string, compareTo: string): boolean
searchFor = searchFor.toLowerCase();
// strips numbers and symbols from a word.
compareTo = compareTo.replace(/W/g, '');
if (searchFor === compareTo.toLowerCase())
return true;
return false;
*/
// Meat and potatoes of the code, does the word comparisons by character values instead of whole words.
compareStrings(searchFor: string, compareTo: string): boolean
searchFor = searchFor.toLowerCase();
// building string to compareTo...
for (let stringIndex = 0; stringIndex <= compareTo.length - searchFor.length; stringIndex++)
let buildingString = '';
for (let searchLength = 0; searchLength < searchFor.length; searchLength++)
buildingString = buildingString + compareTo[stringIndex + searchLength];
// finished string
const builtString = buildingString.toLowerCase();
if (searchFor === builtString)
return true;
return false;
// Sort results in relevenace to how many matches were found to be true.
sortResults(ascOrders: any[]): Order[]
const tempLength = ascOrders.length;
const orderToReturn = [];
for (let j = 0; j < tempLength; j++)
let index = 0;
let high = 0;
for (let i = 0; i < ascOrders.length; i++)
if (high < ascOrders[i].amountSame)
high = ascOrders[i].amountSame;
index = i;
orderToReturn.push(ascOrders[index].anOrder);
ascOrders.splice(index, 1);
return orderToReturn;
orderPub()
if ( this.pub === 'pubDESC' )
this.pub = 'pubASC';
else
this.pub = 'pubDESC';
this.orderService.reOrder(this.pub);
orderTitles()
if (this.titles === 'titleDESC')
this.titles = 'titleASC';
else
this.titles = 'titleDESC';
this.orderService.reOrder(this.titles);
orderNums()
if (this.order === 'DESC')
this.order = 'ASC';
else
this.order = 'DESC';
this.orderService.reOrder(this.order);
openDialog(order: Order): void
const dialogRef = this.dialog.open(DeleteOrderComponent,
data:
anOrder: order
);
dialogRef.afterClosed().subscribe(result =>
if ( result === 'Delete')
this.onDelete(order._id);
);
editOrder(order: Order): void
const dialogRef = this.dialog.open(EditOrderComponent,
data:
anOrder: order
);
dialogRef.afterClosed().subscribe();
orderYearsToShow(order: Order): boolean (!order.isPub && this.isAdmin === true)) && this.currentYear.toString() === 'All Years' )
return true;
else return false;
onDelete(_id: number)
this.orderService.deleteOrder(_id);
An example output would be if someone entered: "Sub" the function would match with "Subpoena". There is an alternative method that would require the entire word to match.
search typescript
search typescript
edited 7 hours ago
Happy Sheep
asked Feb 23 at 0:28
Happy SheepHappy Sheep
12
12
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214083%2fstring-search-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214083%2fstring-search-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown