|
| 1 | + |
| 2 | +/** |
| 3 | + * Function to prevent unexpected regex expressions from the required string. |
| 4 | + * |
| 5 | + * @param {String} str |
| 6 | + * @returns { String } |
| 7 | + */ |
| 8 | + const escape = function (str = ''){ |
| 9 | + return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&') |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Funcion principal de busqueda. |
| 14 | + * |
| 15 | + * @param {Array} json // Array a investigar. |
| 16 | + * @param {String} parametro // Cadena de texto a evaluar con los elementos. |
| 17 | + * @param {Array} whitelist // Nombre de las keys a ignorar. |
| 18 | + * @returns { Array } |
| 19 | + */ |
| 20 | +const search = function (json, parametro, whitelist = []){ |
| 21 | + const scapedParam = escape(parametro); |
| 22 | + const parametroRegex = new RegExp(scapedParam, "gi") |
| 23 | + const arrayFiltrada = searcher(json, parametroRegex, whitelist) |
| 24 | + return arrayFiltrada |
| 25 | +} |
| 26 | + |
| 27 | +// Json es cualquier json o array clave valor (Funciona sin clave, genial), |
| 28 | +// parametro debe ser una expresión regular a evaluar. |
| 29 | +/** |
| 30 | + * Function to iterate in the given array and look for matches in the key values. |
| 31 | + * |
| 32 | + * @param {Array} json // Array a investigar. |
| 33 | + * @param {String} parametro // Cadena de texto a evaluar con los elementos. |
| 34 | + * @param {Array} whitelist // Nombre de las keys a ignorar. |
| 35 | + * @returns |
| 36 | + */ |
| 37 | +const searcher = function(json, parametro, whitelist){ |
| 38 | + |
| 39 | + // La voy a llenar de los elementos del array los cuales al menos |
| 40 | + // en sus elementos o elementos anidados hay una coincidencia. |
| 41 | + const filterBySearchArray = []; |
| 42 | + |
| 43 | + // Recorrer todos los elementos del array del json |
| 44 | + try { |
| 45 | + json.forEach((element) => { |
| 46 | + // Recorrer todas las claves del objeto |
| 47 | + for (const key in element) { |
| 48 | + if (whitelist.includes(key)) { |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + // Esto es por si no es clave valor, verifique inmediatamente |
| 53 | + // el valor del elemento actual del array, si encuentra un |
| 54 | + // numero o string, verificar match. |
| 55 | + if ( ['string', 'number'].includes(typeof element) ){ |
| 56 | + const hallado = element.toString().match(parametro) |
| 57 | + if (hallado !== null) { |
| 58 | + filterBySearchArray.push(element) |
| 59 | + return |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Si es clave valor y encuentra un numero o string, verificar match. |
| 64 | + if ( ['string', 'number'].includes(typeof element[key]) ) { |
| 65 | + const hallado = element[key].toString().match(parametro) |
| 66 | + if (hallado !== null) { |
| 67 | + filterBySearchArray.push(element) |
| 68 | + return |
| 69 | + } |
| 70 | + } |
| 71 | + // Si no es lo anterior y es un array, entonces llamar de nuevo |
| 72 | + // en la funcion si hay mas elementos dentro del array. |
| 73 | + else if (Array.isArray(element[key])) { |
| 74 | + const recursivo = searcher(element[key], parametro, whitelist); |
| 75 | + if (recursivo.length > 0) { |
| 76 | + filterBySearchArray.push(element) |
| 77 | + return; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + }); |
| 82 | + } catch (error) { |
| 83 | + throw new Error('The given element is not an iterable array.') |
| 84 | + } |
| 85 | + |
| 86 | + return filterBySearchArray; |
| 87 | +} |
| 88 | + |
| 89 | +export { search } |
0 commit comments