Skip to content

Instantly share code, notes, and snippets.

View flacial's full-sized avatar
What's happening?

Faisal A. flacial

What's happening?
View GitHub Profile
@flacial
flacial / nextjs-with-notion-sdk.md
Created August 26, 2023 19:38
Notion-sdk-js with Next.js app router

Issue

Next.js extends the native fetch Web API to allow you to configure the caching and revalidating behavior for each fetch request on the server. React extends fetch to automatically memoize fetch requests while rendering a React component tree. However, the Notion SDK Client utilizes node-fetch by default, which lacks the advanced features found in Next.js's fetch and subsequently, does not cache requests in server-side logic (page or route).

Solution

The official documentation does not state that the Client method accepts a fetch property that supersedes the default fetch function it uses. However, the method actually accepts it. To resolve this issue, you can simply pass fetch to the fetch property a

@flacial
flacial / aws-cdk-apigateway-with-vpc-ec2.md
Last active March 4, 2023 15:34
CDK — API Gateawy with VCP Integration to EC2

Overview

The following document goes through how to write an CDK stack that creates an EC2 instance and integrate it with an API Gateway through a VPC link and create a CI/CD pipeline with CodePipeline, CodeDeploy, and CodeCommit.

Steps

  1. Create an EC2 instance
    1. Create a VPC instance
    2. Create a key pair for SSH access
    3. Create an instance by specifying its subnets, type, and image
@flacial
flacial / premutationsInAstring.js
Last active November 17, 2022 14:41
Sliding Window(hard): Premutations in a string
/*
Desc: Given a string and a pattern, find out if the string contains any permutation of the pattern.
Input: str=string, pattern=string
Output: boolean
Steps:
1. We'll iterate through the array
2. Once the window has 3 elements, we'll check if what's in the sliding window is equal to the pattern
@flacial
flacial / longestSubstringWithSameLetter.js
Created November 16, 2022 18:47
Sliding Window: longest substring with same letters
/*
Steps:
1. We'll iterate through the array
2. We'll create a hashmap to remember the occurence for each char
3. We'll also remember the most repeated char in the sliding window
4. If we removed the most repeated char from the sliding window and the left chars to replace with the most repeated char is more than K, we'll shrink the window
5. We'll remember the length of the sliding window as the one with the longest substring with the same letter
*/
@flacial
flacial / longestSubstringDistinctCharacter.js
Created November 13, 2022 13:31
Sliding Window: longest substring with distinct characters
const longestSubstringWithDistinctCharacters = str => {
let windowStart = 0;
let longestSubstring = 0;
const hashMap = {};
// Expand the window
for (let windowEnd = 0; windowEnd < str.length; windowEnd++) {
const rightChar = str[windowEnd];
// if the map already contains the 'rightChar', shrink the window from the beginning
@flacial
flacial / findAveragesOfSubarrays.js
Created November 11, 2022 18:39
Sliding Window: find averages of subarrays
function find_averages_of_subarrays(K, arr) {
const result = []
let windowSum = 0
let windowStart = 0
for (let windowEnd = 0; windowEnd < arr.length; windowEnd++) {
// Push the last element back, slide new element as the last one
windowSum += arr[windowEnd]
// We reached a window with 5 elements
@flacial
flacial / typeguard.md
Created August 5, 2022 02:25
TypeScript: type guard

type guard is a function that takes a type and returns a boolean. TypeScript uses it to determine if the element type can be narrowed down.

It's needed when we try to filter our data and end up returning a narrowed type. In the following case, we want filter to filter the array to only include elements that aren't a string.

interface User {
  email: string;
  userId: number;
  name: string;
}
@flacial
flacial / diff-variable-declaration-keywords.md
Last active October 5, 2021 12:29
Let, const, and var in (hopefully) laymans terms

We can begin by knowing what they share in common, storing data. Imagine data as boxes.

Brief definitions for a general idea:

var: used to store a box that will probably change later (mutable).
let: similar to var but a bit different (mutable).
const: used to store a box that won't change later (immutable).

Block, Function, and Global scopes:

@flacial
flacial / urlShortener.js
Created August 18, 2021 13:30
URL Shortener - ExpressJS/Postgresql
/*
Steps:
0. Choose a method to save the shortUrl and originalUrl (Database or file)
1. Create an endpoint (GET "/pathName/:shortUrlParameter") to validate the shortUrl and redirect
the user to the corresponding originalUrl
2. Create an endpoint (POST "/pathName") to create new shorten links
*/