Package Exports
- subsequence-search
- subsequence-search/build/subsequence-search.min.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (subsequence-search) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
subsequence-search
Search for a given subsequence in a list of strings and transform the resulting list as required.
It behaves a lot like the sublime text fuzzy search.
The resulting list can be transformed using chainable transforms.
Demo it here.
###Installation
npm install subsequence-search --save####Node
Go ahead and require('subsequence-search) in node after installation.
####Browser
After installation, serve:
subsequence-search.jsorsubsequence-search.min.js
out of node_modules/subsequence-search/build/
In your browser code, go ahead and require('subsequence-search) to use it.
###Usage
####search(dataList, searchString, transforms)
dataListis an array ofstrings that you want to match againstsearchStringis thestringyou want to match against thedataListtransformsis anobjectcontaining transform functions that are applied in order to the data list got after matchingsearchStringanddataList. (transforms are explained later)
E.g.,
var subsearch = require('subsequence-search');
var data = ['there is some fog', 'have an apple', 'omg! potato?', 'foxes are kinda cool!'];
console.log(subsearch.search(data, 'fo', {
rank: subsearch.transforms.rank,
highlight: subsearch.transforms.highlight('highlightClass')
}));
//output
//["<span class="highlightClass">f</span><span class="highlightClass">o</span>xes are kinda cool!", "there is some <span class="highlightClass">f</span><span class="highlightClass">o</span>g"]####transforms
A transform is a function that accepts an Array and returns a transformed Array.
The Array received by a transform function is of the form of an Array returned by String.prototype.match.
For example:
var subsearch = require('subsequence-search');
//lets say you have the following data
var data = ['there is some fog', 'have an apple', 'omg! potato?', 'foxes are kinda cool!'];
//and you do
subsearch.search(data, 'fo', {
myTransform: function(list){
console.log(list);
return list;
}
});
//then you get an array containing to arrays printed in your console
//see the image below
As you can see in the image, each item is the same as what you get when you do 'some string'.match(/^(s)(.*?)(e)(.*)$/) i.e., a match with some capturing groups.
You can chain as many transforms as you want by passing them in the transforms object to the search call.
The only thing to keep in mind is that they are applied in order.
Keeping that in mind, you can do what you wish in those transforms to get the data in a format that is useful for your application.
subsequence-search ships with three transforms for your convenience. They are:
rank: re-order the result to have most relevant results firsthighlight: accepts acssclass and transforms the result set to encapsulate the matching letters in aspanwith the givencss class.noHighlight: returns back plaintext matches
These are available on the transforms property on the object you get when you do require('subsequence-search') i.e.,
var subsearch = require('subsequence-search');
//built in transforms:
//subsearch.transforms.rank
//subsearch.transforms.highlight(classname)
//subsearch.transforms.noHighlight
var data = ['there is some fog', 'have an apple', 'omg! potato?', 'foxes are kinda cool!'];
console.log(subsearch.search(data, 'fo', {
rank: subsearch.transforms.rank,
highlight: subsearch.transforms.highlight('highlightClass')
}));
//output
//["<span class="highlightClass">f</span><span class="highlightClass">o</span>xes are kinda cool!", "there is some <span class="highlightClass">f</span><span class="highlightClass">o</span>g"]###Changelog
- 0.1.2
- Fixed
package.json(missing git repo)
- Fixed
- 0.1.1
- Fixed documentation (added demo)
- 0.1.0
- added chainable
transforms - added test suite
- added chainable