Skip to content

Commit ce739df

Browse files
committed
refactor: fix serialization of native functions like number, string
The issue is that serialize-javascript throws a TypeError when trying to serialize native functions (like `Number`, `String`, `Array`, etc.). The fix should detect native built-in constructors and serialize them by their name (e.g., `Number` instead of throwing an error). Affected files: index.js Signed-off-by: hieuit095 <139037144+hieuit095@users.noreply.github.com>
1 parent df3f1c1 commit ce739df

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ module.exports = function serialize(obj, options) {
159159
function serializeFunc(fn, options) {
160160
var serializedFn = fn.toString();
161161
if (IS_NATIVE_CODE_REGEXP.test(serializedFn)) {
162+
if (fn.name) {
163+
return fn.name;
164+
}
162165
throw new TypeError('Serializing native function: ' + fn.name);
163166
}
164167

0 commit comments

Comments
 (0)