File tree Expand file tree Collapse file tree 2 files changed +70
-0
lines changed Expand file tree Collapse file tree 2 files changed +70
-0
lines changed Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ var slashRe = new RegExp ( '/' , 'g' ) ;
4+ var escapedSlashRe = new RegExp ( '~1' , 'g' ) ;
5+ var tildeRe = / ~ / g;
6+ var escapedTildeRe = / ~ 0 / g;
7+
8+ var Path = {
9+ escape : function ( str ) {
10+ if ( typeof ( str ) !== 'string' ) {
11+ throw 'param str (' + str + ') is not a string' ;
12+ }
13+
14+ return str . replace ( tildeRe , '~0' ) . replace ( slashRe , '~1' ) ;
15+ } ,
16+
17+ unescape : function ( str ) {
18+ if ( typeof ( str ) == 'string' ) {
19+ return str . replace ( escapedSlashRe , '/' ) . replace ( escapedTildeRe , '~' ) ;
20+ }
21+ else {
22+ return str ;
23+ }
24+ } ,
25+ concat : function ( path , key ) {
26+ return path + '/' + key ;
27+ }
28+ } ;
29+
30+ module . exports = Path ;
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ var assert = require ( 'assert' ) ;
4+ var path = require ( '../src/path' ) ;
5+
6+ describe ( 'Path' , function ( ) {
7+ it ( 'escape / in string' , function ( ) {
8+ var str = 'prop/prop' ;
9+ var expected = 'prop~1prop' ;
10+
11+ var result = path . escape ( str ) ;
12+
13+ assert . strictEqual ( result , expected ) ;
14+ } ) ;
15+
16+ it ( 'unescape / in string' , function ( ) {
17+ var str = 'prop~1prop' ;
18+ var expected = 'prop/prop' ;
19+
20+ var result = path . unescape ( str ) ;
21+
22+ assert . strictEqual ( result , expected ) ;
23+ } ) ;
24+
25+ it ( 'reverts to original string' , function ( ) {
26+ var str = 'prop/prop' ;
27+
28+ var result = path . unescape ( path . escape ( str ) ) ;
29+
30+ assert . strictEqual ( result , str ) ;
31+ } ) ;
32+
33+ it ( 'returns same string when escaping is not necessary' , function ( ) {
34+ var str = 'normalstring' ;
35+
36+ var result = path . escape ( str ) ;
37+
38+ assert . strictEqual ( result , str ) ;
39+ } ) ;
40+ } ) ;
You can’t perform that action at this time.
0 commit comments