@@ -3,11 +3,9 @@ import dictionary from './dictionary/sortedYopta.json';
3
3
4
4
function escapeRegExp ( str : string ) {
5
5
str = str . replace ( / [ - \/ \\ ^ $ * + ? . ( ) | [ \] { } ] / g, '\\$&' ) ;
6
-
7
6
if ( / ^ \w + $ / . test ( str ) ) {
8
7
str = '\\b' + str + '\\b' ;
9
8
}
10
-
11
9
return str ;
12
10
}
13
11
@@ -23,14 +21,11 @@ function yoptReplaceAll(str: string, search: string, replacement: string) {
23
21
function iterateText ( text : string , to : 'js' | 'ys' = 'ys' ) {
24
22
const langCol = to === 'ys' ? 1 : 0 ;
25
23
const dick = dictionary ;
26
- dick . sort ( ( a , b ) => {
27
- const al = a [ langCol ] . length ;
28
- const bl = b [ langCol ] . length ;
29
- return bl - al ;
30
- } ) . forEach (
31
- ( pair ) => ( text = yoptReplaceAll ( text , pair [ langCol ] , pair [ + ! langCol ] ) )
24
+ dick . sort ( ( a , b ) => b [ langCol ] . length - a [ langCol ] . length ) . forEach (
25
+ ( pair ) => {
26
+ text = yoptReplaceAll ( text , pair [ langCol ] , pair [ + ! langCol ] ) ;
27
+ }
32
28
) ;
33
-
34
29
return text ;
35
30
}
36
31
@@ -47,25 +42,46 @@ export function compile(text: string, lang: 'js' | 'ys' = 'ys'): string {
47
42
interface Literals {
48
43
[ key : string ] : string ;
49
44
}
45
+ const rJsxTextLiterals : Literals = { } ;
46
+ text = text . replace (
47
+ / ( < [ A - Z a - z ] [ ^ > ] * > ) ( [ \s \S ] + ?) (? = < \/ [ A - Z a - z ] ) / g,
48
+ ( _ , openTag , content , offset ) => {
49
+ const key = tmpToken + 'jsx_' + offset ;
50
+ rJsxTextLiterals [ key ] = content ;
51
+ return openTag + key ;
52
+ }
53
+ ) ;
54
+
50
55
const commentRegExp = / ( (?: \/ \* (?: [ ^ * ] | (?: \* + [ ^ * \/ ] ) ) * \* + \/ ) | (?: \/ \/ .* ) ) / g;
51
56
const tmpToken = 'ys_' + new Date ( ) . getTime ( ) + '_' ;
57
+
52
58
const rStringLiterals : Literals = { } ;
53
59
text = text . replace (
54
60
/ \" (?: \\ .| [ ^ \" \\ ] ) * \" | \' (?: \\ .| [ ^ \' \\ ] ) * \' / g,
55
- function ( val , pos ) {
61
+ ( val , pos ) => {
56
62
const needKey = tmpToken + pos ;
57
63
rStringLiterals [ needKey ] = val ;
58
64
return needKey ;
59
65
}
60
66
) ;
67
+
61
68
const commentsArray = text . match ( commentRegExp ) || [ ] ;
69
+
62
70
text = iterateText ( text , lang ) ;
71
+
63
72
// comeback comments
64
73
text = text . replace ( commentRegExp , ( ) => commentsArray . shift ( ) || '' ) ;
74
+
65
75
// comeback strings
66
76
for ( const key in rStringLiterals ) {
67
77
text = text . replace ( key , rStringLiterals [ key ] ) ;
68
78
}
79
+
80
+ // comeback jsx
81
+ for ( const key in rJsxTextLiterals ) {
82
+ text = text . replace ( key , rJsxTextLiterals [ key ] ) ;
83
+ }
84
+
69
85
return text ;
70
86
}
71
87
0 commit comments