Git Product home page Git Product logo

ngfuzzysearch's Introduction

ngFuzzySearch

Based on fuzzysearch by Nicolás Bevacqua, who did a fantastic job, btw!

Tiny and blazing-fast fuzzy search in JavaScript, now with Angular 1.6.x support!

Fuzzy searching allows for flexibly matching a string with partial input, useful for filtering data very quickly based on lightweight user input.

Demo

A fully functional demo is included in the example/ directory.

Installation

Using Bower:

bower install --save ngFuzzySearch

Usage

Include a script tag for ngFuzzySearch in your index.html file:

<script type="text/javascript" src="bower_components/ngFuzzySearch/ngFuzzySearch.js"></script>

Include ngFuzzySearch as a dependency in your app.js file:

angular.module('myApp', ['ngFuzzySearch']);

Include the fuzzySearch Angular Service in your controller and use it! The service returns true if needle matches haystack using a fuzzy-searching algorithm. Note that this program doesn't implement levenshtein distance, but rather a simplified version where there's no approximation. The method will return true only if each character in the needle can be found in the haystack and occurs after the preceding matches.

module.controller('myCtrl', function(fuzzySearch){
	fuzzySearch.find('twl', 'cartwheel') // <- true
	fuzzySearch.find('cart', 'cartwheel') // <- true
	fuzzySearch.find('cw', 'cartwheel') // <- true
	fuzzySearch.find('ee', 'cartwheel') // <- true
	fuzzySearch.find('art', 'cartwheel') // <- true
	fuzzySearch.find('eeel', 'cartwheel') // <- false
	fuzzySearch.find('dog', 'cartwheel') // <- false
})

fuzzysearch(needle, haystack)

Filtering ng-repeat

ngFuzzySearch also comes equipped with a filter, which you can use to filter ng-repeat blocks with a scope variable. The filter syntax goes as follows:

<div class="col-xs-12" ng-repeat="thing in model.things | fuzzyFilter:[needle]:[keyString]">

Where keyString is the key of the iterator object you would like to filter against, i.e. if your object is:

var model = {};
model.things = [{
  _id: 1,
  name: 'Iterator One'
},{
  _id: 2,
  name: 'Iterator Two'
}];
...

If you want to search the name field, in the model.things object, using an input on the same page, you can do the following:

<input type="text" ng-model="model.fuzzySearchValue" />
...
<div class="col-xs-12" ng-repeat="thing in model.things | fuzzyFilter:model.fuzzySearchValue:'name'">

Filtering using multiple fields

If you want to search the name field AND the description field, in the model.things object, using an input on the same page, you can do the following:

<input type="text" ng-model="model.fuzzySearchValue" />
...
<div class="col-xs-12" ng-repeat="thing in model.things | filterMultiple:{name:model.fuzzySearchValue, desc:model.fuzzySearchValue}:'fuzzyFilter'">

NOTE: You can also use filterMultiple to filter through most string-based filters.

Nested Object References

If you want to search using a nested value like person.name, you can do so by passing a dot-notation string to the filter like this:

<input type="text" ng-model="model.fuzzySearchValue" />
...
<div class="col-xs-12" ng-repeat="thing in model.things | fuzzyFilter:model.fuzzySearchValue:'person.name'">

You can also use this in conjunction with filterMulitple and even mix and match like below:

<input type="text" ng-model="model.fuzzySearchValue">
...
<div class="col-xs-12" ng-repeat="thing in model.things | filterMultiple:{'person.name':model.fuzzySearchValue, desc:model.fuzzySearchValue}:'fuzzyFilter'">

Changelog

Version Date Notes
v1.2.0 07/17 Added nested object filtering via dot notation keys
v1.1.2 07/17 Added filterMultiple and alphabetized filtering. Ng-repeat order will be preserved until input starts
v1.1.0 07/17 Added module export as ngFuzzySearch to better support webpack

License - MIT

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

ngfuzzysearch's People

Contributors

bevacqua avatar driannaude avatar jayrhynas avatar

Stargazers

 avatar

Watchers

 avatar  avatar

ngfuzzysearch's Issues

Not for Angular, but for Angular.js

I know this project has been abandoned (is it?), but the description (README) is misleading.
The project is about Angular.js, not Angular.
I suppose this has been written before Angular came in, but I suggest to change this, for the sake of Google searches and to avoid confusion… 🙂

Thanks.

Cannot read property length of undefined

Hi, I get this error:
angular-1.6.10.js:14962 TypeError: Cannot read property 'length' of undefined at Object.applyFilter (ng-fuzzy-search.js:26) at ng-fuzzy-search.js:64 at Object.forEach (angular-1.6.10.js:433) at ng-fuzzy-search.js:63 at fn (eval at compile (angular-1.6.10.js:15813), <anonymous>:4:483) at regularInterceptedExpression (angular-1.6.10.js:16940) at Scope.$digest (angular-1.6.10.js:18538) at Scope.$apply (angular-1.6.10.js:18906) at done (angular-1.6.10.js:12776) at completeRequest (angular-1.6.10.js:13033)
when I use multipleFilter with multiple nested properties, here is the code:
<tr ng-repeat="rosterItem in $data | filterMultiple:{'rosterItem.masterRoster.enterpriseId':searchString, 'rosterItem.masterRoster.resourceFirstName': searchString}: 'fuzzyFilter'">

searchString is an ng-model for a text input. I also injected fuzzySearch into my controller dependencies

Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.