11import * as React from "react" ;
22
33type ProcessResult = {
4- str : string ,
5- bindChain : string [ ]
4+ fragments : string [ ] ,
5+ binds : string [ ]
66}
77
88export type StringBinderT = {
@@ -13,8 +13,6 @@ class StringBinder {
1313
1414 public static readonly SpecialSymbol : string = '$' ;
1515
16- public static readonly Anchor : string = "%1s" ;
17-
1816 private static readonly TruePattern : RegExp = new RegExp ( / [ A - Z a - z ] / , 'gi' ) ;
1917
2018 private static _instance ?: StringBinder ;
@@ -34,22 +32,18 @@ class StringBinder {
3432 if ( ! ( bindKeys = Object . keys ( binds ) ) . length )
3533 return str ;
3634
37- const result : ProcessResult = this
38- . _process ( str ) ;
39-
40- const words = result . str
41- . split ( StringBinder . Anchor ) ;
35+ const result : ProcessResult = this . _process ( str ) ;
4236
43- if ( ! words . length )
37+ if ( ! result . fragments . length )
4438 return str ;
4539
4640 let idx : number = 0 ;
4741 const children : React . ReactNode [ ] = [ ] ;
48- words . forEach ( ( word , i ) => {
42+ result . fragments . forEach ( ( word , i ) => {
4943 children . push ( word ) ;
5044
51- if ( ( idx = bindKeys . indexOf ( result . bindChain [ i ] ) ) == - 1 )
52- children . push ( result . bindChain [ i ] ) ;
45+ if ( ( idx = bindKeys . indexOf ( result . binds [ i ] ) ) == - 1 )
46+ children . push ( result . binds [ i ] ) ;
5347
5448 else
5549 children . push ( binds [ bindKeys [ idx ] ] ) ;
@@ -59,19 +53,18 @@ class StringBinder {
5953 }
6054
6155 private _process ( str : string ) : ProcessResult {
62-
63- let buf = '' ;
64- let sub = '' ;
65- let keyIdx = - 1 ;
56+ let sub : string = '' ;
57+ let keyIdx : number = - 1 ;
6658
6759 const chain : string [ ] = [ ] ;
68- for ( let i = 0 , j = 0 , len = str . length ; i < len ; i ++ ) {
60+ const fragments : string [ ] = [ "" ] ;
61+ for ( let i = 0 , j = 0 , fid = 0 , len = str . length ; i < len ; i ++ ) {
6962
7063 if ( keyIdx === - 1 && str [ i ] === StringBinder . SpecialSymbol && ( i + 1 ) !== len )
7164 keyIdx = i ;
7265
7366 if ( keyIdx === - 1 ) {
74- buf += str [ i ] ;
67+ fragments [ fid ] += str [ i ] ;
7568 continue ;
7669 }
7770
@@ -87,20 +80,18 @@ class StringBinder {
8780 continue ;
8881
8982 if ( ! sub . length ) {
90- buf += str [ i ] ;
83+ fragments [ fid ] += str [ i ] ;
9184 continue ;
9285 }
9386
9487 chain . push ( sub ) ;
95- buf += StringBinder . Anchor ;
9688
9789 i += sub . length + 1 ;
90+ fid ++ ;
91+ fragments [ fid ] = ""
9892 }
9993
100- return {
101- str : buf ,
102- bindChain : chain ,
103- }
94+ return { fragments, binds : chain }
10495 }
10596}
10697
0 commit comments