Skip to content

Instantly share code, notes, and snippets.

View jmaicaaan's full-sized avatar

JM Santos jmaicaaan

  • Philippines
View GitHub Profile
const person01 = {
name: 'JM'
};
const person02 = person01;
/**
* Mutating `person02` will cause effect also on `person01` because
* `person01` was assigned by reference not by value
*/
let y = 3;
/**
* The `sum` function is accessing the global variable `y` to compute for the sum
*/
const sum = (x) => x + y;
/**
* Here we are mutating the state of the variable `y` to `6`
* which means that the computation of `sum` will now be different
const sum = (x, y) => x + y;
/**
* We are sure that `sum(3, 3)` will *always* return `6`
* because the function `sum` is *pure*.
* No matter how many times we call it with the same input,
* it will produce the same output
*/
const six = sum(3, 3);
@jmaicaaan
jmaicaaan / block03.tsx
Last active May 30, 2021 05:16
Protected Route
import React, { useEffect } from 'react';
import {
BrowserRouter as Router,
Redirect,
Route,
Switch,
} from 'react-router-dom';
import './App.css';
@jmaicaaan
jmaicaaan / block02.tsx
Last active May 30, 2021 04:50
Protected Route
import { RouteProps } from 'react-router-dom';
import { GuardFunction } from '../types/guard';
import { tryCatchSync } from '../utils/tryCatch';
export type GuardFunctionArgs = RouteProps;
export type GuardFunction = (args: GuardFunctionArgs) => boolean;
type User = {
@jmaicaaan
jmaicaaan / block01.tsx
Last active May 30, 2021 02:17
Protected Route
import {
Route,
RouteProps,
matchPath,
} from 'react-router-dom';
import { GuardFunction } from '../types/guard';
export type ProtectedRouteProps = RouteProps & {
guards: GuardFunction[];
import {
format,
isAfter,
isSameDay,
} from 'date-fns';
import * as Yup from 'yup';
import { tryCatch } from '../../utils/tryCatch';
const validationSchema = Yup.object().shape({
import { format } from 'date-fns';
export const post = async (req, res) => {
const {
body: {
date: unformattedDate,
startTime: unformattedStartTime,
endTime: unformattedEndTime,
},
} = req;
@jmaicaaan
jmaicaaan / try-catch-snippet-03.js
Created March 31, 2021 13:25
[Tutorial] Functional try catch
const getData = (id) => {
if (!id) {
throw new Error('No id found');
}
return [1, 2, 3, 4, 5].filter(i => i % id);
};
const tryCatch = (tryer) => {
try {
@jmaicaaan
jmaicaaan / try-catch-snippet-02.js
Last active March 31, 2021 13:11
[Tutorial] Functional try catch
// mutable variable
const getData = (id) => {
if (!id) {
throw new Error('No id found');
}
return [1, 2, 3, 4, 5].filter(i => i % id);
};