Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 68373ee

Browse filesBrowse files
committed
Add another version 2 search algorithm - test
1 parent 28eb0fb commit 68373ee
Copy full SHA for 68373ee

File tree

Expand file treeCollapse file tree

3 files changed

+124
-2
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+124
-2
lines changed

‎index.html

Copy file name to clipboardExpand all lines: index.html
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ <h1 class="d-flex flex-column align-items-center font-monospace text-uppercase t
244244
<!-- components -->
245245
<script src="/js/components/MainSearchBar.js"></script>
246246
<script src="/js/components/BinarySearch.js"></script>
247+
<script src="/js/components/V2.js"></script>
247248
<script src="/js/components/DropdownSearchFilter.js"></script>
248249
<script src="/js/components/DataDropdownList.js"></script>
249250
<!-- utils -->

‎js/RecipesApp.js

Copy file name to clipboardExpand all lines: js/RecipesApp.js
+14-2Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,23 @@ class RecipesApp {
9898
const binarySearch = new BinarySearch();
9999
const userInputValue = e.target.value;
100100

101-
const isInputValid = binarySearch.inputValidation(userInputValue);
101+
// const isInputValid = binarySearch.inputValidation(userInputValue);
102+
103+
const v2 = new V2();
104+
const isInputValid = v2.inputValidation(userInputValue);
105+
106+
// if (isInputValidByV2) {
107+
108+
// const userInputMatchingDataV2 = v2.isUserValueMatchesByV2(userInputValue, this.recipesData, 10);
109+
// console.log(userInputMatchingDataV2)
110+
// }
102111

103112
if (isInputValid) {
104113

105-
const userInputMatchingData = binarySearch.isUserValueMatches(userInputValue, this.recipesData, 0, this.recipesData.length - 1);
114+
// const userInputMatchingData = binarySearch.isUserValueMatches(userInputValue, this.recipesData, 0, this.recipesData.length - 1);
115+
116+
const userInputMatchingData = v2.isUserValueMatchesByV2(userInputValue, this.recipesData, 10);
117+
console.log(userInputMatchingData)
106118

107119
this.$recipeCards.innerHTML = '';
108120

‎js/components/V2.js

Copy file name to clipboard
+109Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* ------------------------------------------------------------
3+
* Les Petits Plats components/V2.js
4+
* ------------------------------------------------------------
5+
*/
6+
7+
class V2 extends MainSearchBar {
8+
constructor() {
9+
super();
10+
}
11+
12+
/**
13+
* Slices main array into chunks
14+
* Returns matching data of all trunks
15+
* @param {Event & {eventTargetValue: HTMLInputElement}} eventTargetValue
16+
* @param {Array} mainArray - Data array from API
17+
* @param {number} size - size of mainArray trunks
18+
* @returns matchingDataArray
19+
*/
20+
isUserValueMatchesByV2(eventTargetValue, mainArray, size) {
21+
22+
let chunks = [];
23+
24+
this.sliceMainArrayIntoChunks(mainArray, size, chunks);
25+
26+
const searchOrder = this.updateIndexesOrder(chunks);
27+
28+
// console.log(chunks)
29+
// console.log(searchOrder)
30+
31+
let isMatches = [];
32+
33+
isMatches.push(
34+
this.searchMatchingData(chunks, 0, searchOrder, eventTargetValue),
35+
this.searchMatchingData(chunks, 1, searchOrder, eventTargetValue),
36+
this.searchMatchingData(chunks, 2, searchOrder, eventTargetValue),
37+
this.searchMatchingData(chunks, 3, searchOrder, eventTargetValue),
38+
this.searchMatchingData(chunks, 4, searchOrder, eventTargetValue));
39+
40+
return isMatches.flat();
41+
}
42+
43+
searchMatchingData(array, index, order, eventTargetValue) {
44+
let isMatches;
45+
46+
const indexArray = order[index];
47+
48+
const currentArray = array[indexArray];
49+
50+
isMatches = this.isChunkMatches(eventTargetValue, currentArray);
51+
52+
return isMatches;
53+
}
54+
55+
isChunkMatches(expression, array) {
56+
57+
const result = array.filter(el => {
58+
59+
const regExp = new RegExp(expression, 'gmi');
60+
61+
return el.search.match(regExp);
62+
});
63+
64+
return result;
65+
}
66+
67+
sliceMainArrayIntoChunks(mainArray, size, chunks) {
68+
69+
for (let i = 0; i < mainArray.length; i += size) {
70+
const chunk = mainArray.slice(i, i + size);
71+
chunks.push(chunk);
72+
}
73+
74+
return chunks;
75+
}
76+
77+
updateIndexesOrder(chunks) {
78+
79+
let searchOrder = [];
80+
81+
for (let i = 0; i < chunks.length; i++) {
82+
83+
const indexes = chunks.indexOf(chunks[i]);
84+
85+
searchOrder.push(indexes)
86+
}
87+
88+
searchOrder = this.moveSearchOrderIndex(searchOrder);
89+
90+
return searchOrder;
91+
}
92+
93+
moveSearchOrderIndex(array) {
94+
let result = [];
95+
const middle = Math.floor(array.length / 2);
96+
97+
for (let i = 0; i < middle; i++) {
98+
result.push(
99+
array[i],
100+
array[array.length - 1 - i]);
101+
}
102+
103+
if (array.length % 2 !== 0) {
104+
result.push(array[middle]);
105+
}
106+
107+
return result;
108+
}
109+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.