@gkucmierz/utils 1.25.0 → 1.27.0

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.
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.25.0",
3
+ "version": "1.27.0",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@gkucmierz/utils",
9
- "version": "1.25.0",
9
+ "version": "1.27.0",
10
10
  "license": "MIT",
11
11
  "devDependencies": {
12
12
  "husky": "^8.0.1",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.25.0",
3
+ "version": "1.27.0",
4
4
  "description": "Usefull functions for solving programming tasks",
5
5
  "main": "main.mjs",
6
6
  "scripts": {
@@ -0,0 +1,69 @@
1
+
2
+ import {
3
+ Trie,
4
+ } from '../src/Trie.mjs';
5
+
6
+ describe('Trie', () => {
7
+ it('check empty', () => {
8
+ const trie = Trie();
9
+ expect(trie.has('')).toBe(false);
10
+ expect(trie.has('abc')).toBe(false);
11
+ });
12
+
13
+ it('init data', () => {
14
+ const trie = Trie(['abc', '']);
15
+ expect(trie.has('')).toBe(true);
16
+ expect(trie.has('abc')).toBe(true);
17
+ });
18
+
19
+ it('add/readd data', () => {
20
+ const trie = Trie();
21
+ expect(trie.has('')).toBe(false);
22
+ expect(trie.add('')).toBe(true);
23
+ expect(trie.has('')).toBe(true);
24
+ expect(trie.add('')).toBe(false);
25
+ });
26
+
27
+ it('list data', () => {
28
+ const trie = Trie(['abc', '', 'abcdef', 'xyz']);
29
+ const abc = ['abc', 'abcdef'];
30
+ expect(trie.get('a')).toEqual(abc);
31
+ expect(trie.get('ab')).toEqual(abc);
32
+ expect(trie.get('abc')).toEqual(abc);
33
+ expect(trie.get('aa')).toEqual([]);
34
+ expect(trie.get('')).toEqual(['', 'abc', 'abcdef', 'xyz']);
35
+ });
36
+
37
+ it('remove node', () => {
38
+ const trie = Trie(['a', 'ab', 'abc']);
39
+
40
+ expect(trie.has('abc')).toEqual(true);
41
+ expect(trie.remove('abc')).toEqual(true);
42
+ expect(trie.remove('abc')).toEqual(false);
43
+ expect(trie.has('abc')).toEqual(false);
44
+
45
+ expect(trie.has('ab')).toEqual(true);
46
+ expect(trie.remove('ab')).toEqual(true);
47
+ expect(trie.remove('ab')).toEqual(false);
48
+ expect(trie.has('ab')).toEqual(false);
49
+
50
+ expect(trie.has('a')).toEqual(true);
51
+ expect(trie.remove('a')).toEqual(true);
52
+ expect(trie.remove('a')).toEqual(false);
53
+ expect(trie.has('a')).toEqual(false);
54
+ });
55
+
56
+ it('keep child node after parent removal', () => {
57
+ const trie = Trie(['a', 'abc']);
58
+
59
+ expect(trie.has('a')).toEqual(true);
60
+ expect(trie.remove('a')).toEqual(true);
61
+ expect(trie.remove('a')).toEqual(false);
62
+ expect(trie.has('a')).toEqual(false);
63
+
64
+ expect(trie.has('abc')).toEqual(true);
65
+ expect(trie.remove('abc')).toEqual(true);
66
+ expect(trie.remove('abc')).toEqual(false);
67
+ expect(trie.has('abc')).toEqual(false);
68
+ })
69
+ });
package/src/Trie.mjs ADDED
@@ -0,0 +1,78 @@
1
+
2
+ // stric = true - remove removes whole unused data structure
3
+ // stric = false - quick remove (data structure can grow fast)
4
+ export const Trie = (words = [], strict = true) => {
5
+ const HAS = 0;
6
+ const MAP = 1;
7
+ const data = [false, new Map()];
8
+ const add = word => {
9
+ let node = data;
10
+ for (let i = 0; i < word.length; ++i) {
11
+ const char = word[i];
12
+ if (!node[MAP]) node[MAP] = new Map();
13
+ const child = node[MAP].get(char) ?? [false];
14
+ node[MAP].set(char, child);
15
+ node = child;
16
+ }
17
+ if (node[HAS]) return false;
18
+ node[HAS] = true;
19
+ return true;
20
+ };
21
+ const listNodes = word => {
22
+ const nodes = [data];
23
+ for (let i = 0; i < word.length; ++i) {
24
+ const node = nodes.at(-1);
25
+ const char = word[i];
26
+ if (!node[MAP]?.has(char)) {
27
+ nodes.push([false]);
28
+ break;
29
+ }
30
+ nodes.push(node[MAP].get(char));
31
+ }
32
+ return nodes;
33
+ };
34
+ const findNode = word => {
35
+ return listNodes(word).at(-1);
36
+ };
37
+ const remove = word => {
38
+ const nodes = listNodes(word);
39
+ const rev = nodes.reverse();
40
+ const removed = rev.at(0)[HAS];
41
+ rev.at(0)[HAS] = false;
42
+ if (!strict) return removed;
43
+ for (let i = 0; i < rev.length; ++i) {
44
+ const node = rev[i];
45
+ const first = i === 0;
46
+ const noMap = !node[MAP];
47
+ const size1 = node[MAP]?.size <= 1;
48
+ if (first) {
49
+ if (node[MAP]) break;
50
+ } else {
51
+ if (node[MAP]?.size <= 1) {
52
+ delete node[MAP];
53
+ } else {
54
+ break;
55
+ }
56
+ }
57
+ if (node[HAS]) break;
58
+ }
59
+ return removed;
60
+ };
61
+ const has = word => findNode(word)[HAS];
62
+ const get = begin => {
63
+ const res = [];
64
+ const loop = (node, str = '') => {
65
+ if (!node) return;
66
+ if (node[HAS]) res.push(begin + str);
67
+ const map = node[MAP] || new Map();
68
+ [...map].map(([char, node]) => loop(node, str + char));
69
+ };
70
+ loop(findNode(begin));
71
+ return res;
72
+ };
73
+ words.map(word => add(word));
74
+ return {
75
+ add, has, get, remove,
76
+ getData: () => data,
77
+ };
78
+ };