Giter Club home page Giter Club logo

trieste's Introduction

trieste

NPM

NPM version Build Status Coverage Status

Trie generator.

Repl.it | JSFiddle | Examples

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

trieste's People

Contributors

remarkablemark avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.