trieste

0.2.0 • Public • Published

trieste

NPM

NPM version Build Status Coverage Status

Trie generator.

Installation

NPM:

$ npm install trieste --save

Yarn:

$ yarn add trieste

CDN:

<script src="https://unpkg.com/trieste@latest/dist/trieste.min.js"></script>

Usage

Module

Import the module:

// CommonJS
const trieste = require('trieste');
 
// ES Modules
import trieste from 'trieste';

Trie

Create a trie instance:

const trie = trieste();

This can also be done by instantiating the constructor:

const Trie = require('trieste/lib/trie');
const trie = new Trie();

Options

Options can be set for each instance:

const options = {
  endKey: 'END_OF_STRING_KEY',
  endValue: 'END_OF_STRING_VALUE'
};
const trie = trieste(options);

This can also be achieved with the constructor:

const Trie = require('trieste/lib/trie');
 
const trie = new Trie({
  endKey: 'END_OF_STRING_KEY',
  endValue: 'END_OF_STRING_VALUE'
});

Options are found on the instance's options property:

trie.options;

The default options are:

{
  endKey: '$$',
  endValue: 1
}

Options have a direct effect on the trie's data and methods like add and get.

Data

Data can be found on the instance's data property:

trie.data;

Data is a POJO (Plain Old JavaScript Object), which means it can be converted to JSON:

JSON.stringify(trie.data);

As an example, the following is the output of trieste().add('a').data:

{ a: { '$$': 1 } }

Methods

Add

Add a string to the trie:

trie.add('foo');

Add multiple strings to the trie:

trie.add('foo', 'bar');

Add an array of strings to the trie:

trie.add.apply(trie, ['foo', 'bar']);

Add a string with a value to the trie:

trie.add({ answer: 42 });

This is useful if you want to store value(s) other than the default. See method get on how to retrieve a string value.

Since the method returns its own instance, method chaining is possible:

trie.add('foo').add('bar');

Arguments that are not type string will be skipped.

Contains

Check if a string is found in the trie:

trie.contains('foo');

The method returns a boolean value.

Arguments that are not type string will return false.

Get

Get a string value from the trie:

trie.get('foo');

The value comes from options.endValue, which is 1 by default:

trie.add('foo').get('foo'); // 1

The value can be set using the add method:

trie.add({ foo: 'bar' }).get('foo'); // 'bar'

The value can also be set in options:

const trie = trieste({ endValue: null });
trie.add('foo').get('foo'); // null

Arguments that are not type string will return undefined.

Remove

Remove a string from the trie:

trie.remove('foo');

Remove multiple strings from the trie:

trie.remove('foo', 'bar');

Remove an array of strings from the trie:

trie.remove.apply(trie, ['foo', 'bar']);

Since the method returns its own instance, method chaining is possible:

trie.remove('foo').remove('bar');

Arguments that are not type string will be skipped.

Testing

Run tests:

$ npm test

Run tests in watch mode:

$ npm run test:watch

Run tests with coverage:

$ npm run test:coverage

View coverage in browser:

$ npm run test:coverage:report
$ open coverage/index.html

Lint files:

$ npm run lint

Fix lint errors:

$ npm run lint:fix

Release

Only collaborators with credentials can release and publish:

$ npm run release
$ git push --follow-tags && npm publish

License

MIT

Package Sidebar

Install

npm i trieste

Weekly Downloads

11

Version

0.2.0

License

MIT

Unpacked Size

40.6 kB

Total Files

15

Last publish

Collaborators

  • remarkablemark