52 lines
2.1 KiB
TypeScript
52 lines
2.1 KiB
TypeScript
export const compareObjects = (
|
|
obj1: object | null | undefined,
|
|
obj2: object | null | undefined
|
|
): boolean => {
|
|
if (Array.isArray(obj1) && Array.isArray(obj2)) {
|
|
const obj1Copy = [...obj1].sort()
|
|
const obj2Copy = [...obj2].sort()
|
|
|
|
return JSON.stringify(obj1Copy) === JSON.stringify(obj2Copy)
|
|
}
|
|
|
|
return JSON.stringify(obj1) === JSON.stringify(obj2)
|
|
}
|
|
|
|
// Function to check if the system is online by making a network request
|
|
export const isOnline = async () => {
|
|
// First, check the navigator's online status
|
|
if (!navigator.onLine) return false // If navigator reports offline, return false
|
|
|
|
/**
|
|
* If navigator.onLine is true, it can be false positive.
|
|
* In other words, navigator.onLine being true does not necessarily mean that there is a working internet connection.
|
|
* Most implementations seem to only be checking if there's a connected network adapter.
|
|
* There are a number of circumstances where that's the case, but the internet is still unreachable. For example:
|
|
* The user is only connected to an internal network
|
|
* The user is inside a virtual machine, and the virtual network adapter is connected, but the host system is offline
|
|
* The user uses a VPN which has installed a virtual network adapter that is always connected
|
|
*
|
|
* To overcome the above problem we'll have to make a http request
|
|
*/
|
|
|
|
try {
|
|
// Define a URL to check the online status
|
|
const url = 'https://www.google.com'
|
|
|
|
// Make a HEAD request to the URL with 'no-cors' mode
|
|
// This mode is used to handle opaque responses which do not expose their content
|
|
const response = await fetch(url, { method: 'HEAD', mode: 'no-cors' })
|
|
|
|
// Check if the response is OK or if the response type is 'opaque'
|
|
// An 'opaque' response type means that the request succeeded but the response content is not available
|
|
if (response.ok || response.type === 'opaque') {
|
|
return true // If the request is successful, return true
|
|
} else {
|
|
return false // If the request fails, return false
|
|
}
|
|
} catch (error) {
|
|
// Catch any errors that occur during the fetch request
|
|
return false // If an error occurs, return false
|
|
}
|
|
}
|