Skip to content

Commit

Permalink
Merge pull request #15 from wavify/fix-escape
Browse files Browse the repository at this point in the history
Fix escape/unescape functions according to the RFC
  • Loading branch information
Pierre-Élie Fauché @ Sunrise committed Mar 6, 2015
2 parents a82f203 + a4b3f01 commit 6181c3b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
8 changes: 6 additions & 2 deletions lib/vobject/helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@

exports.escape = function(str) {
str = str || '';
return str.replace(/\n/g, '\\n').replace(/;/g, '\\;').replace(/,/g, '\\,').replace(/"/g, '\\"');
return str.replace(/[\\\n;,]/g, function(match) {
return '\\' + (match === '\n' ? 'n' : match);
});
};

exports.unescape = function(str) {
return str.replace(/\\n/g, '\n').replace(/\\;/g, ';').replace(/\\,/g, ',').replace(/\\"/g, '"');
return str.replace(/\\[\\nN;,]/g, function(match) {
return (match === '\\n' || match === '\\N') ? '\n' : match.substr(1);
});
};
16 changes: 8 additions & 8 deletions test/vobject/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ describe('lib/vobject/helpers.js', function() {
assert.equal(helpers.escape(str), 'this\\,that');
});

it('should escape double quote', function() {
var str = 'this"that';
assert.equal(helpers.escape(str), 'this\\"that');
it('should escape backslash', function() {
var str = 'this\\that';
assert.equal(helpers.escape(str), 'this\\\\that');
});
});

describe('unescape', function() {
it('should unescape newline', function() {
var str = 'this\\nthat';
assert.equal(helpers.unescape(str), 'this\nthat');
var str = 'this\\nthat\\Nthose';
assert.equal(helpers.unescape(str), 'this\nthat\nthose');
});

it('should unescape semicolon', function() {
Expand All @@ -40,9 +40,9 @@ describe('lib/vobject/helpers.js', function() {
assert.equal(helpers.unescape(str), 'this,that');
});

it('should unescape double quote', function() {
var str = 'this\\"that';
assert.equal(helpers.unescape(str), 'this"that');
it('should unescape backslash', function() {
var str = 'this\\\\that';
assert.equal(helpers.unescape(str), 'this\\that');
});
});
});

0 comments on commit 6181c3b

Please sign in to comment.