Skip to content
String matching library with support of camel case, dash case, etc...
JavaScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea
dist
spec
.editorconfig
.gitignore
.jshintrc
Gruntfile.js
MIT-LICENSE.txt
README.md
bower.json
karma.conf.js
package.json
release-notes.md
string-case-match.js

README.md

string-case-match

String matching class. Given a set of strings to match, it will search it by different input.
It is able to search strings as abbreviations (e.g. HelloWorld matches hw, hewo, h w, ...). Works in node.js and all modern browsers without any dependencies. This script is intended to use as ranking function for autocompletes, searches for short strings, etc.

See the live demo page to play with it.

Installation

With node.js

npm install string-case-match
bower install string-case-match
var StringCaseMatch = require("string-case-match");
console.log(new StringCaseMatch(["hello"], { start: "<i>", end: "</i>" }).matches("hell"));

In browser with require.js

require(["string-case-match"], function(StringCaseMatch) {
  console.log(new StringCaseMatch(["hello"], { start: "<i>", end: "</i>" }).matches("hell"));
})

Without require.js:

<script src="path/to/string-case-match.js"></script>
<script>
  console.log(new StringCaseMatch(["hello"], { start: "<i>", end: "</i>" }).matches("hell"));
</script>

Usage

You can find actual examples in the spec file but here's a quick example:

// init a matcher instance with strings to search
var matcher = new StringCaseMatch(["HelloWorld", "Hello", "Bye"]);
// get matches against "hell" word (matches will be sorted by rank in descending order)
var matches = matcher.matches("hell"); // ["Hello", "HelloWorld"] 
var matches = matcher.matches("hell", 1); // top 1 match

// to highlight found characters, use this:
var matcher = new StringCaseMatch(["HelloWorld"], { start: "<i>", end: "</i>" });
var matches = matcher.matches("hell"); // matches will look like "<i>Hell</i>oWorld"

// just get a string rank, don't search anything
// rank is [0..1], 0 - no match, 1 - complete match
var rank = new StringCaseMatch().rank("hello-world", "hell"); 

License

string-case-match is redistributed under MIT license. So, you can use it in any projects without attribution.

You can’t perform that action at this time.