@scrabble-solver/configs 2.13.0 → 2.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/build/locales/roRo.js +17 -14
  2. package/dictionaries/build/Dictionaries.d.ts +13 -0
  3. package/dictionaries/build/Dictionaries.js +46 -0
  4. package/dictionaries/build/constants/index.d.ts +3 -0
  5. package/dictionaries/build/constants/index.js +11 -0
  6. package/dictionaries/build/index.d.ts +3 -0
  7. package/dictionaries/build/index.js +23 -0
  8. package/dictionaries/build/lib/DiskCache.d.ts +11 -0
  9. package/dictionaries/build/lib/DiskCache.js +48 -0
  10. package/dictionaries/build/lib/LayeredCache.d.ts +13 -0
  11. package/dictionaries/build/lib/LayeredCache.js +55 -0
  12. package/dictionaries/build/lib/MemoryCache.d.ts +13 -0
  13. package/dictionaries/build/lib/MemoryCache.js +32 -0
  14. package/dictionaries/build/lib/createAsyncProxy.d.ts +3 -0
  15. package/dictionaries/build/lib/createAsyncProxy.js +18 -0
  16. package/dictionaries/build/lib/createCacheTimestampComparator.d.ts +5 -0
  17. package/dictionaries/build/lib/createCacheTimestampComparator.js +19 -0
  18. package/dictionaries/build/lib/downloadDictionary.d.ts +4 -0
  19. package/dictionaries/build/lib/downloadDictionary.js +16 -0
  20. package/dictionaries/build/lib/getDictionaryFilepath.d.ts +3 -0
  21. package/dictionaries/build/lib/getDictionaryFilepath.js +11 -0
  22. package/dictionaries/build/lib/index.d.ts +7 -0
  23. package/dictionaries/build/lib/index.js +20 -0
  24. package/dictionaries/build/types/Cache.d.ts +8 -0
  25. package/dictionaries/build/types/Cache.js +2 -0
  26. package/dictionaries/build/types/index.d.ts +1 -0
  27. package/dictionaries/build/types/index.js +2 -0
  28. package/dictionaries/node_modules/.package-lock.json +16 -0
  29. package/dictionaries/node_modules/@kamilmielnik/trie/LICENSE +21 -0
  30. package/dictionaries/node_modules/@kamilmielnik/trie/README.md +187 -0
  31. package/dictionaries/node_modules/@kamilmielnik/trie/dist/Trie.d.ts +99 -0
  32. package/dictionaries/node_modules/@kamilmielnik/trie/dist/Trie.js +118 -0
  33. package/dictionaries/node_modules/@kamilmielnik/trie/dist/constants.d.ts +8 -0
  34. package/dictionaries/node_modules/@kamilmielnik/trie/dist/constants.js +11 -0
  35. package/dictionaries/node_modules/@kamilmielnik/trie/dist/index.d.ts +5 -0
  36. package/dictionaries/node_modules/@kamilmielnik/trie/dist/index.js +24 -0
  37. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/add.d.ts +10 -0
  38. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/add.js +22 -0
  39. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/deserialize.d.ts +11 -0
  40. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/deserialize.js +49 -0
  41. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/find.d.ts +10 -0
  42. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/find.js +21 -0
  43. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/fromArray.d.ts +9 -0
  44. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/fromArray.js +18 -0
  45. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/has.d.ts +10 -0
  46. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/has.js +18 -0
  47. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/hasPrefix.d.ts +12 -0
  48. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/hasPrefix.js +20 -0
  49. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/index.d.ts +11 -0
  50. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/index.js +28 -0
  51. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/nodeKeyComparator.d.ts +10 -0
  52. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/nodeKeyComparator.js +20 -0
  53. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/remove.d.ts +10 -0
  54. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/remove.js +32 -0
  55. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/serialize.d.ts +17 -0
  56. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/serialize.js +34 -0
  57. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/toArray.d.ts +10 -0
  58. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/toArray.js +22 -0
  59. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/traverse.d.ts +10 -0
  60. package/dictionaries/node_modules/@kamilmielnik/trie/dist/lib/traverse.js +53 -0
  61. package/dictionaries/node_modules/@kamilmielnik/trie/dist/types.d.ts +30 -0
  62. package/dictionaries/node_modules/@kamilmielnik/trie/dist/types.js +2 -0
  63. package/dictionaries/node_modules/@kamilmielnik/trie/package.json +69 -0
  64. package/package.json +4 -4
  65. package/src/locales/roRo.ts +17 -14
@@ -0,0 +1,187 @@
1
+ # @kamilmielnik/trie
2
+
3
+ ![Version](https://img.shields.io/github/package-json/v/kamilmielnik/trie)
4
+ ![License](https://img.shields.io/npm/l/@kamilmielnik/trie)
5
+ ![Node version](https://img.shields.io/node/v/@kamilmielnik/trie)
6
+ ![Dependencies](https://img.shields.io/librariesio/github/kamilmielnik/trie)
7
+ ![Vulnerabilities](https://img.shields.io/snyk/vulnerabilities/github/kamilmielnik/trie)
8
+ ![Test](https://github.com/kamilmielnik/trie/workflows/Test/badge.svg)
9
+ ![Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)
10
+ ![Prettier](https://github.com/kamilmielnik/trie/workflows/Prettier/badge.svg)
11
+
12
+ [Trie](https://en.wikipedia.org/wiki/Trie) data structure implementation in TypeScript.
13
+ Highly performant. No dependencies. Built for a [Scrabble Solver](https://github.com/kamilmielnik/scrabble-solver).
14
+
15
+ # Table of contents
16
+
17
+ - [Installation](#installation)
18
+ - [API](#api)
19
+ - [Object-oriented API](#object-oriented-api)
20
+ - [Functional API](#functional-api)
21
+ - [Examples](#examples)
22
+ - [Load dictionary from file](#load-dictionary-from-file)
23
+ - [Serialize `Node` to a file](#serialize-node-to-a-file)
24
+ - [Load serialized `Node` from a file](#load-serialized-node-from-a-file)
25
+ - [Find all words with given prefix](#find-all-words-with-given-prefix)
26
+ - [Serialization and compression](#serialization-and-compression)
27
+ - [Performance](#performance)
28
+
29
+ # Installation
30
+
31
+ ## npm
32
+
33
+ ```Shell
34
+ npm install @kamilmielnik/trie --save
35
+ ```
36
+
37
+ ## yarn
38
+
39
+ ```Shell
40
+ yarn add @kamilmielnik/trie
41
+ ```
42
+
43
+ # [API](docs/README.md)
44
+
45
+ See full [API Docs](docs/README.md) - generated by [typedoc](http://typedoc.org/).
46
+
47
+ Good to know:
48
+
49
+ - all objects are mutable
50
+ - every class, interface, type, constant and function is exported
51
+ - all exports are named (there is no default export)
52
+
53
+ There are 2 ways to use the API.
54
+
55
+ ## [Object-oriented API](docs/README.md#classes)
56
+
57
+ Create [`Trie`](docs/classes/trie.md) instance and use its' methods.
58
+
59
+ ### Example
60
+
61
+ ```ts
62
+ import { Trie } from '@kamilmielnik/trie';
63
+
64
+ const trie = new Trie();
65
+ trie.add('master');
66
+ trie.add('mask');
67
+ console.log(trie.hasPrefix('man')); // false
68
+ console.log(trie.hasPrefix('mas')); // true
69
+ console.log(trie.has('mas')); // false
70
+ console.log(trie.remove('mas')); // false
71
+ console.log(trie.has('master')); // true
72
+ console.log(trie.serialize()); // "(m(a(s(t(e(r))k))))"
73
+ console.log(trie.remove('master')); // true
74
+ console.log(trie.serialize()); // "(m(a(s(k))))"
75
+ ```
76
+
77
+ ## [Functional API](docs/README.md#functions)
78
+
79
+ Manipulate existing instances of [`Node`](docs/interfaces/node.md) with [functions](docs/README.md#functions).
80
+
81
+ ### Example
82
+
83
+ The following example works identically to the object-oriented example above.
84
+
85
+ ```ts
86
+ import { add, has, hasPrefix, Node, remove, serialize } from '@kamilmielnik/trie';
87
+
88
+ const root: Node = {};
89
+
90
+ add(root, 'master');
91
+ add(root, 'mask');
92
+ console.log(hasPrefix(root, 'man')); // false
93
+ console.log(hasPrefix(root, 'mas')); // true
94
+ console.log(has(root, 'mas')); // false
95
+ console.log(remove(root, 'mas')); // false
96
+ console.log(has(root, 'master')); // true
97
+ console.log(serialize(root)); // "(m(a(s(t(e(r))k))))"
98
+ console.log(remove(root, 'master')); // true
99
+ console.log(serialize(root)); // "(m(a(s(k))))"
100
+ ```
101
+
102
+ # Examples
103
+
104
+ - [Load dictionary from file](#load-dictionary-from-file)
105
+ - [Serialize `Node` to a file](#serialize-node-to-a-file)
106
+ - [Load serialized `Node` from a file](#load-serialized-node-from-a-file)
107
+ - [Find all words with given prefix](#find-all-words-with-given-prefix)
108
+
109
+ ## Load dictionary from file
110
+
111
+ ```ts
112
+ import { fromArray, Node } from '@kamilmielnik/trie';
113
+ import fs from 'fs';
114
+
115
+ const fromFile = (filepath: string): Node => {
116
+ const file = fs.readFileSync(filepath, 'utf-8');
117
+ // Assuming file contains 1 word per line
118
+ const words = file.split('\n').filter(Boolean);
119
+ const node = fromArray(words);
120
+ return node;
121
+ };
122
+ ```
123
+
124
+ ## Serialize [`Node`](docs/interfaces/node.md) to a file
125
+
126
+ ```ts
127
+ import { Trie } from '@kamilmielnik/trie';
128
+ import fs from 'fs';
129
+
130
+ const toFile = (filepath: string, trie: Trie): void => {
131
+ const serialized = trie.serialize();
132
+ fs.writeFileSync(filepath, serialized);
133
+ };
134
+ ```
135
+
136
+ ## Load serialized [`Node`](docs/interfaces/node.md) from a file
137
+
138
+ ```ts
139
+ import { deserialize, Node } from '@kamilmielnik/trie';
140
+ import fs from 'fs';
141
+
142
+ const fromFile = (filepath: string): Node => {
143
+ const serialized = fs.readFileSync(filepath, 'utf-8');
144
+ const node = deserialize(serialized);
145
+ return node;
146
+ };
147
+ ```
148
+
149
+ ## Find all words with given prefix
150
+
151
+ ```ts
152
+ import { find, Node, toArray } from '@kamilmielnik/trie';
153
+
154
+ const findWordsWithPrefix = (node: Node, prefix: string): string[] => {
155
+ const prefixNode = find(node, prefix) || {};
156
+ const descendants = toArray(prefixNode, { prefix, sort: true, wordsOnly: true });
157
+ const words = descendants.map(({ prefix: word }) => word);
158
+ return words;
159
+ };
160
+ ```
161
+
162
+ # Serialization and compression
163
+
164
+ This package can be used to efficiently [serialize](docs/README.md#serialize) and compress dictionaries.
165
+ It reaches 54.79 [compression ratio](https://en.wikipedia.org/wiki/Data_compression_ratio) (98.17% space saving) for Polish dictionary when combined with [7-Zip](https://en.wikipedia.org/wiki/7z) at ultra compression level.
166
+
167
+ | Language | 🇺🇸 en-US | 🇬🇧 en-GB | 🇵🇱 pl-PL |
168
+ | ------------------------------------------------------------------- | ----------------------------------------------------------------------- | --------------------------------------------------------------------------- | ----------------------------------------- |
169
+ | Name | [TWL06](https://en.wikipedia.org/wiki/NASPA_Word_List) | [SOWPODS](https://en.wikipedia.org/wiki/Collins_Scrabble_Words) | [SJP.PL](https://sjp.pl/slownik/dp.phtml) |
170
+ | Source | [Download](https://www.wordgamedictionary.com/twl06/download/twl06.txt) | [Download](https://www.wordgamedictionary.com/sowpods/download/sowpods.txt) | [Download](https://sjp.pl/slownik/growy/) |
171
+ | Words count | 178,691 | 267,753 | 3,045,878 |
172
+ | File size [B] | 1,941,856 | 2,974,773 | 42,838,508 |
173
+ | File size (serialized) [B] | (-29.75%) 1,364,242 | (-31.57%) 2,035,697 | (-56.33%) 18,705,990 |
174
+ | File size ([7z](https://en.wikipedia.org/wiki/7z)) [B] | (-80.46%) 379,483 | (-81.04%) 563,913 | (-87.58%) 5,320,397 |
175
+ | File size (serialized + [7z](https://en.wikipedia.org/wiki/7z)) [B] | (-89.94%) 195,299 | (-90.40%) 285,430 | (-98.17%) 781,875 |
176
+
177
+ # Performance
178
+
179
+ [`add`](docs/README.md#add), [`find`](docs/README.md#find), [`has`](docs/README.md#has), [`hasPrefix`](docs/README.md#hasPrefix), [`remove`](docs/README.md#remove) are very fast - logarithmic complexity (millions of operations per second).
180
+
181
+ ![image](https://user-images.githubusercontent.com/6830683/100113381-d5b63580-2ea2-11eb-8ed6-0e03515b5d9a.png)
182
+
183
+ ----
184
+
185
+ [`deserialize`](docs/README.md#deserialize), [`fromArray`](docs/README.md#fromArray), [`serialize`](docs/README.md#serialize), [`toArray`](docs/README.md#toArray) are slow - linear complexity (few operations per second).
186
+
187
+ ![image](https://user-images.githubusercontent.com/6830683/100113526-fe3e2f80-2ea2-11eb-9b1e-22ae954e297a.png)
@@ -0,0 +1,99 @@
1
+ import type { Descendant, Node, TraverseCallback, TraverseOptions } from './types';
2
+ /**
3
+ * A class representing the {@link https://en.wikipedia.org/wiki/Trie | Trie data structure}.
4
+ */
5
+ declare class Trie {
6
+ /**
7
+ * Creates a new {@link Trie} by deserializing given string.
8
+ *
9
+ * The inverse of {@link Trie.serialize | serialize}.
10
+ *
11
+ * @param serialized - String with serialized data.
12
+ * @returns {@link Trie} representing deserialized data.
13
+ */
14
+ static deserialize(serialized: string): Trie;
15
+ /**
16
+ * Creates a new {@link Trie} based on array of words.
17
+ *
18
+ * @params words - array of words to put in the {@link Trie}.
19
+ * @returns New {@link Trie} containing all given words.
20
+ */
21
+ static fromArray(words: string[]): Trie;
22
+ /**
23
+ * Represents the root {@link Node} of the {@link Trie}.
24
+ * It's not a copy. Mutate at your own risk.
25
+ */
26
+ readonly root: Node;
27
+ /**
28
+ * Creates a new {@link Trie} with optionally given root {@link Node}.
29
+ *
30
+ * @param root - Root {@link Node} of the {@link Trie} to be created.
31
+ */
32
+ constructor(root?: Node);
33
+ /**
34
+ * Inserts given word into the {@link Trie}.
35
+ *
36
+ * @param word - Word to be inserted into the {@link Trie}.
37
+ * @returns {@link Node} representing the end of the added word.
38
+ */
39
+ add(word: string): Node;
40
+ /**
41
+ * Finds {@link Node} representing given prefix in the {@link Trie}.
42
+ *
43
+ * @param prefix - Prefix to be found.
44
+ * @returns {@link Node} representing a given prefix, undefined if there is no such node.
45
+ */
46
+ find(prefix: string): Node | undefined;
47
+ /**
48
+ * Tells you whether given word is in the {@link Trie}.
49
+ *
50
+ * @param word - Word to be found.
51
+ * @returns true if given word is in the {@link Trie}, false otherwise.
52
+ */
53
+ has(word: string): boolean;
54
+ /**
55
+ * Tells you whether there are any words with given prefix in the {@link Trie}.
56
+ *
57
+ * See: https://en.wikipedia.org/wiki/String_operations#Prefixes
58
+ *
59
+ * @param prefix - Prefix to be found.
60
+ * @returns true if there are any words with given prefix in the {@link Trie}, false otherwise.
61
+ */
62
+ hasPrefix(prefix: string): boolean;
63
+ /**
64
+ * Removes given word from the {@link Trie} if it exists.
65
+ *
66
+ * @param word - Word to be removed.
67
+ * @returns true if the word was removed, false otherwise.
68
+ */
69
+ remove(word: string): boolean;
70
+ /**
71
+ * Converts the {@link Trie} into a string.
72
+ *
73
+ * The inverse of {@link Trie.deserialize | deserialize}.
74
+ *
75
+ * It serializes {@link https://sjp.pl/slownik/growy/ | 42.8 MB Polish dictionary} down to 18.7 MB (-56%).
76
+ *
77
+ * It serializes {@link https://www.wordgamedictionary.com/twl06/download/twl06.txt | 1.9 MB English (US) dictionary} down to 1.4 MB (-30%).
78
+ *
79
+ * It serializes {@link https://www.wordgamedictionary.com/sowpods/download/sowpods.txt | 3 MB English (GB) dictionary} down to 2 MB (-32%).
80
+ *
81
+ * @returns String with serialized data.
82
+ */
83
+ serialize(): string;
84
+ /**
85
+ * Finds all {@link Descendant | descendants} of the {@link Trie | Trie's} root and returns them as an array.
86
+ *
87
+ * @param options - See {@link TraverseOptions}.
88
+ * @returns An array of {@link Descendant | descendants}.
89
+ */
90
+ toArray(options?: TraverseOptions): Descendant[];
91
+ /**
92
+ * Visits every descendant {@link Node} of the {@link Trie} and calls a callback.
93
+ *
94
+ * @param callback - Callback that will be called for each visited {@link Node}. Return true from callback to stop traversing.
95
+ * @param options - See {@link TraverseOptions}.
96
+ */
97
+ traverse(callback: TraverseCallback, options?: TraverseOptions): void;
98
+ }
99
+ export default Trie;
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const lib_1 = require("./lib");
4
+ /**
5
+ * A class representing the {@link https://en.wikipedia.org/wiki/Trie | Trie data structure}.
6
+ */
7
+ class Trie {
8
+ /**
9
+ * Creates a new {@link Trie} with optionally given root {@link Node}.
10
+ *
11
+ * @param root - Root {@link Node} of the {@link Trie} to be created.
12
+ */
13
+ constructor(root = {}) {
14
+ this.root = root;
15
+ }
16
+ /**
17
+ * Creates a new {@link Trie} by deserializing given string.
18
+ *
19
+ * The inverse of {@link Trie.serialize | serialize}.
20
+ *
21
+ * @param serialized - String with serialized data.
22
+ * @returns {@link Trie} representing deserialized data.
23
+ */
24
+ static deserialize(serialized) {
25
+ return new Trie((0, lib_1.deserialize)(serialized));
26
+ }
27
+ /**
28
+ * Creates a new {@link Trie} based on array of words.
29
+ *
30
+ * @params words - array of words to put in the {@link Trie}.
31
+ * @returns New {@link Trie} containing all given words.
32
+ */
33
+ static fromArray(words) {
34
+ return new Trie((0, lib_1.fromArray)(words));
35
+ }
36
+ /**
37
+ * Inserts given word into the {@link Trie}.
38
+ *
39
+ * @param word - Word to be inserted into the {@link Trie}.
40
+ * @returns {@link Node} representing the end of the added word.
41
+ */
42
+ add(word) {
43
+ return (0, lib_1.add)(this.root, word);
44
+ }
45
+ /**
46
+ * Finds {@link Node} representing given prefix in the {@link Trie}.
47
+ *
48
+ * @param prefix - Prefix to be found.
49
+ * @returns {@link Node} representing a given prefix, undefined if there is no such node.
50
+ */
51
+ find(prefix) {
52
+ return (0, lib_1.find)(this.root, prefix);
53
+ }
54
+ /**
55
+ * Tells you whether given word is in the {@link Trie}.
56
+ *
57
+ * @param word - Word to be found.
58
+ * @returns true if given word is in the {@link Trie}, false otherwise.
59
+ */
60
+ has(word) {
61
+ return (0, lib_1.has)(this.root, word);
62
+ }
63
+ /**
64
+ * Tells you whether there are any words with given prefix in the {@link Trie}.
65
+ *
66
+ * See: https://en.wikipedia.org/wiki/String_operations#Prefixes
67
+ *
68
+ * @param prefix - Prefix to be found.
69
+ * @returns true if there are any words with given prefix in the {@link Trie}, false otherwise.
70
+ */
71
+ hasPrefix(prefix) {
72
+ return (0, lib_1.hasPrefix)(this.root, prefix);
73
+ }
74
+ /**
75
+ * Removes given word from the {@link Trie} if it exists.
76
+ *
77
+ * @param word - Word to be removed.
78
+ * @returns true if the word was removed, false otherwise.
79
+ */
80
+ remove(word) {
81
+ return (0, lib_1.remove)(this.root, word);
82
+ }
83
+ /**
84
+ * Converts the {@link Trie} into a string.
85
+ *
86
+ * The inverse of {@link Trie.deserialize | deserialize}.
87
+ *
88
+ * It serializes {@link https://sjp.pl/slownik/growy/ | 42.8 MB Polish dictionary} down to 18.7 MB (-56%).
89
+ *
90
+ * It serializes {@link https://www.wordgamedictionary.com/twl06/download/twl06.txt | 1.9 MB English (US) dictionary} down to 1.4 MB (-30%).
91
+ *
92
+ * It serializes {@link https://www.wordgamedictionary.com/sowpods/download/sowpods.txt | 3 MB English (GB) dictionary} down to 2 MB (-32%).
93
+ *
94
+ * @returns String with serialized data.
95
+ */
96
+ serialize() {
97
+ return (0, lib_1.serialize)(this.root);
98
+ }
99
+ /**
100
+ * Finds all {@link Descendant | descendants} of the {@link Trie | Trie's} root and returns them as an array.
101
+ *
102
+ * @param options - See {@link TraverseOptions}.
103
+ * @returns An array of {@link Descendant | descendants}.
104
+ */
105
+ toArray(options) {
106
+ return (0, lib_1.toArray)(this.root, options);
107
+ }
108
+ /**
109
+ * Visits every descendant {@link Node} of the {@link Trie} and calls a callback.
110
+ *
111
+ * @param callback - Callback that will be called for each visited {@link Node}. Return true from callback to stop traversing.
112
+ * @param options - See {@link TraverseOptions}.
113
+ */
114
+ traverse(callback, options) {
115
+ return (0, lib_1.traverse)(this.root, callback, options);
116
+ }
117
+ }
118
+ exports.default = Trie;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Represents end of a node in serialized format.
3
+ */
4
+ export declare const CLOSE_PARENS = ")";
5
+ /**
6
+ * Represents start of a node in serialized format.
7
+ */
8
+ export declare const OPEN_PARENS = "(";
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OPEN_PARENS = exports.CLOSE_PARENS = void 0;
4
+ /**
5
+ * Represents end of a node in serialized format.
6
+ */
7
+ exports.CLOSE_PARENS = ')';
8
+ /**
9
+ * Represents start of a node in serialized format.
10
+ */
11
+ exports.OPEN_PARENS = '(';
@@ -0,0 +1,5 @@
1
+ import { default as Trie } from './Trie';
2
+ export { Trie };
3
+ export * from './constants';
4
+ export * from './lib';
5
+ export type { Descendant, Node, TraverseCallback, TraverseOptions } from './types';
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.Trie = void 0;
21
+ const Trie_1 = __importDefault(require("./Trie"));
22
+ Object.defineProperty(exports, "Trie", { enumerable: true, get: function () { return Trie_1.default; } });
23
+ __exportStar(require("./constants"), exports);
24
+ __exportStar(require("./lib"), exports);
@@ -0,0 +1,10 @@
1
+ import type { Node } from '../types';
2
+ /**
3
+ * Inserts given `word` into given `node`.
4
+ *
5
+ * @param node - {@link Node} to insert the `word` to.
6
+ * @param word - Word to be inserted into `node`.
7
+ * @returns {@link Node} representing the end of the added word.
8
+ */
9
+ declare const add: (node: Node, word: string) => Node;
10
+ export default add;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Inserts given `word` into given `node`.
5
+ *
6
+ * @param node - {@link Node} to insert the `word` to.
7
+ * @param word - Word to be inserted into `node`.
8
+ * @returns {@link Node} representing the end of the added word.
9
+ */
10
+ const add = (node, word) => {
11
+ let currentNode = node;
12
+ for (let index = 0; index < word.length; ++index) {
13
+ const character = word[index];
14
+ if (!currentNode[character]) {
15
+ currentNode[character] = {};
16
+ }
17
+ currentNode = currentNode[character];
18
+ }
19
+ currentNode.wordEnd = true;
20
+ return currentNode;
21
+ };
22
+ exports.default = add;
@@ -0,0 +1,11 @@
1
+ import type { Node } from '../types';
2
+ /**
3
+ * Creates a new {@link Node} by deserializing given string.
4
+ *
5
+ * The inverse of {@link serialize}.
6
+ *
7
+ * @param serialized - String with value returned by {@link serialize}.
8
+ * @returns Instance of a root {@link Node} of deserialized string.
9
+ */
10
+ declare const deserialize: (serialized: string) => Node;
11
+ export default deserialize;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("../constants");
4
+ /**
5
+ * Creates a new {@link Node} by deserializing given string.
6
+ *
7
+ * The inverse of {@link serialize}.
8
+ *
9
+ * @param serialized - String with value returned by {@link serialize}.
10
+ * @returns Instance of a root {@link Node} of deserialized string.
11
+ */
12
+ const deserialize = (serialized) => {
13
+ const stack = [];
14
+ let node = {};
15
+ let i = 1;
16
+ while (i < serialized.length - 1) {
17
+ const character = serialized[i];
18
+ const nextCharacter = serialized[i + 1];
19
+ ++i;
20
+ if (character === constants_1.CLOSE_PARENS) {
21
+ const nextNode = stack.pop();
22
+ if (!nextNode) {
23
+ throw new Error(`Syntax error: misplaced "${constants_1.CLOSE_PARENS}"`);
24
+ }
25
+ node = nextNode;
26
+ }
27
+ else if (nextCharacter === constants_1.CLOSE_PARENS) {
28
+ node[character] = { wordEnd: true };
29
+ const nextNode = stack.pop();
30
+ if (!nextNode) {
31
+ throw new Error(`Syntax error: misplaced "${constants_1.CLOSE_PARENS}"`);
32
+ }
33
+ node = nextNode;
34
+ ++i;
35
+ }
36
+ else if (nextCharacter === constants_1.OPEN_PARENS) {
37
+ stack.push(node);
38
+ const newNode = node[character] || {};
39
+ node[character] = newNode;
40
+ node = newNode;
41
+ ++i;
42
+ }
43
+ else {
44
+ node[character] = { wordEnd: true };
45
+ }
46
+ }
47
+ return node;
48
+ };
49
+ exports.default = deserialize;
@@ -0,0 +1,10 @@
1
+ import type { Node } from '../types';
2
+ /**
3
+ * Finds {@link Node} representing given prefix in given {@link Node}.
4
+ *
5
+ * @param node - {@link Node} to look for prefix in.
6
+ * @param prefix - Prefix to be found.
7
+ * @returns {@link Node} representing a given prefix, undefined if there is no such node.
8
+ */
9
+ declare const find: (node: Node, prefix: string) => Node | undefined;
10
+ export default find;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Finds {@link Node} representing given prefix in given {@link Node}.
5
+ *
6
+ * @param node - {@link Node} to look for prefix in.
7
+ * @param prefix - Prefix to be found.
8
+ * @returns {@link Node} representing a given prefix, undefined if there is no such node.
9
+ */
10
+ const find = (node, prefix) => {
11
+ let currentNode = node;
12
+ for (let index = 0; index < prefix.length; ++index) {
13
+ const character = prefix[index];
14
+ if (!currentNode[character]) {
15
+ return undefined;
16
+ }
17
+ currentNode = currentNode[character];
18
+ }
19
+ return currentNode;
20
+ };
21
+ exports.default = find;
@@ -0,0 +1,9 @@
1
+ import type { Node } from '../types';
2
+ /**
3
+ * Creates a new {@link Node} based on array of words.
4
+ *
5
+ * @params words - array of words to put in the {@link Node}.
6
+ * @returns New {@link Node} containing all given words.
7
+ */
8
+ declare const fromArray: (words: string[]) => Node;
9
+ export default fromArray;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const add_1 = __importDefault(require("./add"));
7
+ /**
8
+ * Creates a new {@link Node} based on array of words.
9
+ *
10
+ * @params words - array of words to put in the {@link Node}.
11
+ * @returns New {@link Node} containing all given words.
12
+ */
13
+ const fromArray = (words) => {
14
+ const node = {};
15
+ words.forEach((word) => (0, add_1.default)(node, word));
16
+ return node;
17
+ };
18
+ exports.default = fromArray;
@@ -0,0 +1,10 @@
1
+ import type { Node } from '../types';
2
+ /**
3
+ * Tells you whether given word is in the {@link Node}.
4
+ *
5
+ * @param node - {@link Node} to look for word in.
6
+ * @param word - Word to be found.
7
+ * @returns true if given word is in the {@link Node}, false otherwise.
8
+ */
9
+ declare const has: (node: Node, word: string) => boolean;
10
+ export default has;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const find_1 = __importDefault(require("./find"));
7
+ /**
8
+ * Tells you whether given word is in the {@link Node}.
9
+ *
10
+ * @param node - {@link Node} to look for word in.
11
+ * @param word - Word to be found.
12
+ * @returns true if given word is in the {@link Node}, false otherwise.
13
+ */
14
+ const has = (node, word) => {
15
+ const foundNode = (0, find_1.default)(node, word);
16
+ return Boolean(foundNode && foundNode.wordEnd);
17
+ };
18
+ exports.default = has;
@@ -0,0 +1,12 @@
1
+ import type { Node } from '../types';
2
+ /**
3
+ * Tells you whether there are any words with given prefix in the {@link Node}.
4
+ *
5
+ * See: https://en.wikipedia.org/wiki/String_operations#Prefixes
6
+ *
7
+ * @param node - {@link Node} to look for prefix in.
8
+ * @param prefix - Prefix to be found.
9
+ * @returns true if there are any words with given prefix in the {@link Node}, false otherwise.
10
+ */
11
+ declare const hasPrefix: (node: Node, prefix: string) => boolean;
12
+ export default hasPrefix;