serialize-javascript
Advanced tools
Comparing version 1.5.0 to 1.6.0
36
index.js
@@ -14,4 +14,7 @@ /* | ||
var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g; | ||
var IS_PURE_FUNCTION = /function.*?\(/; | ||
var UNSAFE_CHARS_REGEXP = /[<>\/\u2028\u2029]/g; | ||
var RESERVED_SYMBOLS = ['*', 'async']; | ||
// Mapping of unsafe HTML and invalid JavaScript line terminator chars to their | ||
@@ -72,2 +75,28 @@ // Unicode char counterparts which are safe to use in JavaScript strings. | ||
function serializeFunc(fn) { | ||
var serializedFn = fn.toString(); | ||
if (IS_NATIVE_CODE_REGEXP.test(serializedFn)) { | ||
throw new TypeError('Serializing native function: ' + fn.name); | ||
} | ||
// pure functions, example: {key: function() {}} | ||
if(IS_PURE_FUNCTION.test(serializedFn)) { | ||
return serializedFn; | ||
} | ||
var argsStartsAt = serializedFn.indexOf('('); | ||
var def = serializedFn.substr(0, argsStartsAt).trim().split(' ').filter(val => val.length > 0); | ||
var nonReservedSymbols = def.filter(val => RESERVED_SYMBOLS.indexOf(val) === -1); | ||
// enhanced literal objects, example: {key() {}} | ||
if(nonReservedSymbols.length > 0) { | ||
return (def.indexOf('async') > -1 ? 'async ' : '') + 'function' | ||
+ (def.join('').indexOf('*') > -1 ? '*' : '') | ||
+ serializedFn.substr(argsStartsAt); | ||
} | ||
// arrow functions | ||
return serializedFn; | ||
} | ||
var str; | ||
@@ -113,10 +142,5 @@ | ||
var fn = functions[valueIndex]; | ||
var serializedFn = fn.toString(); | ||
if (IS_NATIVE_CODE_REGEXP.test(serializedFn)) { | ||
throw new TypeError('Serializing native function: ' + fn.name); | ||
} | ||
return serializedFn; | ||
return serializeFunc(fn); | ||
}); | ||
} |
{ | ||
"name": "serialize-javascript", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Serialize JavaScript to a superset of JSON that includes regular expressions and functions.", | ||
@@ -31,4 +31,4 @@ "main": "index.js", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.4.2" | ||
"mocha": "^5.2.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
12339
114
0