Created
March 25, 2024 06:44
-
-
Save kyawswarthwin/e413de198c192714881c09f5ac4455b6 to your computer and use it in GitHub Desktop.
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
import { fileURLToPath } from 'url'; | |
import path from 'path'; | |
const filename = (url) => fileURLToPath(url); | |
const dirname = (url) => path.dirname(filename(url)); | |
function handleResponse(response) { | |
const formatResponse = ({ ok, status, statusText }, data) => { | |
const responseData = { | |
success: ok, | |
status: status, | |
statusText: statusText, | |
data | |
}; | |
if (!response.ok) { | |
return Promise.reject(responseData); | |
} | |
return responseData; | |
}; | |
const contentType = response.headers.get('content-type'); | |
if (contentType && contentType.indexOf('application/json') !== -1) { | |
return response.json().then((data) => formatResponse(response, data)); | |
} else { | |
return response.text().then((data) => formatResponse(response, data)); | |
} | |
} | |
function median(arr) { | |
const mid = Math.floor(arr.length / 2), | |
nums = [...arr].sort((a, b) => a - b); | |
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2; | |
} | |
export { dirname, filename, handleResponse, median }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment