Package Exports
- triematch
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 (triematch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
triematch
Fast Trie structure implementation with matching feature
npm install triematch
Features
- Very fast
get
andmatch
(aka matching) functions - Straightforward
API
- No dependencies
- Very tiny
- Browser compatible (but not tested there just yet)
Roadmap
- Add
Array
specific methods like maybepop
,shift
,forEach
, etc. - Performance improvements by hacking stuff
- Add benchamrks and compare
triematch
with something similar
Examples
Let's learn by example
const Store = require('triematch')
const store = new Store()
Then we can add things to the store
store.set('Michael Joseph', '#8')
store.set('Michael Jones', 888)
store.set('Michael Joneson', { score: 512 })
store.set('Michael Jacobs', [5, 5, 4, 7])
store.set('Michael Jackson', () => 'Moonwalk')
We can use get
function to get exactly that item that was inserted by this key
before
assert(store.get('Michael Joseph') === '#8')
assert(store.get('Michael Jones') === 888)
assert(store.get('Michael Joneson').score === 512)
assert.deepEqual(store.get('Michael Jacobs'), [5, 5, 4, 7])
assert(store.get('Michael Jackson')() === 'Moonwalk')
assert(store.get('Michael') === null)
Or we can use match
function which is similar to the String.prototype.match
but in a way that it uses Trie
structure to get every possible thing that has query
assert(store.match('M').length === 5)
assert(store.match('Michael').length === 5)
assert(store.match('Michael Jones').length === 2) // Jones and Joneson
// As a result there will be at least empty array anyway
assert(Array.isArray(store.match(null)))
assert(store.match('foo').length === 0)
API
Trie
Trie class
Examples
const store = new Trie()
get
Get exact one value like e.g. Map
does
Parameters
query
string
Examples
store.get('Michael')
Returns (any | null)
match
Get all values matching this query
. If count
is greater than 0
then return that many results (it will increase searching in case you have thousands of elements). Will return all results by default
Parameters
Examples
store.match('Michael')
store.match('John', 20)
Returns Array<any>
set
Add value
Parameters
key
stringvalue
any
Examples
store.set('Michael Jackson', { id: 1 })
store.set('Lord Kelvin', 1824)
store.set('William Osler', 'Scientist')
store.set('Anton Webern', [])
store.set('Charles Best', function info () {})
Returns void
remove
Remove value by a given key
Parameters
query
string
Examples
store.remove('Michael Jacobs')
Returns void
reset
Reset the state after which the whole store will be empty
Examples
store.reset()
Returns void
toObject
Return the key - value representation of the state
Examples
store.toObject()
Returns Object
toArray
Return array with just values
Examples
store.toArray()
Returns Array<any>
toJSON
Return state converted to the JSON
Examples
store.toJSON()
Returns string