More unit tests and a refinement of the regexp to remove JSONP encapsulation

This commit is contained in:
jos 2015-02-28 17:03:40 +01:00
parent b94b87c66b
commit 75372fa3d7
2 changed files with 3 additions and 1 deletions

View File

@ -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];
}

View File

@ -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({});');