Skip to content

Instantly share code, notes, and snippets.

View stevemu's full-sized avatar

Steve Mu stevemu

View GitHub Profile
@stevemu
stevemu / bridge.drawio
Created March 28, 2024 22:23
Bridge pattern graph
<mxfile host="app.diagrams.net" modified="2024-03-28T22:22:28.517Z" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" etag="oXXWV0pBanl8WLS-OPtp" version="24.0.4" type="device">
<diagram name="Page-1" id="fbFyoZ6bNdFxacDbzJJn">
<mxGraphModel dx="845" dy="878" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="guI2NQdVJi73r4s4yvk4-1" value="«interface»&lt;br&gt;&lt;b&gt;Interface1&lt;/b&gt;&lt;div&gt;&lt;hr&gt;+method1&lt;/div&gt;&lt;div&gt;+method2&lt;/div&gt;&lt;div&gt;+method3&lt;/div&gt;&lt;div&gt;+method4&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;" style="html=1;whiteSpace=wrap;" vertex="1" parent="1">
<mxGeometry x="30" y="160" width="110" height="130" as="geometry" />
</mxCell>
<mxCell id="guI2NQd
@stevemu
stevemu / config-overrides.js
Last active May 5, 2022 14:39
react-app-rewired config-overrides.js for referencing folders in adjacent folder https://github.com/facebook/create-react-app/issues/9127
const { override, addWebpackAlias } = require('customize-cra');
const path = require('path');
const overridePath = (webpackConfig) => {
const oneOfRule = webpackConfig.module.rules.find((rule) => rule.oneOf);
if (oneOfRule) {
const tsxRule = oneOfRule.oneOf.find(
(rule) => rule.test && rule.test.toString().includes('tsx')
);
@stevemu
stevemu / withResizeAware.js
Created May 4, 2019 21:34
a HOC for react that inject height and weight of window in a component, good for the use-case that you need to show or hide something depend on the size of the window
import React from 'react';
function withResizeAware(WrappedComponent) {
return class extends React.Component {
constructor(props) {
super(props);
this.state = {
height: document.documentElement.clientHeight,
width: document.documentElement.clientWidth
@stevemu
stevemu / MasterDetail.js
Last active May 4, 2019 21:34
a master-detail view for react
import React from 'react';
import {withStyles} from '@material-ui/core/styles';
import withResizeAware from '../../../core/withResizeAware';
const styles = {
root: {
flex: 1,
padding: 10
},
container: {
// algorithm question
// how to solve the buy - sell problem
// q: [3,5,6,9,2]
// a: 6
// init best dff = 3-5
// for each number in array
// loop each number after the number
// convert a csv file to form like texts
// Convert a csv like this:
// Name,Age, Bob,30, John,40,
// To text like this:
// Name: Bob Age: 30
// convert a csv like array like this:
// const tourdates = [
// ["date", "venue", "city"],
// ["jan 1", "paladium", "worcester"],
// ["jan 2", "irving", "nyc"],
// ["jan 3", "gramercy", "nyc"]
// ];
// into objects, like this:
@stevemu
stevemu / use await with promise.js
Created April 12, 2019 10:59
demoing using await with a promise function
const fetch = require('node-fetch'); // fetch polyfill in node.js; not required in front end situation
// returns an promise, so it can be used by await
async function getTodo(id) {
return new Promise((resolve, reject) => {
fetch('https://jsonplaceholder.typicode.com/todos/' + id)
.then(response => response.json())
.then(json => {
// success
// return data through resolve
@stevemu
stevemu / multiple-promise-in-async.js
Created December 9, 2018 00:41
run mutiple promises in one async function
const fetch = require('node-fetch'); // fetch polyfill in node.js; not required in front end situation
// returns an promise, so it can be used by await
async function getTodo(id) {
return new Promise((resolve, reject) => {
fetch('https://jsonplaceholder.typicode.com/todos/' + id)
.then(response => response.json())
.then(json => {
// success
// return data through resolve
@stevemu
stevemu / java-object-encryption-with-file.java
Last active August 13, 2021 03:15
How to encrypt an object and write it to file in Java and decrypt it
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import java.io.*;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class Main {