@@ -3,11 +3,9 @@ import dictionary from './dictionary/sortedYopta.json';
33
44function escapeRegExp ( str : string ) {
55 str = str . replace ( / [ - \/ \\ ^ $ * + ? . ( ) | [ \] { } ] / g, '\\$&' ) ;
6-
76 if ( / ^ \w + $ / . test ( str ) ) {
87 str = '\\b' + str + '\\b' ;
98 }
10-
119 return str ;
1210}
1311
@@ -23,14 +21,11 @@ function yoptReplaceAll(str: string, search: string, replacement: string) {
2321function iterateText ( text : string , to : 'js' | 'ys' = 'ys' ) {
2422 const langCol = to === 'ys' ? 1 : 0 ;
2523 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+ }
3228 ) ;
33-
3429 return text ;
3530}
3631
@@ -47,25 +42,46 @@ export function compile(text: string, lang: 'js' | 'ys' = 'ys'): string {
4742 interface Literals {
4843 [ key : string ] : string ;
4944 }
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+
5055 const commentRegExp = / ( (?: \/ \* (?: [ ^ * ] | (?: \* + [ ^ * \/ ] ) ) * \* + \/ ) | (?: \/ \/ .* ) ) / g;
5156 const tmpToken = 'ys_' + new Date ( ) . getTime ( ) + '_' ;
57+
5258 const rStringLiterals : Literals = { } ;
5359 text = text . replace (
5460 / \" (?: \\ .| [ ^ \" \\ ] ) * \" | \' (?: \\ .| [ ^ \' \\ ] ) * \' / g,
55- function ( val , pos ) {
61+ ( val , pos ) => {
5662 const needKey = tmpToken + pos ;
5763 rStringLiterals [ needKey ] = val ;
5864 return needKey ;
5965 }
6066 ) ;
67+
6168 const commentsArray = text . match ( commentRegExp ) || [ ] ;
69+
6270 text = iterateText ( text , lang ) ;
71+
6372 // comeback comments
6473 text = text . replace ( commentRegExp , ( ) => commentsArray . shift ( ) || '' ) ;
74+
6575 // comeback strings
6676 for ( const key in rStringLiterals ) {
6777 text = text . replace ( key , rStringLiterals [ key ] ) ;
6878 }
79+
80+ // comeback jsx
81+ for ( const key in rJsxTextLiterals ) {
82+ text = text . replace ( key , rJsxTextLiterals [ key ] ) ;
83+ }
84+
6985 return text ;
7086}
7187
0 commit comments