I'm working with product codes, so a user can only scan these type of product codes into a textarea element.
I don't know if my code is written the best way possible, or has good performance.
I'm using a whitelist solution for preventing bad entries.
It saves into file all product codes after 3 seconds of last keyup.
Is it good to use javascript and jQuery mixed?
And is it the correct way to use a php query to fetch product code and product code ending?
Why is "use strict" mode useless in my code?
"use strict";toFile.focus();var timeoutId;toFile.addEventListener("keyup", event => { clearTimeout(timeoutId); timeoutId = setTimeout(function() { const data = toFile.value.split("\n"); const result = data.unique(); info.textContent = result.length !== data.length ? "Duplicate removed" : ""; toFile.value = result.join('\n'); }, 100); timeoutId = setTimeout(function() { onInput(document.getElementById('toFile')); }, 3000);});if (!Array.prototype.unique) { Object.defineProperty(Array.prototype, "unique", { configurable: false, // these 3 properties default to false so not needed enumerable: false, // But have added them just to show their availability. writable: false, value: function() { const existing = {}; return this.filter(a => existing[a] = !existing[a] ? true : false); } });} else { throw new Error("Array.prototype.unique already defined.");}const regexNounFilters = [<?php $data = $pdo->query("SELECT PRODUCT_CODE AS code, ENDING AS ec FROM test")->fetchAll(PDO::FETCH_OBJ); foreach ($data as $key) { $separator = ($key != end($data)) ? ", " : ''; $std = "/^(" . $key->code . ")([a-zA-Z0-9]{" . $key->ec . "})$/"; echo $std.$separator; }?>];// example: const regexNounFilters = [/^(AAAB)([a-zA-Z0-9]{7})$/, /^(BBBBBC)([a-zA-Z0-9]{9})$/];const extractNouns = string => string .split('\n') .filter(line => regexNounFilters.some(re => line.trim().toUpperCase().match(re) ));function onInput(target) { target.value = extractNouns(target.value).join('\n');}function saveToFile() { console.log('Saving to the db'); toFile = $('#toFile').val().replace(/\n\r?/g, '<br />'); $.ajax({ url: "test2.php", type: "POST", data: {toFile:toFile}, // serializes the form's elements. beforeSend: function(xhr) { // Let them know we are saving $('#status').html('Saving...'); }, success: function(toFile) { // You can get data returned from your ajax call here. ex. jqObj.find('.returned-data').html() // Now show them we saved and when we did var d = new Date(); $('#status').html('Saved! Last: '+ d.toLocaleTimeString()); }, });}$('.form').submit(function(e) { saveToFile(); e.preventDefault();});