-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Handle unsafe
structureClone
with functions
in matchers whil…
…e using workers
- Loading branch information
1 parent
a2a54a0
commit 627031c
Showing
3 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import {replaceFunctionsWithStringReferences} from '../helpers'; | ||
|
||
it('serialize functions inside the nested object', () => { | ||
const obj = { | ||
foo: () => {}, | ||
nested: { | ||
fn: function bar() { | ||
return 0; | ||
}, | ||
}, | ||
nestedArray: [{baz: function baz() {}}, () => {}], | ||
}; | ||
|
||
expect(replaceFunctionsWithStringReferences(obj)).toEqual({ | ||
foo: '[Function foo]', | ||
nested: { | ||
fn: '[Function bar]', | ||
}, | ||
nestedArray: [ | ||
{ | ||
baz: '[Function baz]', | ||
}, | ||
'[Function anonymous]', | ||
], | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
export const replaceFunctionsWithStringReferences = <T>(value: T): T => { | ||
if (typeof value === 'function') { | ||
return `[Function ${value.name || 'anonymous'}]` as unknown as T; | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
return value.map(replaceFunctionsWithStringReferences) as T; | ||
} | ||
|
||
const isObject = value !== null && typeof value === 'object'; | ||
if (isObject) { | ||
const oldObject = value as Record<string, unknown>; | ||
const newObject: Record<string, unknown> = {}; | ||
for (const key of Object.keys(value)) { | ||
newObject[key] = replaceFunctionsWithStringReferences(oldObject[key]); | ||
} | ||
|
||
return newObject as T; | ||
} | ||
|
||
return value; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters