File tree 9 files changed +231
-188
lines changed
9 files changed +231
-188
lines changed Original file line number Diff line number Diff line change
1
+ export default class Node {
2
+
3
+ constructor ( char ) {
4
+ this . char = char
5
+ this . lowerChar = char . toLowerCase ( ) ;
6
+ this . children = { } ;
7
+ this . count = 1 ;
8
+ this . priority = 0 ; // Binary weights
9
+ }
10
+
11
+ setPriority ( _ ) {
12
+ this . priority = _ ;
13
+ }
14
+
15
+ isWord ( ) {
16
+ return this . _isWord === true ;
17
+ }
18
+
19
+ setIsWord ( _ ) {
20
+ this . _isWord = ! ! _ ;
21
+ }
22
+
23
+ upCount ( ) {
24
+ this . count ++ ;
25
+ }
26
+
27
+ wordNodes ( ) {
28
+ const words = [ ] ; // ordered list of { node, string}
29
+ if ( this . isWord ( ) ) {
30
+ words . push ( {
31
+ s : this . char , // string
32
+ n : this , // node
33
+ } )
34
+ }
35
+ for ( const key of Object . keys ( this . children ) ) {
36
+ const childNodes = this . children [ key ] . wordNodes ( ) ;
37
+ for ( const d of childNodes ) {
38
+ words . push ( {
39
+ s : this . char + d . s ,
40
+ n : d . n ,
41
+ } ) ;
42
+ }
43
+ }
44
+ return words ;
45
+ }
46
+ }
Original file line number Diff line number Diff line change
1
+ import Node from './Node.js' ;
2
+
3
+ export default class Trie {
4
+
5
+ constructor ( ) {
6
+ this . root = new Node ( '' ) ;
7
+ this . size = 0 ;
8
+ }
9
+
10
+ addWords ( words ) {
11
+ words . forEach ( w => this . addWord ( w ) ) ;
12
+ }
13
+
14
+ addWord ( word ) {
15
+ let node = this . root ;
16
+ for ( const char of word ) {
17
+ if ( ! node . children [ char ] ) {
18
+ node . children [ char ] = new Node ( char ) ;
19
+ }
20
+ node = node . children [ char ] ;
21
+ }
22
+ node . setIsWord ( true ) ;
23
+ node . upCount ( ) ;
24
+ this . size ++ ;
25
+ }
26
+
27
+ markSpecial ( words ) {
28
+ if ( typeof words === 'string' ) {
29
+ words = [ words ] ;
30
+ }
31
+ words . forEach ( w => {
32
+ if ( ! this . get ( w ) ) {
33
+ this . addWord ( w ) ;
34
+ }
35
+ this . get ( w ) . setPriority ( 1 ) ;
36
+ } )
37
+ }
38
+
39
+ contains ( word ) {
40
+ const node = this . get ( word ) ;
41
+ return node && node . isWord ( ) ;
42
+ }
43
+
44
+ find ( prefix ) {
45
+ if ( ! prefix ) {
46
+ return '' ;
47
+ }
48
+ const node = this . get ( prefix ) ;
49
+ const words = node ? node . wordNodes ( ) : [ ] ;
50
+ prefix = prefix . substr ( 0 , prefix . length - 1 ) ;
51
+ return words . sort ( ( a , b ) => b . n . priority - a . n . priority ) . map ( w => prefix + w . s ) ;
52
+ }
53
+
54
+ get ( word ) {
55
+ let node = this . root ;
56
+ let lowerChar ;
57
+ for ( const char of word ) {
58
+ let lowerChar = char . toLowerCase ( ) ;
59
+ if ( node . children [ char ] ) {
60
+ node = node . children [ char ] ;
61
+ } else if ( node . children [ lowerChar ] ) {
62
+ node = node . children [ lowerChar ] ;
63
+ } else {
64
+ return ;
65
+ }
66
+ }
67
+ return node ;
68
+ }
69
+
70
+ size ( ) {
71
+ return this . size ;
72
+ }
73
+ }
Original file line number Diff line number Diff line change
1
+
2
+ export function getFirstParent ( el , selector ) {
3
+ if ( ! el . parentElement ) {
4
+ return false ;
5
+ } else if ( el . parentElement . matches ( selector ) ) {
6
+ return el . parentElement ;
7
+ }
8
+ return getFirstParent ( el . parentElement , selector ) ;
9
+ }
10
+
11
+ export function log ( ...args ) {
12
+ if ( true || window . logmeplease ) {
13
+ console . log ( ...args ) ;
14
+ }
15
+ }
16
+
17
+ export function time ( ) {
18
+ return ( new Date ( ) ) . getTime ( ) ;
19
+ }
20
+
21
+ export function totalOffset ( element ) {
22
+ const offset = { top : 0 , left : 0 } ;
23
+ while ( element ) {
24
+ offset . left += element . offsetLeft || 0 ;
25
+ offset . top += element . offsetTop || 0 ;
26
+ element = element . offsetParent ;
27
+ }
28
+ return offset ;
29
+ }
30
+
31
+ export function clean ( s ) {
32
+ return s . replace ( CLEAN_REGEX , ' ' ) ;
33
+ }
You can’t perform that action at this time.
0 commit comments