1+ module . exports = class Notation {
2+ constructor ( ) {
3+ this . config = {
4+ name :[ "သုည" , "တစ်" , "နှစ်" , "သုံး" , "လေး" , "ငါး" , "ခြောက်" , "ခုနစ်" , "ရှစ်" , "ကိုး" ] ,
5+ digit :[ "ဝ" , "၁" , "၂" , "၃" , "၄" , "၅" , "၆" , "၇" , "၈" , "၉" ] ,
6+ // 1:"ခု",
7+ tone :{ 1 :"" , 2 :"ဆယ်" , 3 :"ရာ" , 4 :"ထောင်" , 5 :"သောင်း" , 6 :"သိန်း" , 7 :"သန်း" , 8 :"ကုဋေ" } ,
8+ creaky :{ 2 :"ဆယ့်" , 3 :"ရာ့" , 4 :"ထောင့်" } ,
9+ conjunction :{ space :" " , comma :"၊ " , and :"နှင့်" , plus :"ပေါင်း" , over :"ကျော်" , times :"ကြိမ်" } ,
10+ task :[ 6 , 7 , 8 ]
11+ }
12+ }
13+ configName ( ) {
14+ return this . config . name ;
15+ }
16+ configDigit ( ) {
17+ return this . config . digit ;
18+ }
19+ configTone ( ) {
20+ return this . config . tone ;
21+ }
22+ configCreaky ( ) {
23+ return this . config . creaky ;
24+ }
25+ configConjunction ( ) {
26+ return this . config . conjunction ;
27+ }
28+ get ( query ) {
29+ let q = Math . floor ( Number ( query . toString ( ) . replace ( / , \s ? / g, "" ) ) ) . toString ( ) , t = 0 ;
30+ if ( isNaN ( q ) ) {
31+ q = this . getUni ( query ) , t = 1 ;
32+ if ( q < 1 || isNaN ( q ) ) return false ;
33+ }
34+ return this . getResult ( q , t ) ;
35+ }
36+ getUni ( query ) {
37+ let n = query . toString ( ) . split ( '' ) . map ( k => {
38+ return ( this . config . digit . indexOf ( k ) >= 0 ) ?this . config . digit . indexOf ( k ) :k ;
39+ } ) . filter ( function ( e ) { return e == 0 || e } ) . join ( '' ) ;
40+ return Math . floor ( Number ( n . replace ( / , \s ? / g, "" ) ) ) . toString ( ) ;
41+ }
42+ getResult ( q , t ) {
43+ let n = this . requestFormat ( q ) ;
44+ return {
45+ Numeric :( t ) ?n :this . requestDigit ( n ) ,
46+ Notation :this . config . task . map ( k => {
47+ return this . requestTask ( q , k ) ;
48+ } ) . filter ( function ( e ) { return e } )
49+ } ;
50+ }
51+ requestTask ( q , s ) {
52+ if ( q . length <= s ) {
53+ if ( this . config . task . length ) {
54+ this . config . task = [ ] ;
55+ return {
56+ // sense:this.requestCreakyTone(q)
57+ sense :this . requestWrittenTone ( q )
58+ }
59+ }
60+ return false ;
61+ }
62+ let raw = this . strSeparate ( q , s ) ;
63+ let rawCount = raw . length ;
64+ let rawEnd = raw . slice ( - 1 ) [ 0 ] ;
65+ let rawEndCount = rawEnd . length ;
66+
67+ let ruleExtract = rawEndCount ;
68+ let rulePrime = rawCount ;
69+ let ruleMax = Object . keys ( this . config . tone ) . length ;
70+ let rawSense = [ ] ;
71+
72+ // let Measure=this.config.tone[s];
73+ // let ConjunctionPlus=this.config.conjunction.plus;
74+ // let PrefixeTimes='';
75+ // let SuffixeMeasure='';
76+
77+ let examNotation = false ;
78+ let eachName = ( k ) => {
79+ if ( k && Number ( k ) ) {
80+ // let ke = this.strSeparate(kr,s);
81+ let ks = ( s * 2 ) - 2 ;
82+ let kr = k . substring ( 0 , ks ) ;
83+ let kp = this . requestPrime ( 1 , s ) ;
84+ let k1 = this . requestWrittenTone ( kr , 0 , s - 1 ) ;
85+ let k2 = this . requestCreakyTone ( kr , s - 1 , k1 . length ) ;
86+ if ( k2 ) {
87+ rawSense . push ( kp + k1 + ' ' + k2 ) ;
88+ } else {
89+ rawSense . push ( k1 ) ;
90+ }
91+ eachName ( k . substring ( ks , k . length ) ) ;
92+ }
93+ }
94+ if ( rawCount < 3 ) {
95+ if ( s == rawEndCount ) {
96+ ruleExtract = rawCount ;
97+ // prime, base, baseName, rest, restName, tail, tailName
98+ rawSense . push ( this . requestPrime ( rulePrime , s ) + this . requestCreakyTone ( q , 0 , ruleExtract ) ) ;
99+ rawSense . push ( this . requestCreakyTone ( q , rulePrime , s + 1 ) ) ;
100+ rawSense . push ( this . requestWrittenTone ( q , s + 1 , q . length ) ) ;
101+ examNotation = { test :1 } ;
102+ } else {
103+ if ( rawCount > rawEndCount ) {
104+ ruleExtract = rawCount ;
105+ } else {
106+ ruleExtract = rawEndCount + 1 ;
107+ }
108+ rulePrime = 1 ;
109+ rawSense . push ( this . requestPrime ( rulePrime , s , true ) + this . requestCreakyTone ( q , 0 , ruleExtract ) ) ;
110+ rawSense . push ( this . requestWrittenTone ( q , ruleExtract , q . length ) ) ;
111+ examNotation = { test :2 } ;
112+ }
113+ } else {
114+ ruleExtract = rawCount - 1 + rawEndCount ;
115+ rulePrime = rawCount - 1 ;
116+ if ( ruleExtract > ruleMax ) {
117+ ruleExtract = rawEndCount - 3 ;
118+ rulePrime = rawCount ;
119+ }
120+ rawSense . push ( this . requestPrime ( 2 , s ) + this . requestCreakyTone ( q , 0 , ruleExtract ) ) ;
121+ eachName ( q . substring ( ruleExtract , q . length ) ) ;
122+ examNotation = { test :2 , raw :raw } ;
123+ }
124+
125+ return {
126+ sense :this . createString ( rawSense ) ,
127+ // exam: examNotation
128+ } ;
129+ }
130+ requestObject ( str , callback ) {
131+ let strCount = str . length , e = [ ] ;
132+ // let testing = this.config.tone.reverse()
133+ // let strReverse = str.reverse(); index += to, ++index
134+ for ( let index = 0 ; index < strCount ; ++ index ) {
135+ let position = strCount - index ;
136+ let digit = str [ index ] , next = index + 1 ;
137+ let tone = this . config . tone . hasOwnProperty ( position ) ?this . config . tone [ position ] :'' ;
138+
139+ if ( digit > 0 ) e . push ( {
140+ name :this . config . name [ digit ] ,
141+ tone :callback ( position , next , tone )
142+ } ) ;
143+ }
144+ return e ;
145+ }
146+ requestCreakyTone ( num , from , to , pair = '' ) {
147+ let str = this . strIntersect ( num , from , to ) ;
148+ return this . requestObject ( str , ( position , next , tone ) => {
149+ if ( str . hasOwnProperty ( next ) && str [ next ] > 0 ) {
150+ return this . config . creaky . hasOwnProperty ( position ) ?this . config . creaky [ position ] :tone ;
151+ }
152+ return tone ;
153+ } ) . map ( ( k , index ) => {
154+ return k . name + k . tone ;
155+ } ) . join ( pair )
156+ }
157+ requestWrittenTone ( num , from , to , pair = '' ) {
158+ let str = this . strIntersect ( num , from , to ) ;
159+ return this . requestObject ( str , ( position , next , tone ) => {
160+ return tone ;
161+ } ) . map ( ( k , index , e ) => {
162+ if ( ( e . length - 2 ) == index ) {
163+ return k . name + k . tone + this . config . conjunction . and ;
164+ }
165+ return k . name + k . tone ;
166+ } ) . join ( pair ) ;
167+ }
168+ requestCreakyTail ( num , from , to ) {
169+ let str = this . strIntersect ( num , from , to ) ;
170+ return this . requestObject ( str , ( position , next , tone ) => {
171+ if ( str . hasOwnProperty ( next ) && this . config . creaky . hasOwnProperty ( position ) && str [ next ] > 0 ) {
172+ return { normal :tone , creaky :this . config . creaky [ position ] } ;
173+ }
174+ return { normal :tone }
175+ } ) . map ( ( k , index , e ) => {
176+ if ( ( e . length - 2 ) == index ) {
177+ return k . name + k . tone . hasOwnProperty ( 'creaky' ) ?k . name + k . tone . creaky :k . name + k . tone . normal ;
178+ }
179+ return k . name + k . tone . normal ;
180+ } ) . join ( ' ' ) ;
181+ }
182+ requestDigit ( n ) {
183+ return n . split ( '' ) . map ( k => {
184+ return this . config . digit . hasOwnProperty ( k ) ?this . config . digit [ k ] :k ;
185+ } ) . join ( '' ) ;
186+ }
187+ requestFormat ( n ) {
188+ return n . replace ( / ( \d ) (? = ( \d { 3 } ) + (? ! \d ) ) / g, "$1," ) ;
189+ }
190+ requestPrime ( l , k , s ) {
191+ let i = 0 , e = [ ] ;
192+ while ( i < l ) { e . push ( this . config . tone [ k ] ) ; i ++ ; }
193+ if ( s ) e . push ( '' ) ;
194+ return e . join ( this . config . conjunction . plus + this . config . conjunction . space ) ;
195+ }
196+ createString ( n ) {
197+ // return n.filter(function(e){return e}).join(this.config.conjunction.and+this.config.conjunction.space)
198+ let a = n . filter ( function ( e ) { return e } ) ;
199+ if ( a . length > 1 ) {
200+ return a . slice ( 0 , - 1 ) . join ( this . config . conjunction . comma ) + this . config . conjunction . and + this . config . conjunction . space + a . slice ( - 1 ) ;
201+ } else {
202+ return a . join ( this . config . conjunction . space ) ;
203+ }
204+ }
205+ // createString_Org(n) {
206+ // return n.filter(function(e){return e}).join(this.config.conjunction.and)
207+ // }
208+ // createJoin(a) {
209+ // if (a.length > 1){
210+ // return a.slice(0, -1).join(',')+' and '+a.slice(-1);
211+ // } else {
212+ // return a.join(',')
213+ // }
214+ // }
215+ strSeparate ( str , n ) {
216+ let e = new Array ;
217+ for ( let index = 0 ; index < str . length ; index += n ) e . push ( str . substr ( index , n ) ) ;
218+ return e ;
219+ }
220+ strIntersect ( str , from , to ) {
221+ if ( to ) {
222+ return str . substring ( from , to ) . split ( '' ) ;
223+ } else {
224+ return str . split ( '' ) ;
225+ }
226+ }
227+ }
0 commit comments