Skip to content

Instantly share code, notes, and snippets.

View DoctorDerek's full-sized avatar
☀️
https://linkedin.com/in/derek-austin Read my blog https://DoctorDerek.medium.com

Dr. Derek Austin DoctorDerek

☀️
https://linkedin.com/in/derek-austin Read my blog https://DoctorDerek.medium.com
View GitHub Profile
@DoctorDerek
DoctorDerek / yarn.sh
Created October 12, 2022 15:59
The right way to configure ESLint with Prettier https://medium.com/p/52f278b2f47f
yarn add --dev eslint-config-prettier
module.exports = {
...
extends: [
'plugin:prettier/recommended',
],
rules: {
'prettier/prettier': 'error',
}
}
@DoctorDerek
DoctorDerek / getChartOptionsObj.spec.js
Created September 8, 2022 15:38
Why You Should Always Pass Objects as Function Parameters in JavaScript https://medium.com/p/7fb7c5833dc6
// @/__tests__/utils/getChartOptionsObj.spec.js
describe("@/utils/getChartOptionsObj", () => {
const chartData = [1, 2, 3]
const defaultChart = getDefaultChartObj({ chartData })
it("passes along defaultChart only without extraOptions", () => {
const output = getChartOptionsObj({ chartData })
expect(output).toBe(defaultChart)
})
@DoctorDerek
DoctorDerek / getChartOptionsObj.js
Created September 8, 2022 15:36
Why You Should Always Pass Objects as Function Parameters in JavaScript https://medium.com/p/7fb7c5833dc6
// @/utils/getChartOptionsObj.js
export default function getChartOptionsObj({
chartData,
maximum,
limitOne,
limitTwo,
percent,
extraOptions,
}) {
const defaultChart = getDefaultChartObj({
@DoctorDerek
DoctorDerek / getChartOptions.spec.js
Created September 8, 2022 15:35
Why You Should Always Pass Objects as Function Parameters in JavaScript https://medium.com/p/7fb7c5833dc6
// @/__tests__/utils/getChartOptions.spec.js
describe("@/utils/getChartOptions", () => {
const chartData = [1, 2, 3]
const defaultChart = getDefaultChart(chartData)
it("passes along defaultChart only without extraOptions", () => {
const output = getChartOptions(chartData)
expect(output).toBe(defaultChart)
})
@DoctorDerek
DoctorDerek / getChartOptions.js
Created September 8, 2022 15:33
Why You Should Always Pass Objects as Function Parameters in JavaScript https://medium.com/p/7fb7c5833dc6
// @/utils/getChartOptions.js
export default function getChartOptions(
chartData,
maximum,
limitOne,
limitTwo,
percent,
extraOptions
) {
const defaultChart = getDefaultChart(
// We'll query the Rick and Morty API using a GET request:
const URL = "https://rickandmortyapi.com/api/character/"
const fetchCharacter = (id) => {
const httpRequest = new XMLHttpRequest()
httpRequest.open("GET", `${URL}${id}`)
httpRequest.send()
return httpRequest.responseText
}
const fetchJerry = () => fetchCharacter(5) // Jerry has id 5.
const result = fetchJerry() // The result is always undefined.
@DoctorDerek
DoctorDerek / How To Return the Response From an Asynchronous Function in JavaScript.js
Last active August 13, 2022 16:15
How To Return the Response From an Asynchronous Function in JavaScript https://medium.com/p/ecfcacef6138
// We'll query the Rick and Morty API using a GET request:
const URL = "https://rickandmortyapi.com/api/character/"
const fetchCharacter = (id) => {
const httpRequest = new XMLHttpRequest()
// When the request is loaded, call the callback function:
httpRequest.onload = function () {
callback(httpRequest.responseText)
}
httpRequest.open("GET", `${URL}${id}`)
httpRequest.send()
// We'll query the Rick and Morty API using a GET request:
const URL = "https://rickandmortyapi.com/api/character/"
const fetchCharacter = async (id) => {
const response = await fetch(`${URL}${id}`)
return await response.json()
}
// Any function with an await keyword must be labeled async:
const fetchJerry = async () => await fetchCharacter(5)
const result = await fetchJerry() // We await the fetch call.
const { name, status } = result ? result : {}
// We'll query the Rick and Morty API using a GET request:
const URL = "https://rickandmortyapi.com/api/character/"
const fetchCharacter = (id) => {
return fetch(`${URL}${id}`).then((response) => response.json())
}
const fetchJerry = () => fetchCharacter(5)
fetchJerry().then((result) => {
const { name, status } = result ? result : {}
console.log(`${name} is ${status}`) // Jerry Smith is Alive
})