Skip to content

Commit

Permalink
📚 docs(README): Add simple example.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Dec 19, 2021
1 parent c43f64f commit 4b6d68d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ See [docs](https://trie-data-structure.github.io/uncompressed-trie/index.html).
> `regeneratorRuntime` to be defined, for instance by importing
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
```js
import {Trie, ArrayNode} from '@trie-data-structure/uncompressed-trie';
import {range} from '@iterable-iterator/range';
import {map} from '@iterable-iterator/map';

const encode = (key) => map((i) => key.charCodeAt(i), range(key.length));
const degree = 256;
const trie = new Trie(new ArrayNode(degree));

const set = (key, value) => trie.set(encode(key), value);
const get = (key) => trie.get(encode(key));

set('abra', 1);
set('cadabra', 2);
set('abracadabra', 3);

get('abra'); // 1
get('cadabra'); // 2
get('abracadabra'); // 3
```

[![License](https://img.shields.io/github/license/trie-data-structure/uncompressed-trie.svg)](https://raw.githubusercontent.com/trie-data-structure/uncompressed-trie/main/LICENSE)
[![Version](https://img.shields.io/npm/v/@trie-data-structure/uncompressed-trie.svg)](https://www.npmjs.org/package/@trie-data-structure/uncompressed-trie)
[![Tests](https://img.shields.io/github/workflow/status/trie-data-structure/uncompressed-trie/ci:test?event=push&label=tests)](https://github.com/trie-data-structure/uncompressed-trie/actions/workflows/ci:test.yml?query=branch:main)
Expand Down
24 changes: 24 additions & 0 deletions test/src/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import test from 'ava';

import {range} from '@iterable-iterator/range';
import {map} from '@iterable-iterator/map';

import {Trie, ArrayNode} from '../../src/index.js';

test('README', (t) => {
// eslint-disable-next-line unicorn/prefer-code-point
const encode = (key) => map((i) => key.charCodeAt(i), range(key.length));
const degree = 256;
const trie = new Trie(new ArrayNode(degree));

const set = (key, value) => trie.set(encode(key), value);
const get = (key) => trie.get(encode(key));

set('abra', 1);
set('cadabra', 2);
set('abracadabra', 3);

t.is(get('abra'), 1);
t.is(get('cadabra'), 2);
t.is(get('abracadabra'), 3);
});

0 comments on commit 4b6d68d

Please sign in to comment.