Skip to content

Commit

Permalink
More unit tests and a refinement of the regexp to remove JSONP encaps…
Browse files Browse the repository at this point in the history
…ulation
  • Loading branch information
josdejong committed Feb 28, 2015
1 parent b94b87c commit 75372fa
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ exports.sanitize = function (jsString) {
//If JSON starts with a function (characters/digits/"_-"), remove this function.
//This is useful for "stripping" JSONP objects to become JSON
//For example: /* some comment */ function_12321321 ( [{"a":"b"}] ); => [{"a":"b"}]
var match = jsString.match(/^(\/\*(.|[\r\n])*?\*\/)?\s*[\da-zA-Z_$]+\s*\(([\s\S]*)\)\s*;?\s*$/);
var match = jsString.match(/^\s*(\/\*(.|[\r\n])*?\*\/)?\s*[\da-zA-Z_$]+\s*\(([\s\S]*)\)\s*;?\s*$/);
if (match) {
jsString = match[3];
}
Expand Down
2 changes: 2 additions & 0 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ describe('util', function () {
assert.equal(util.sanitize('/* foo bar */ callback_123 ({})'), '{}');
assert.equal(util.sanitize('/* foo bar */\ncallback_123({})'), '{}');
assert.equal(util.sanitize('/* foo bar */ callback_123 ( {} )'), ' {} ');
assert.equal(util.sanitize(' /* foo bar */ callback_123 ({}); '), '{}');
assert.equal(util.sanitize('\n/* foo\nbar */\ncallback_123 ({});\n\n'), '{}');

// non-matching
assert.equal(util.sanitize('callback abc({});'), 'callback abc({});');
Expand Down

0 comments on commit 75372fa

Please sign in to comment.