@@ -7,51 +7,69 @@ export interface IPatcher {
77}
88
99export class SlickPatchParser {
10+
11+ static stringify ( operation : Operation ) : string {
12+ if ( operation . op == "add" ) {
13+ return "+ " + operation . path + " => " + JSON . stringify ( operation . value ) ;
14+ } else if ( operation . op == "remove" ) {
15+ return "- " + operation . path ;
16+ } else if ( operation . op == "replace" ) {
17+ return "= " + operation . path + " => " + JSON . stringify ( operation . value ) ;
18+ } else if ( operation . op == "copy" ) {
19+ return "+ " + operation . from + " => " + operation . path ;
20+ } else if ( operation . op == "move" ) {
21+ return "+ " + operation . from + " => " + operation . path ;
22+ } else if ( operation . op == "test" ) {
23+ return "+ " + operation . path + " => " + JSON . stringify ( operation . value ) ;
24+ }
25+ return "Unknown operation" ;
26+ }
27+
1028 parse ( sourcePatch : string ) : Operation [ ] {
1129 var result : Operation [ ] = [ ] ;
1230
13- XRegExp . forEach ( sourcePatch , XRegExp ( '^\\s*(?<op>\\+|-|=|&|>|\\?)\\s*(?<path>.*?)\\s*(=>\\s*(?<value>.*))?$' , 'gm' ) , ( match ) => {
31+ XRegExp . forEach ( sourcePatch , XRegExp ( '^\\s*(?<op>\\+|-|=|&|>|\\?)\\s*(?<path>.*?)\\s*(=>\\s*(?<value>.*))?$' , 'gm' ) , ( match ) => {
1432 var op = ( < any > match ) . op ;
15- if ( op == "+" ) {
33+ if ( op == "+" ) {
1634 result . push ( {
17- op : "add" ,
18- path : ( < any > match ) . path ,
19- value : JSON . parse ( ( < any > match ) . value )
35+ op : "add" ,
36+ path : ( < any > match ) . path ,
37+ value : JSON . parse ( ( < any > match ) . value )
2038 } ) ;
21- } else if ( op == "-" ) {
39+ } else if ( op == "-" ) {
2240 result . push ( {
23- op : "remove" ,
24- path : ( < any > match ) . path
41+ op : "remove" ,
42+ path : ( < any > match ) . path
2543 } ) ;
26- } else if ( op == "=" ) {
44+ } else if ( op == "=" ) {
2745 result . push ( {
28- op : "replace" ,
29- path : ( < any > match ) . path ,
30- value : JSON . parse ( ( < any > match ) . value )
46+ op : "replace" ,
47+ path : ( < any > match ) . path ,
48+ value : JSON . parse ( ( < any > match ) . value )
3149 } ) ;
32- } else if ( op == "&" ) {
50+ } else if ( op == "&" ) {
3351 result . push ( {
34- op : "copy" ,
35- path : ( < any > match ) . value ,
36- from : ( < any > match ) . path
52+ op : "copy" ,
53+ path : ( < any > match ) . value ,
54+ from : ( < any > match ) . path
3755 } ) ;
38- } else if ( op == ">" ) {
56+ } else if ( op == ">" ) {
3957 result . push ( {
40- op : "move" ,
41- path : ( < any > match ) . value ,
42- from : ( < any > match ) . path
58+ op : "move" ,
59+ path : ( < any > match ) . value ,
60+ from : ( < any > match ) . path
4361 } ) ;
44- } else if ( op == "?" ) {
62+ } else if ( op == "?" ) {
4563 result . push ( {
46- op : "test" ,
47- path : ( < any > match ) . path ,
48- value : JSON . parse ( ( < any > match ) . value )
64+ op : "test" ,
65+ path : ( < any > match ) . path ,
66+ value : JSON . parse ( ( < any > match ) . value )
4967 } ) ;
5068 } else {
5169 throw new Error ( "operator " + op + " is no supported." ) ;
5270 }
5371 } ) ;
5472
55- return result ;
73+ return result ;
5674 }
5775}
0 commit comments