From 4b6d68df3d88933eb7c6f0653e02a560a92efe6a Mon Sep 17 00:00:00 2001 From: make-github-pseudonymous-again <5165674+make-github-pseudonymous-again@users.noreply.github.com> Date: Sun, 19 Dec 2021 19:41:03 +0100 Subject: [PATCH] :books: docs(README): Add simple example. --- README.md | 21 +++++++++++++++++++++ test/src/example.js | 24 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 test/src/example.js diff --git a/README.md b/README.md index c690963..5b581b2 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/test/src/example.js b/test/src/example.js new file mode 100644 index 0000000..e6f0020 --- /dev/null +++ b/test/src/example.js @@ -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); +});