Last active
January 18, 2022 17:06
-
-
Save CezaryDanielNowak/c2d398758564374f2837679a10309997 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* Some JS objects like InputDeviceInfo or GeolocationCoordinates are impossible to clone easly. | |
* `Object.keys(obj)` returns `{}` | |
* `JSON.stringify(obj)` returns `{}` | |
* | |
* with following functions you're able to clone object properties to the simple object. | |
*/ | |
function cloneGeolocationCoordinates(instance) { | |
return Object.keys(instance.constructor.prototype).reduce((acc, key) => { | |
acc[key] = instance[key]; | |
return acc; | |
}, {}); | |
} | |
function cloneInputDeviceInfo(instance) { | |
const result = Object.keys(MediaDeviceInfo.prototype).reduce((acc, key) => { | |
acc[key] = instance[key]; | |
return acc; | |
}, {}); | |
delete result.toJSON; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment