Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View allenhwkim's full-sized avatar

Allen Kim allenhwkim

View GitHub Profile
@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 April 20, 2023 13:59
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;
@allenhwkim
allenhwkim / NumberOfDiscIntersections.js
Last active July 5, 2022 03:16
NumberOfDiscIntersections
function solution(A) { // [1,5,2,1,4,0]
var starts = []; // disk start points
var ends = []; // disk end points
for (var i=0; i<A.length; i++) {
starts.push(i - A[i]); // [-1, -4, 0, 2, 0, 5]
ends.push(i + A[i]); // [1, 6, 4, 4, 8, 5]
}
starts.sort( (a,b) => a-b) // [ -4, -1, 0, 0, 2, 5 ]
ends.sort( (a,b) => a-b) // [ 1, 4, 4, 5, 6, 8 ]
function solution(A) {
if (A.length < 3) return 0;
A = A.sort( (a,b) => a - b)
for (var i=0; i< A.length -2; i++) {
const [p, q, r] = A.slice(i, i+3)
if ( (p + q) > r && (p+r) > q && (q+r) > p)
return 1
}
function solution(A) {
n = A.length;
maxProduct = Number.MIN_SAFE_INTEGER
A = A.sort( (a,b) => a-b) // <--- Important
maxProduct = Math.max(maxProduct, A[0] * A[1] * A[n-1]);
maxProduct = Math.max(maxProduct, A[n-3] * A[n-2] * A[n-1]);
return maxProduct;
}