Browse Source

Use const when possible

KaKi87 3 months ago
parent
commit
06316f09e3
2 changed files with 36 additions and 33 deletions
  1. 34
    31
      public/assets/js/index.js
  2. 2
    2
      public/assets/js/main.js

+ 34
- 31
public/assets/js/index.js View File

@@ -3,7 +3,7 @@ Search
3 3
  */
4 4
 
5 5
 // Android versions
6
-let androidVersions = [
6
+const androidVersions = [
7 7
 	'1.0', '1.1', '1.5', '1.6',
8 8
 	'2.0', '2.0.1', '2.1', '2.2', '2.2.1', '2.2.2', '2.2.3', '2.3', '2.3.1', '2.3.2', '2.3.3', '2.3.4', '2.3.5', '2.3.6', '2.3.7',
9 9
 	'3.0', '3.1', '3.2', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5', '3.2.6',
@@ -23,14 +23,14 @@ androidVersions.forEach(v => document.querySelector('#minAndroidVersion').innerH
23 23
  * @param callback
24 24
  */
25 25
 let lastQuery = '';
26
-let search = (query, callback) => {
26
+const search = (query, callback) => {
27 27
 	if(query.name === lastQuery)
28 28
 		query.number = 120;
29 29
 	socket.emit('search', query);
30 30
 	socket.on('res', res => {
31
-		let r = [];
31
+		const r = [];
32 32
 		for(let i = 0; i < res.length; i++){
33
-			let _c = [];
33
+			const _c = [];
34 34
 			for(let j = 0; j < query.conditions.length; j++){
35 35
 				_c.push(query.conditions[j](res[i]));
36 36
 			}
@@ -63,7 +63,7 @@ let search = (query, callback) => {
63 63
 /*
64 64
 Conditions checking functions
65 65
  */
66
-let _c = {
66
+const _c = {
67 67
 	maxPrice: (app, n) => app.price <= n,
68 68
 	adFree: app => !app.adSupported,
69 69
 	inAppFree: app => !app.offersIAP,
@@ -77,10 +77,10 @@ let _c = {
77 77
 // Global search settings
78 78
 let Query_Conditions, Query_Price, Query_Number;
79 79
 // Default search text display (for reset)
80
-let Query_Text = [...document.querySelectorAll('.search__settings span')].map(el => el.textContent);
81
-let Query_Set = [...document.querySelectorAll('.search__setting')].map(el => el.classList.contains('search__setting--set'));
80
+const Query_Text = [...document.querySelectorAll('.search__settings span')].map(el => el.textContent);
81
+const Query_Set = [...document.querySelectorAll('.search__setting')].map(el => el.classList.contains('search__setting--set'));
82 82
 
83
-let resetSettings = () => {
83
+const resetSettings = () => {
84 84
 	// Reset search settings
85 85
 	Query_Conditions = [_c.adFree, _c.inAppFree];
86 86
 	Query_Price = 'free';
@@ -104,19 +104,19 @@ resetSettings();
104 104
 document.querySelector('.resetSettings').onclick = resetSettings;
105 105
 
106 106
 // Reset arrow sort symbols in results table
107
-let resetSorting = () => document.querySelectorAll('.output__header th').forEach(el => el.classList.remove('output__header--sortUp', 'output__header--sortDown'));
107
+const resetSorting = () => document.querySelectorAll('.output__header th').forEach(el => el.classList.remove('output__header--sortUp', 'output__header--sortDown'));
108 108
 
109
-let searchInput = document.querySelector('.search__input');
110
-let searchButton = document.querySelector('.search__submit');
109
+const searchInput = document.querySelector('.search__input');
110
+const searchButton = document.querySelector('.search__submit');
111 111
 
112 112
 searchButton.onclick = () => {
113 113
 	// Empty query won't work
114 114
 	if(!searchInput.value) return;
115 115
 	// Time search
116
-	let startTime = Date.now();
117
-	let outputMessage = document.querySelector('.output__message');
116
+	const startTime = Date.now();
117
+	const outputMessage = document.querySelector('.output__message');
118 118
 	// Query settings
119
-	let query = { name: searchInput.value, number: Query_Number, price: Query_Price, conditions: Query_Conditions };
119
+	const query = { name: searchInput.value, number: Query_Number, price: Query_Price, conditions: Query_Conditions };
120 120
 	// Let user know that I'm searching
121 121
 	outputMessage.innerHTML = '<div class="spinner" data-uk-spinner="ratio: 0.75"></div></div>Searching<span class="loading-dots"></span>';
122 122
 	// Reset and hide previous search elements
@@ -131,18 +131,18 @@ searchButton.onclick = () => {
131 131
 			return outputMessage.textContent = 'No result.';
132 132
 		}
133 133
 		// Calculate elapsed time
134
-		let elapsedTime = ((Date.now() - startTime) / 1000).toFixed(2);
134
+		const elapsedTime = ((Date.now() - startTime) / 1000).toFixed(2);
135 135
 		/*
136 136
 		Show results
137 137
 		 */
138 138
 		// Message
139 139
 		outputMessage.textContent = `Results : ${res.length} items (${(res.length / query.number * 100).toFixed(2)}% of ${query.number} apps), elapsed time : ${elapsedTime}s`;
140
-		let displayedPermissions = [];
140
+		const displayedPermissions = [];
141 141
 		res.forEach(app => {
142 142
 			// Show all permissions
143 143
 			app.permissions.forEach(permission => {
144
-				let p = permission.permission;
145
-				let d = permission.description;
144
+				const p = permission.permission;
145
+				const d = permission.description;
146 146
 				if(displayedPermissions.indexOf(p) === -1){
147 147
 					displayedPermissions.push(p);
148 148
 					document.querySelector('.output2__data').innerHTML += `<tr class="output2__permission"><th class="output2__permission__name" data-uk-tooltip="title: ${d}; pos: right">${p}</th></tr>`;
@@ -183,16 +183,16 @@ searchInput.onkeydown = e => {
183 183
 
184 184
 searchInput.focus();
185 185
 
186
-let inputs = document.querySelectorAll('.search__settings input, .search__settings select');
186
+const inputs = document.querySelectorAll('.search__settings input, .search__settings select');
187 187
 inputs.forEach(el => el.oninput = () => {
188 188
 	/*
189 189
 	Set query conditions from user input
190 190
 	 */
191
-	let filter = el.id;
191
+	const filter = el.id;
192 192
 	Query_Conditions = [];
193 193
 	inputs.forEach(el => {
194 194
 		if([el.min, 'on', ''].indexOf(el.value) === -1 || el.checked){
195
-			let filter = el.id;
195
+			const filter = el.id;
196 196
 			switch(filter){
197 197
 				case 'minAndroidVersion':
198 198
 					Query_Conditions.push(app => _c[filter](app, el.value));
@@ -208,7 +208,7 @@ inputs.forEach(el => el.oninput = () => {
208 208
 					Query_Conditions.push(_c[filter]);
209 209
 					break;
210 210
 				case 'minInstalls':
211
-					let range = [0, 1, 1000, 10000, 100000, 1000000, 10000000];
211
+					const range = [0, 1, 1000, 10000, 100000, 1000000, 10000000];
212 212
 					Query_Conditions.push(app => _c[filter](app, range[parseInt(el.value)]));
213 213
 					break;
214 214
 			}
@@ -223,10 +223,11 @@ inputs.forEach(el => el.oninput = () => {
223 223
 	 */
224 224
 	Query_Number = Math.floor(Query_Conditions.length * 12.5) || 25;
225 225
 	if(el.type === 'range'){
226
-		let text = document.querySelector(`.${filter}-text`);
227
-		let suffix = '';
228
-		let minText = 'min';
229
-		let maxText = 'max';
226
+		const text = document.querySelector(`.${filter}-text`);
227
+		let
228
+			suffix = '',
229
+			minText = 'min',
230
+			maxText = 'max';
230 231
 		if(filter === 'maxSize') suffix = 'M';
231 232
 		if(filter === 'maxPrice'){
232 233
 			minText = 'free';
@@ -246,7 +247,7 @@ inputs.forEach(el => el.oninput = () => {
246 247
 			text.textContent = maxText;
247 248
 		else {
248 249
 			if(filter === 'minInstalls'){
249
-				let range = ['0', '1', '1k', '10k', '100k', '1M', '10M'];
250
+				const range = ['0', '1', '1k', '10k', '100k', '1M', '10M'];
250 251
 				return text.textContent = range[parseInt(el.value)];
251 252
 			}
252 253
 			text.textContent = `${el.value}${suffix}`;
@@ -263,9 +264,11 @@ document.querySelectorAll('.output__header th').forEach(el => el.onclick = () =>
263 264
 	if(['output__header__description', 'output__header__version'].indexOf(el.className) !== -1) return;
264 265
 	// Reset and re-sort
265 266
 	resetSorting();
266
-	let items = [...document.querySelectorAll('.output__item')];
267
-	let sortedItems = items.slice(0);
268
-	let getItem = item => item.querySelector(`.output__data__${el.className.split('__').slice(-1)[0]}`).textContent;
267
+	const
268
+		items = [...document.querySelectorAll('.output__item')];
269
+	let
270
+		sortedItems = items.slice(0),
271
+		getItem = item => item.querySelector(`.output__data__${el.className.split('__').slice(-1)[0]}`).textContent;
269 272
 	if(el.className === 'output__header__last-update'){
270 273
 		// Use UNIX timestamp instead of interpreted date string
271 274
 		getItem = item => item.querySelector(`.output__data__${el.className.split('__').slice(-1)[0]}`).getAttribute('data-date');
@@ -311,7 +314,7 @@ document.querySelectorAll('.output__header th').forEach(el => el.onclick = () =>
311 314
 	// Hide all items
312 315
 	items.forEach(item => item.parentNode.removeChild(item));
313 316
 	// Unhide all items in proper sort order
314
-	for(let item of sortedItems){
317
+	for(const item of sortedItems){
315 318
 		document.querySelector('.output__data').appendChild(item);
316 319
 	}
317 320
 });

+ 2
- 2
public/assets/js/main.js View File

@@ -8,7 +8,7 @@ setInterval(() => document.querySelectorAll('.loading-dots').forEach(el => el.te
8 8
 /*
9 9
 Dark mode
10 10
  */
11
-let toggleDark = () => {
11
+const toggleDark = () => {
12 12
 	// Toggle UIkit properties
13 13
 	['uk-light', 'uk-background-secondary'].forEach(Class => [document.body, document.querySelector('.uk-modal-body')].forEach(el => el.classList.toggle(Class)));
14 14
 	// Toggle dark button text
@@ -29,4 +29,4 @@ document.querySelector('.dark').onclick = () => {
29 29
 };
30 30
 
31 31
 // Restore saved dark style
32
-if(Cookies.get('dark') === 'true') toggleDark();
32
+if(Cookies.get('dark') === 'true') toggleDark();

Loading…
Cancel
Save