Skip to content

Commit

Permalink
added proper string escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
Gagnant committed Jan 21, 2017
1 parent 451cdff commit 123a7ab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion JSTP.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = 'JSTP'
s.version = '0.1.9.1'
s.version = '0.1.9.2'
s.license = { :type => "MIT" }

s.homepage = 'https://github.com/metarhia/JSTP'
Expand Down
28 changes: 22 additions & 6 deletions JSTP/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ common.extend = function (object, extension) {
return object;
};

common.escape = function (string) {

var escapes = {
'\\':'\\\\',
'\'':'\\\'',
'\n':'\\n',
'\r':'\\r',

'\u2028':'\\u2028',
'\u2029':'\\u2029'
};

return string.replace(/['\\\n\r\u2028\u2029]/g, function (character) {
return escapes[character];
})
};

common.serializer = function (types) {

function serialize(object) {
Expand All @@ -25,31 +42,30 @@ common.serializer = function (types) {

if (object instanceof Array) type = 'array';
else if (object instanceof Date ) type = 'date';
else if (object === null ) type = 'undefined';
else if (object === null ) type = 'null';
else type = typeof object;

return serialize.types[type](object);
}

serialize.types = common.extend ({

null: function (arg) { return 'null'; },
undefined: function (arg) { return 'undefined'; },
boolean: function (arg) { return String(arg); },
number: function (arg) { return String(arg); },
string: function (arg) { return '\'' + arg.replace(/'/g, '\\\'') + '\''; },
string: function (arg) { return '\'' + common.escape(arg) + '\''; },
array: function (arg) { return '[' + arg.map(serialize).join(',') + ']'; },

object: function(arg) {

var keys = Object.keys(arg);
var array = [];

keys.forEach(function(key, index) {

keys.forEach(function(key) {
var representation = serialize(arg[key]);

if (representation !== 'undefined') {
array.push(key + ':' + representation);
array.push('\'' + key + '\':' + serialize(arg[key]));
}
});

Expand Down

0 comments on commit 123a7ab

Please sign in to comment.