Faster Custom Fuzzy Search

Developer
Size
2,739 Kb
Views
36,432

How do I make an faster custom fuzzy search?

What is a faster custom fuzzy search? How do you make a faster custom fuzzy search? This script and codes were developed by Kev Rousseau on 12 August 2022, Friday.

Faster Custom Fuzzy Search Previews

Faster Custom Fuzzy Search - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Faster Custom Fuzzy Search</title>
</head>
<body> <input id="fuzzy" type="text"/> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Faster Custom Fuzzy Search - Script Codes JS Codes

var _data = [
{ "name": "Ortiz", "slug": "Harriett"
}, { "name": "Clayton", "slug": "Ginger"
}, { "name": "Noble", "slug": "Bettye"
}, { "name": "Suzanne", "slug": "Rodriquez"
}, { "name": "Ruby", "slug": "Terri"
}, { "name": "Robyn", "slug": "Kennedy"
}, { "name": "Bond", "slug": "Meyer"
}, { "name": "Alyce", "slug": "Golden"
}, { "name": "Shannon", "slug": "Alta"
}, { "name": "Mercer", "slug": "Ada"
}, { "name": "Jimmie", "slug": "Hansen"
}, { "name": "Cobb", "slug": "Roach"
}, { "name": "Geneva", "slug": "Cooke"
}, { "name": "Tonia", "slug": "Erin"
}, { "name": "Christina", "slug": "Louise"
}, { "name": "Charlene", "slug": "Saundra"
}, { "name": "Dolly", "slug": "Salazar"
}, { "name": "Barrera", "slug": "Julianne"
}, { "name": "Blevins", "slug": "Hicks"
}, { "name": "Blair", "slug": "Lillie"
}, { "name": "Adrienne", "slug": "Owens"
}, { "name": "Mendoza", "slug": "Patrica"
}, { "name": "Gates", "slug": "Sofia"
}, { "name": "Maura", "slug": "Fisher"
}, { "name": "Audrey", "slug": "Hughes"
}, { "name": "Lucille", "slug": "Goodwin"
}, { "name": "Aline", "slug": "Jaime"
}, { "name": "Simone", "slug": "Leona"
}, { "name": "Roy", "slug": "Ilene"
}, { "name": "Alexander", "slug": "Maxwell"
}, { "name": "Chang", "slug": "Ana"
}, { "name": "Conrad", "slug": "Allison"
}, { "name": "James", "slug": "Jacquelyn"
}, { "name": "Greer", "slug": "Kim"
}, { "name": "Bernard", "slug": "Snow"
}, { "name": "Workman", "slug": "Buck"
}, { "name": "Serena", "slug": "Velma"
}, { "name": "Leanne", "slug": "Mcdaniel"
}, { "name": "Bolton", "slug": "Mcfadden"
}, { "name": "Branch", "slug": "Deena"
}, { "name": "Mai", "slug": "Case"
}, { "name": "Nettie", "slug": "Kathy"
}, { "name": "Stacy", "slug": "Wendi"
}, { "name": "Kate", "slug": "Erika"
}, { "name": "Madeline", "slug": "Spears"
}, { "name": "Watts", "slug": "Ochoa"
}, { "name": "Evelyn", "slug": "Grimes"
}, { "name": "Christian", "slug": "Norris"
}, { "name": "Zamora", "slug": "Ayers"
}, { "name": "Veronica", "slug": "Josephine"
}
];
var a = function(options) { this.init = function(options) { //scoring works like golf, the lowest score wins this.options = options || {}; this.maxResults = this.options.maxResults || 3; this.attr = this.options.attr || "slug"; this.scoreMap = []; }; this.init(options); this.fuzzySearch = function(text) { if (text === "") { return []; } var chunks = text; //loop through the slugs in data, and loop in the chunks. var data = JSON.parse(JSON.stringify(_data)); for (var i = 0; i < data.length; i++) { var fuzzy_match = (function() { var cache = _.memoize(function(str) { return new RegExp("^" + str.replace(/./g, function(x) { return /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/.test(x) ? "\\" + x + "?" : x + "?"; }) + "$"); }); return function(str, pattern) { return cache(str).test(pattern); }; })(); var match = fuzzy_match(data[i][this.attr].toLowerCase(), text); if (match) { this.scoreMap.push(data[i]); } } //return this.getBestScored(this.scoreMap); var score = this.scoreMap; this.free(); return score; }; this.getBestScored = function(data) { //sortby score from lowest value to -1 value (2,5,10,13,18,-1,-1) var bestScored = _.chain(data) .reject(function(item) { return item.score < 0; }) .sortBy('score') .first(this.maxResults) .value(); this.free(); return bestScored; }; this.free = function() { //re-init to reset values this.init(options); };
};
var app = new a({ maxResults: 10, attr: "slug"
});
$("#fuzzy").on("keyup", function() { console.clear(); console.log(app.fuzzySearch(this.value));
});
Faster Custom Fuzzy Search - Script Codes
Faster Custom Fuzzy Search - Script Codes
Home Page Home
Developer Kev Rousseau
Username kevrousso
Uploaded August 12, 2022
Rating 3
Size 2,739 Kb
Views 36,432
Do you need developer help for Faster Custom Fuzzy Search?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Kev Rousseau (kevrousso) Script Codes
Create amazing blog posts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!