Created
December 2, 2022 18:40
-
-
Save edgvi10/85a3a18e3fc23ad6f15c997e29a39935 to your computer and use it in GitHub Desktop.
Função de Busca em arrays e Objetos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const search = (list, keyword, options) => { | |
options = (options) ? { ...options } : {}; | |
if (options.caseSensitive === undefined) options.caseSensitive = false; | |
if (options.exactMatch === undefined) options.exactMatch = false; | |
if (options.exactMatch) options.caseSensitive = true; | |
if (options.deepSearch === undefined) options.deepSearch = false; | |
if (options.caseSensitive) keyword = keyword.toString().trim(); | |
else keyword = keyword.toString().trim().toLowerCase(); | |
const sentences = (options.exactMatch) ? [keyword] : keyword.split(" "); | |
if (typeof list === "object") list = Object.values(list); | |
const checkItem = (item, sentence) => { | |
if (typeof item === "string" || typeof item === "number") { | |
item = item.toString().trim(); | |
if (!options.caseSensitive) item = item.toLowerCase(); | |
if (options.exactMatch) return item === sentence; | |
else return (item.indexOf(sentence) > -1); | |
} else if (typeof item === "object" && options.deepSearch) { | |
for (const key in item) if (checkItem(item[key], sentence)) return true; | |
} else return false; | |
} | |
const findInList = (list, sentence) => { | |
const found = []; | |
for (const item of list) { | |
if (typeof item === "object") { | |
const keys = (options.keys === undefined) ? Object.keys(item) : options.keys; | |
for (const key of keys) if (checkItem(item[key], sentence)) found.push(item); | |
} else { | |
if (checkItem(item, sentence)) found.push(item); | |
} | |
} | |
return found; | |
} | |
var results = list; | |
for (const sentence of sentences) { | |
results = findInList(results, sentence); | |
if (results.length === 0) break; | |
} | |
return results; | |
} | |
module.exports = search; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default (list, keyword, options) => { | |
options = (options) ? { ...options } : {}; | |
if (options.caseSensitive === undefined) options.caseSensitive = false; | |
if (options.exactMatch === undefined) options.exactMatch = false; | |
if (options.exactMatch) options.caseSensitive = true; | |
if (options.deepSearch === undefined) options.deepSearch = false; | |
if (options.caseSensitive) keyword = keyword.toString().trim(); | |
else keyword = keyword.toString().trim().toLowerCase(); | |
const sentences = (options.exactMatch) ? [keyword] : keyword.split(" "); | |
if (typeof list === "object") list = Object.values(list); | |
const checkItem = (item, sentence) => { | |
if (typeof item === "string" || typeof item === "number") { | |
item = item.toString().trim(); | |
if (!options.caseSensitive) item = item.toLowerCase(); | |
if (options.exactMatch) return item === sentence; | |
else return (item.indexOf(sentence) > -1); | |
} else if (typeof item === "object" && options.deepSearch) { | |
for (const key in item) if (checkItem(item[key], sentence)) return true; | |
} else return false; | |
} | |
const findInList = (list, sentence) => { | |
const found = []; | |
for (const item of list) { | |
if (typeof item === "object") { | |
const keys = (options.keys === undefined) ? Object.keys(item) : options.keys; | |
for (const key of keys) if (checkItem(item[key], sentence)) found.push(item); | |
} else { | |
if (checkItem(item, sentence)) found.push(item); | |
} | |
} | |
return found; | |
} | |
var results = list; | |
for (const sentence of sentences) { | |
results = findInList(results, sentence); | |
if (results.length === 0) break; | |
} | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment