Skip to content

Instantly share code, notes, and snippets.

View nthung2112's full-sized avatar
🏠
Working from home

Hung Nguyen nthung2112

🏠
Working from home
View GitHub Profile
@nthung2112
nthung2112 / password-hasher.ts
Created May 24, 2025 14:37
Password encode decode
interface HashPasswordParams {
password: string;
providedSalt?: Uint8Array;
}
async function hashPassword({ password, providedSalt }: HashPasswordParams) {
const encoder = new TextEncoder();
const salt = providedSalt || crypto.getRandomValues(new Uint8Array(16));
const keyMaterial = await crypto.subtle.importKey(
@nthung2112
nthung2112 / result.ts
Last active May 24, 2025 04:58
Error Try Catch
// /utils/result.ts
export type Failure<E> = {
success: false;
error: E;
};
function Failure<E>(error: E): Failure<E> {
return {
success: false,
error,
@nthung2112
nthung2112 / AlertDialogProvider.tsx
Last active November 30, 2024 14:40 — forked from alexanderson1993/AlertDialogProvider.tsx
A multi-purpose alert/confirm/prompt replacement built with shadcn/ui AlertDialog components.
'use client';
import * as React from 'react';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import {
AlertDialog,
AlertDialogContent,
AlertDialogHeader,
AlertDialogTitle,
@nthung2112
nthung2112 / bookmarket.txt
Last active November 19, 2024 15:50
Translate Extension
javascript:(function()%7Bfunction%20getSelectedText()%20%7B%0A%20%20if%20(typeof%20window.getSelection%20!%3D%20'undefined')%20%7B%0A%20%20%20%20return%20window.getSelection().toString()%3B%0A%20%20%7D%20else%20if%20(typeof%20document.selection%20!%3D%20'undefined'%20%26%26%20document.selection.type%20%3D%3D%20'Text')%20%7B%0A%20%20%20%20return%20document.selection.createRange().text%3B%0A%20%20%7D%0A%20%20return%20''%3B%0A%7D%0A%0Afunction%20doSomethingWithSelectedText(event)%20%7B%0A%20%20event.stopPropagation()%3B%0A%20%20const%20selectedText%20%3D%20getSelectedText()%3B%0A%20%20if%20(selectedText)%20%7B%0A%20%20%20%20doTranslate(selectedText)%3B%0A%20%20%7D%0A%7D%0Adocument.onmouseup%20%3D%20doSomethingWithSelectedText%3B%0Adocument.onkeyup%20%3D%20doSomethingWithSelectedText%3B%0A%0Aasync%20function%20doTranslate(selectedText)%20%7B%0A%20%20const%20res%20%3D%20await%20fetch(%0A%20%20%20%20%60https%3A%2F%2Ftranslate.googleapis.com%2Ftranslate_a%2Fsingle%3Fclient%3Dgtx%26sl%3Den%26tl%3Dvi%26hl%3Den-US%26dt
@nthung2112
nthung2112 / config
Created September 5, 2024 01:49 — forked from pksunkara/config
Sample of git config file (Example .gitconfig) (Place them in $XDG_CONFIG_HOME/git)
[user]
name = Pavan Kumar Sunkara
email = [email protected]
username = pksunkara
[init]
defaultBranch = master
[core]
editor = nvim
whitespace = fix,-indent-with-non-tab,trailing-space,cr-at-eol
pager = delta
@nthung2112
nthung2112 / data.json
Created August 1, 2024 15:14
NTH Bookmarks
[
{
"type": "folder",
"addDate": 1718526477999,
"title": "Bookmarks Bar",
"children": [
{
"type": "link",
"addDate": 1718526687700,
"title": "Google",
@nthung2112
nthung2112 / zustand-internals.jsx
Created April 22, 2024 14:57 — forked from arkatsy/zustand-internals.jsx
How zustand works internally
import { useSyncExternalStore } from "react";
// For more on the useSyncExternalStore hook, see https://react.dev/reference/react/useSyncExternalStore
// The code is almost identical to the source code of zustand, without types and some features stripped out.
// Check the links to see the references in the source code.
// The links are referencing the v5 of the library. If you plan on reading the source code yourself v5 is the best way to start.
// The current v4 version contains lot of deprecated code and extra stuff that makes it hard to reason about if you're new to this.
// https://github.com/pmndrs/zustand/blob/fe47d3e6c6671dbfb9856fda52cb5a3a855d97a6/src/vanilla.ts#L57-L94
function createStore(createState) {
function countShips(B) {
const result = [0, 0, 0];
const map = B.map(row=>row.split(''));
const n = map.length;
const m = map[0].length;
for (let row = 0; row < n; row++) {
for (let col = 0; col < m; col++) {
if (map[row][col] === '#') {
map[row][col] = '.';
@nthung2112
nthung2112 / index.html
Created July 28, 2023 17:43 — forked from dsheiko/index.html
Service-worker to prefetch remote images (with expiration) and respond with fallback one when image cannot be fetched
<!DOCTYPE html>
<html>
<head>
<title>Service-worker demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
if ( "serviceWorker" in navigator ) {
@nthung2112
nthung2112 / kodi-config.json
Created February 3, 2023 19:08
Kodi Plugin
[
{
"name": "Sport",
"logo": "https://i.imgur.com/LTdCdqy.png",
"version": 1,
"searchable": False,
"sites": [
{
"name": "90p.live",
"logo": "https://i.imgur.com/jyM3inb.png",