Custom FuzzySearch
How do I make an custom fuzzysearch?
What is a custom fuzzysearch? How do you make a custom fuzzysearch? This script and codes were developed by Kev Rousseau on 12 August 2022, Friday.
Custom FuzzySearch - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Custom FuzzySearch</title>
</head>
<body> <input id="fuzzy" type="text"/> <script src='https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Custom FuzzySearch - 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) { //FIXME: "Kennedy" = 5 points, should be 6 points... calculating first "n" twice 1+2+2 //we should create a not copy of the slug text in data[i], and then remove the matching char in the slug, so that when we indexOf, we aren't matching more than once the char var chunks = text; //loop through the slugs in data, and loop in the chunks. // attribuate points based on indexOf char. Top {maxResults} lowest wins. var data = JSON.parse(JSON.stringify(_data)); for(var i = 0; i < data.length; i++) { for(var x = 0; x < chunks.length; x++) { var obj = _.find(this.scoreMap, {slug: _data[i][this.attr]}) || {}; var dataVal = data[i][this.attr].toLowerCase(); var chunkVal = chunks[x].toLowerCase(); var score = dataVal.indexOf(chunkVal); var found = score !== -1; //char found in slug if (found) { obj[this.attr] = _data[i][this.attr]; obj.score = _.isUndefined(obj.score) ? 0 + score : obj.score + score; //if object not already present in collection if (!_.find(this.scoreMap, {slug: _data[i][this.attr]})) { this.scoreMap.push(obj); } } //some char might have been found but some not //in this case, it doesn't match the searched text, so remove it if (!found && !_.isEmpty(obj)) { this.scoreMap = _.reject(this.scoreMap, obj); break; } if (!found) { break; } } } return this.getBestScored(this.scoreMap); }; 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));
});
Developer | Kev Rousseau |
Username | kevrousso |
Uploaded | August 12, 2022 |
Rating | 3 |
Size | 3,018 Kb |
Views | 32,384 |
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!
Name | Size |
Square animation on scroll with instant follow v2 | 8,558 Kb |
Notifications | 3,496 Kb |
Event Dispatching example | 1,725 Kb |
Tutorial-Module | 6,953 Kb |
Menu example 2 | 4,107 Kb |
1-Discover-Backpacker | 8,202 Kb |
Square animation on scroll | 4,823 Kb |
Menu example 1 | 2,685 Kb |
Square animation on scroll with instant follow, fixed banner | 5,146 Kb |
Faster Custom Fuzzy Search | 2,739 Kb |
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!
Name | Username | Size |
Fading gradient button | Insprd | 1,713 Kb |
Twitch JSON API | Jvhti | 2,808 Kb |
Twitch | SarahDunlap | 2,937 Kb |
A Pen by Matt Popovich | Mpopv | 3,349 Kb |
A Pen by utcwebdev | Utcwebdev | 2,856 Kb |
Pure CSS read more toggle | Idered | 2,344 Kb |
Video Player Custom Controls | EleftheriaBatsou | 3,665 Kb |
Pericles mi loro... | Judag | 4,125 Kb |
CardMove | Thompsonemerson | 3,699 Kb |
Material design buttons | Fischaela | 4,381 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!