Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

trieste

remarkablemark37MIT0.2.0

Trie generator.

trieste, trie, data structure, digital tree, radix tree, prefix tree, search tree

readme

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

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.2.0 (2019-08-30)

Features

  • rename package name from tried to trieste (c612f2d)

0.1.0 (2019-08-30)

Bug Fixes

  • index: do not override nodes that share the same key (5ae0d9c)
  • index: make sure to delete the correct key during remove (098a216)

Build System

  • package: build umd library with rollup (adc37db)
  • package: minify rollup output with uglifyjs (e376252)

Features

  • change methods so arguments are accepted instead of one (e8ceb14)
  • index: accept string argument for trie functions (64c9910)
  • clone project from npm-package-template (9eab0ad)
  • index: accept an array of strings as 1st argument for remove (608aad1)
  • index: add contains that returns false for invalid argument (d84e597)
  • index: add get method that returns the end of word value (879a7f1)
  • index: add remove to remove a string from the trie (4f205ee)
  • index: add constructor Trie and update main export tried (5c41248)
  • index: add end of word constants and generate basic trie (32e016b)
  • index: allow add to method chain by returning trie instance (2ad16cf)
  • index: include add method that adds words to trie (4bb9f00)
  • index: return {} when undefined or [] is passed (f4d6a6b)
  • index: update contains to check if string is in trie (1f1f8fa)
  • index: update arguments that can return {} (4229e25)
  • lib: add utilities.assign, which polyfills Object.assign (5b187eb)
  • update tried & Trie to receive options instead of arguments (a937202)
  • lib: update add and remove to use options (be41621)
  • lib: update add to allow custom value to be set (350be0e)
  • lib: update contains and get to use options (eadd0af)

Tests

  • data: refactor tests and move invalid arguments to data (325533b)
  • index: add more test cases (8d00f45)
  • index: add test case for 'remove' if argument is invalid (ec99efd)
  • index: add test for adding to and removing from trie (0533fde)
  • index: add tests for add and remove with custom options (b3f4d7e)
  • index: add tests for contains and get with custom options (c998122)
  • index: fix incorrect test in 'add' (fb875e3)
  • index: move test data & constants from index.js to data.js (ccc1eaa)
  • index: organize tests under suite data (c882101)
  • index: refactor add/remove test to use key/value constants (6f8ef27)
  • index: replace assert.ok with assert.strictEqual (a72c841)
  • index: tidy tests so they're more readable (85e5ffa)
  • index: update the 'add' test suite to use the data (3fa8794)
  • index: verify string is not removed from trie if not found (61bec46)
  • utilities: add case for 'assign' when source is not an object (e679b67)
  • refactor test files to use constants from lib/constants.js (f52a539)
  • utilities: add tests for utilities.isObjectEmpty (3053868)