Skip to content

Instantly share code, notes, and snippets.

View allenhwkim's full-sized avatar

Allen Kim allenhwkim

View GitHub Profile
@allenhwkim
allenhwkim / form-validation.html
Created October 1, 2024 21:38
form controls and validation
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form controls and validation</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
</head>
<body>
<form onsubmit="handleSubmit(event)" noValidate> <!-- noValidate -->
@allenhwkim
allenhwkim / reverse-string.js
Last active September 25, 2024 15:48
reverstString(str)
function reverseString(str) {
return !str ? '' : reverseString(str.substr(1)) + str.charAt(0);
}
reverseString("hello");
function reverse(str){
for (var i = str.length - 1, s=""; i >= 0; i--) s += str[i];
return s;
};
reverse("your string comes here")
@allenhwkim
allenhwkim / isBase64(string)
Last active September 23, 2024 18:30
Check if string is base64 encoded or not
function isBase64(v) {
if (typeof v !== 'string') return false;
const regEx = /^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/gi;
const isBase64 = regEx.test(v);
return isBase64;
}
// Test
var pngString = 'iVBORw0KGgoAAAANSUhEUgAABQAAAALQAQMAAAD1s08VAAAAA1BMVEX/AAAZ4gk3AAAAh0lEQVR42u3BMQEAAADCoPVPbQlPoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB4GsTfAAGc95RKAAAAAElFTkSuQmCC';
@allenhwkim
allenhwkim / Storage.js
Last active January 24, 2024 14:30
session storage data management
class Storage {
static getItem(key) {
if (key.match(/^([a-z0-9]+)\[['"]?(.*?)['"]?\]$/i)) { // array format e.g., groupName["foo"]
const [_, groupKey, itemKey] = key.match(/^([a-z0-9]+)\[['"]?(.*?)['"]?\]$/i);
const storageData = sessionStorage.getItem(groupKey);
const storageObj = JSON.parse(storageData);
return storageObj?.[itemKey];
} else if (key.match(/^([a-z0-9]+)\.([a-z0-9]+)/i)) { // key format groupName.foo
const [groupKey, itemKey] = key.split('.');
@allenhwkim
allenhwkim / encrypt.mjs
Last active April 30, 2023 03:20
NodeJS Compress / Encrypt / Base64
import * as crypto from 'crypto';
import * as zlib from 'zlib';
const assert = require('assert');
const encrypt = (val, password='p@55w07d') => {
const key = password.repeat(16).substring(0, 32);
const iv = key.substring(0, 16).split('').reverse().join('');
let cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
let encrypted = cipher.update(val, 'utf8', 'base64');
return (encrypted + cipher.final('base64')).replace(/[=]+/,'');
@allenhwkim
allenhwkim / compress.js
Last active November 12, 2024 15:50
pako string compression to/from base64 in a browser
import pako from 'pako';
// Usage:
// console.log(compress('hello'), decompress(compress('hello')));
export function compress(str) {
var unit8arr = pako.deflate(str);
return base64EncArr(unit8arr);
}
@allenhwkim
allenhwkim / resize-handle.js
Created March 3, 2023 21:33
resize handle like <textarea>
import { disableBodyScroll, enableBodyScroll } from 'body-scroll-lock';
// usage example:
// <resize-handle bottom left></resize-handle>
// <resize-handle bottom left single></resize-handle>
export class ResizeHandle extends HTMLElement {
static css = `
resize-handle { position: absolute; }
resize-handle:after { content: ' '; display: block; width: 12px; height: 12px; opacity: .5; }
[ req ]
default_bits = 2048
default_keyfile = server-key.pem
distinguished_name = subject
req_extensions = req_ext
x509_extensions = x509_ext
string_mask = utf8only
# The Subject DN can be formed using X501 or RFC 4514 (see RFC 4519 for a description).
# Its sort of a mashup. For example, RFC 4514 does not provide emailAddress.
@allenhwkim
allenhwkim / index.ts
Created September 28, 2022 22:12
OAuth2, Twitter API, Express, OpenID client
import express from 'express';
import session from 'express-session';
import crypto from 'crypto';
import { Issuer, generators, TokenSet } from 'openid-client';
declare module 'express-session' {
export interface SessionData {
tokenSet: TokenSet;
state: string;
codeVerifier: string;
/**
given two non-empty arrays A and B consisting of N integers, returns the number of fish that will stay alive.
For example, given the arrays shown above, the function should return 2, as explained above.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..100,000];
each element of array A is an integer within the range [0..1,000,000,000];
each element of array B is an integer that can have one of the following values: 0, 1;