Skip to content

Instantly share code, notes, and snippets.

View lauslim12's full-sized avatar
🌀
Available

Nicholas lauslim12

🌀
Available
View GitHub Profile
@lauslim12
lauslim12 / use-naive-fetch.ts
Last active February 2, 2024 14:09
Naive data fetching hook that can possibly satisfy a lot of straightforward use-cases.
import { useCallback, useEffect, useMemo, useState } from "react";
/**
* State utilizes TypeScript's type system to our advantage. Having a discriminated union
* would make sense here because each state could have their own unique attributes. Being
* declarative here would make it more readable and easier to understand, compared to using
* imperative statements such as creating multiple states (`data`, `isLoading`, `error`) and
* then having them to rely on each other, which also doesn't work that well because of the
* TypeScript's type system being unable to understand dependant variables without making it
* to be a discriminated union.
@lauslim12
lauslim12 / zod-deep-merge-experiment.ts
Created June 14, 2023 13:06
Experiments to deeply merge nested objects in Zod library.
// mkdir zod-deep-merge-experiement
// Copy and paste this code to your editor, save as `index.ts`
// yarn init --yes
// yarn add ts-node zod typescript @types/node
// yarn run tsc --init
// yarn run ts-node .
import { strict as assert } from "node:assert";
import { z } from "zod";
@lauslim12
lauslim12 / chakra-ui-react-hook-form-dynamic-form.tsx
Created April 14, 2023 13:08
Dynamic form with Chakra UI, React Hook Form, and TypeScript.
// Codesandbox: https://codesandbox.io/s/silly-wu-0fz2t1
import React, { useState, useRef } from "react";
import ReactDOM from "react-dom/client";
import { ChakraProvider } from "@chakra-ui/react";
import {
Controller,
useForm,
useFormState,
useFieldArray
@lauslim12
lauslim12 / restartpi.md
Created August 19, 2022 14:16
Yeah, we know Raspberry Pi sometimes loses its connection to Wi-Fi and you have to dirty your hands.

Restart Pi

Yeah, we know that sometimes Raspberry Pi loves to lose its internet connection. Once that is done, you have to unplug and replug it again, which is a real pain.

This helps to mitigate that issue. There are two scripts:

  1. To restart the Raspberry Pi upon sensing no Internet.
  2. To restart the Wi-Fi of the Raspberry Pi upon sensing no Internet.

Choose one and let's explore both of them!

@lauslim12
lauslim12 / raspberry-development-server.md
Last active August 30, 2022 15:31
Set up your personal development server in your network with a Raspberry Pi!

Raspberry Development Server

This note is made to provide knowledge on how to set up your own Raspberry Pi for personal development on your own Wi-Fi network. It does not necessarily have to be a Raspberry Pi, you can do it with any server / instance on Cloud as well (EC2, Compute Engine, Droplets, and the like).

As this is made for development, this is not suitable for production environments due to performance, security, and efficiency issues. Please adjust accordingly if you want to use this in production environments.

Specifications

Personally, I am using a Raspberry Pi 4. It is equipped with 8GB RAM and I installed a 64GB MicroSD on it to act as its storage. My Raspberry Pi has the following software specifications:

@lauslim12
lauslim12 / aes.ts
Last active March 21, 2022 07:35
TypeScript way to implement AES-256-GCM encryption in an object-oriented way in Node.js. Complete with code comments and sample runner.
/**
* You need Node.js, TypeScript, and NanoID. Use `npm i nanoid` in a sample project to test things out!
* Steps:
* - Create a folder, for example `mkdir aes-test` then `cd aes-test`.
* - Copy this script and create a file, for example `index.ts`.
* - Paste the file there.
* - `npm i nanoid ts-node-dev`.
* - `npm run ts-node-dev .`.
* - Profit!
*/
@lauslim12
lauslim12 / nodemailer-in-2022-without-oauth-and-less-secure-access.md
Last active February 11, 2022 05:54
Explains how to use Nodemailer without toggling 'Less Secure App Access' and/or use OAuth2.

Nodemailer

We have got a bit of a problem. How do I use nodemailer without toggling 'Less Secure App Access' and/or using OAuth2 in Google Developers?

Answer

Google developed a concept called App Passwords, and it's something that you can use in order to circumvent this issue.

Below are the steps you need to take:

@lauslim12
lauslim12 / useRequest.ts
Last active September 2, 2022 01:47
SWR React Hook to be reusable with as many requests as possible. TypeScript safe!
import { type Key, useSWR } from 'swr';
/**
* Fetcher is created to be used with `useSWR` hook.
*
* @param url - URL of the endpoint.
* @returns JSON response of the resulting request.
*/
const fetcher = async (url: string) => {
const res = await fetch(url);
@lauslim12
lauslim12 / github-unfollowers.go
Created December 17, 2021 03:09
Find people who do not follow you back on GitHub.
// Package main is a CLI runner to run this script ('github-unfollowers.go').
// This script is used to get a user's 'unfollowers' - which means people who do not follow that user's back.
// You do not need GitHub Tokens, as all the endpoints are public. Use this with responsibility though, do not spam.
//
// How to run: copy this script to a workspace, place your username, then run 'go run github-unfollowers.go'
// You can place your username by using 'export GITHUB_USERNAME=YOUR_GITHUB_USERNAME' or by editing the source code and filling it manually.
//
// Author & License: Nicholas (MIT)
package main
@lauslim12
lauslim12 / group.js
Created December 29, 2020 08:08
An algorithm to equally distribute students to the number of available teachers.
/**
* Group.js
* An algorithm to equally distribute students to the number of available teachers.
* Complexity is O(n).
*
* @input Arrays of teachers and students.
* @output Array of objects of grouped students and teachers.
*
* Run in Node (CommonJS)
* How to run: Clone or copy the gist, then do node ./group.js