Skip to content

Instantly share code, notes, and snippets.

View nomyfan's full-sized avatar
🍵
得閑飲茶

Kim Chan nomyfan

🍵
得閑飲茶
View GitHub Profile
@nomyfan
nomyfan / syncState.ts
Last active February 8, 2021 09:22
Multi-binding props and data shortcuts
type PropName = string;
type DateName = string;
export type PropDataNames = [PropName, DateName];
export function syncState(...tuples: PropDataNames[]) {
const watcher = {} as { [index: string]: (v: any) => void };
for (const tuple of tuples) {
const [propName, dataName] = tuple;
@nomyfan
nomyfan / reverse_singly_linkedlist.rs
Created April 3, 2021 15:37
Reverse singly linkedlist
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode { next: None, val }
@nomyfan
nomyfan / useResizeSensor.ts
Last active May 25, 2021 02:59
A react hook wrapper for ResizeSensor
import { useEffect, useRef } from "react";
import { ResizeSensor, ResizeSensorCallback } from "css-element-queries";
function newResizeSensor(
targetNode: HTMLElement | null,
callback: ResizeSensorCallback,
) {
return targetNode ? new ResizeSensor(targetNode, callback) : null;
}
@nomyfan
nomyfan / MyComponent.js
Created April 15, 2021 09:30
React Context with HOC example
import React from "react";
import { withThemeContext } from "./context";
const MyComponent = withThemeContext(
class extends React.Component {
constructor() {
super();
this.handleToggleTheme = this.handleToggleTheme.bind(this);
}
@nomyfan
nomyfan / csprng.js
Created May 7, 2021 16:55
CSPRNG(Cryptographically Secure Pseudo Random Number Generator)
function csprng() {
const maxU32 = 0xFFFFFFFF;
return crypto.getRandomValues(new Uint32Array(1))[0] / maxU32;
}
async function sha2(txt, sha = "SHA-256") {
const encoder = new TextEncoder();
const message = encoder.encode(txt);
const digest = await crypto.subtle.digest(sha, message);
return Array.from(new Uint8Array(digest)).map((x) => x.toString(16).padStart(2, "0")).join("");
}
@nomyfan
nomyfan / index.html
Created May 7, 2021 17:42
Calculate SHA-256 of a file
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Calculate SHA-2 of a file</title>
</head>
<body>
<input type="file" id="file-input" onchange="handleFileChange(event)" />
<select id="sha2-algo-select" onchange="handleSelectChange(event)">
@nomyfan
nomyfan / App.css
Last active June 20, 2021 12:00
React less more component
@import "~antd/dist/antd.css";
.App {
padding: 50px;
}
.App button {
margin: 10px;
}
@nomyfan
nomyfan / App.css
Created June 27, 2021 14:40
Show less and more children with specific lines
@import "~antd/dist/antd.css";
.App {
padding: 50px;
}
.App button {
margin: 10px;
}
@nomyfan
nomyfan / reactive-search.ts
Last active July 4, 2021 15:32
RxJS demo1 - reactive search
import { Observable, Subscriber, fromEvent } from "rxjs";
import { ajax } from "rxjs/ajax";
import {
pluck,
debounceTime,
filter,
tap,
map,
switchMap,
defaultIfEmpty,