Skip to content

Instantly share code, notes, and snippets.

@rix1
Last active October 27, 2023 08:35
Show Gist options
  • Save rix1/601d4fe53943f848fa03d58b69048117 to your computer and use it in GitHub Desktop.
Save rix1/601d4fe53943f848fa03d58b69048117 to your computer and use it in GitHub Desktop.
Code understanding. Read through and think out loud: What does this code do? How would you change it? Feel free to use your editor.
export const request = ({ method, url, content }) => {
if (content && method === 'GET') {
throw new Error('GET method with content is not supported');
}
if (
!appGlobalsService.get('NODE_ENV') === 'development' &&
typeof navigator !== 'undefined' &&
!!navigator &&
navigator.onLine === false
) {
log.info('Trying to fetch resource, but no internet connection detected');
return Promise.reject(new Error('offline'));
}
let requestUrl;
const isAbsoluteUrl = /^https?:/.test(url);
if (isAbsoluteUrl) {
requestUrl = url;
} else {
requestUrl = `${appGlobalsService.get('OTOVOAPI_URL')}${url}`;
}
const hasJsonPayload =
content && typeof content !== 'string' && !(content instanceof FormData);
const headers = {
Accept: 'application/json',
...(hasJsonPayload ? { 'Content-Type': 'application/json' } : {})
};
let requestData = {
headers,
method
};
if (content) {
const payload = hasJsonPayload ? JSON.stringify(content) : content;
requestData.body = payload;
}
try {
return doRequest({
requestData,
requestUrl
});
} catch (error) {
log.error(`API: Error requesting ${url}`, error);
return { json: null, error };
}
};
const pairs = [
"A gentleman",
"Astronomer",
"Conversation",
"Dirty room",
"Dormitory",
"Elegant man",
"Funeral",
"Listen",
"Moon starer",
"Real fun",
"School master",
"Silent",
"The classroom",
"The eyes",
"They see",
"Voices rant on",
];
/**
* An anagram is a play on words created by rearranging the letters of the original word to make a new word or phrase. You can disregard spaces.
*
* Task: The above list contains 8 pairs of anagrams. The task is to find all pairs. E.g:
*
* Dormitory <-> Dirty room
* Astronomer <-> Moon starer
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment