Faster Custom Fuzzy Search
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 - 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));
});
Developer | Kev Rousseau |
Username | kevrousso |
Uploaded | August 12, 2022 |
Rating | 3 |
Size | 2,739 Kb |
Views | 36,432 |
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 |
Smart fixed ToC | 4,648 Kb |
Custom FuzzySearch | 3,018 Kb |
Menu example 2 | 4,107 Kb |
CSS Collapse boxes | 2,391 Kb |
Square animation on scroll with instant follow, fixed banner | 5,146 Kb |
A Pen by Kev Rousseau | 2,186 Kb |
Square animation on scroll | 4,823 Kb |
Event Dispatching example | 1,725 Kb |
Find needle in haystack by looping through each item one by one | 2,071 Kb |
Base Pen | 1,294 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 |
Spiralator 9000 | AdmiralPotato | 4,671 Kb |
Transitioning application screens with semantically named classes | Djgrant | 3,697 Kb |
Mostly Fluid | Mccreath | 3,308 Kb |
Ripples in water | Nobitagit | 2,704 Kb |
Magnus 3 | ARocketfish | 7,944 Kb |
Countdown Timer | Massiebn | 3,001 Kb |
Donald Trump - The New Yorker | Agbales | 2,502 Kb |
Atom | Bhlaird | 1,932 Kb |
Placeholder support for contentEditable elements, without JavaScript | Flesler | 1,863 Kb |
SVG Basics | HipsterBrown | 1,852 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!