Skip to content

Instantly share code, notes, and snippets.

export const formatDate = (date) => {
if (!date) throw new Error('No date') // If no date, throw an error
const dateObj = new Date(date)
if (isToday(dateObj)) return 'Today' // If the date is today, return 'Today'
if (isYesterday(dateObj)) return 'Yesterday' // If the date is yesterday, return 'Yesterday'
if (!isThisYear(dateObj)) return format(dateObj, 'MMMM d, yyyy') // If the date is not in this year, return date with year
return format(dateObj, 'MMMM d') // If no matching the above, return date with month and date
}
if (kind === 'Persian' || kind === 'Maine' || kind === 'British Shorthair') {
// do something ...
}
const CATS_TYPE = ['Persian', 'Maine', 'British Shorthair']
if (CATS_TYPE.includes(kind)) {
// do something ...
}
if (user && user.addressInfo) {
let zipcode
if (user.addressInfo.zipcode) {
zipcode = user.addressInfo.zipcode
} else {
zipcode = ''
}
// do something
}
const zipcode = user?.addressInfo?.zipcode || ''
const todos = [
{
code: 'code',
name: 'name',
list: [
{
name: 'todo name',
},
{
name: 'todo name',
const list = [];
todos.forEach(t => {
t.todo2.forEach(t2 => {
t2.todo3.forEach(t3 => {
t3.list.forEach(l => {
list.push({
todo3Name: l.name
})
})
})
const list = todos
.reduce((acc, t) => [...acc, ...t.todo2], [])
.reduce((acc, t2) => [...acc, ...t2.todo3], [])
.reduce((acc, t3) => [...acc, ...t3.list], [])
.map(l => ({ todo3Name: l.name }))
const updateUser = async (user) => {
try {
await axios.post('/users', user)
await axios.post('/user/profile', user.profile)
const email = new Email()
await email.send(user.email, 'User has been updated successfully')
const logger = new Logger()
const updateUser = async (user) => {
try {
await axios.post('/users', user)
} catch (e) {
// handling error
}
}