Skip to content

Instantly share code, notes, and snippets.

View max8hine's full-sized avatar
🎯
Focusing

Max Ma max8hine

🎯
Focusing
View GitHub Profile
@max8hine
max8hine / subscriptionStore.jsx
Last active July 8, 2022 23:55
Simplified Code Pattern
Class Store {
#state: { count: 0 };
constractor(initialState) {
if (initialState) this.#state = initialState
}
get state() {
return this.#state
}
@max8hine
max8hine / downloadFile.ts
Last active February 4, 2022 23:07
File Download with Javascript
const downloadFile = (data: string, name: string) => {
const blob = new Blob([data], { type: 'octet-stream' });
const href = URL.createObjectURL(blob);
const a = Object.assign(document.createElement("a"); { href, download: name, style: "display: none"})
a.click();
URL.revokeObjectURL(href);
}
@max8hine
max8hine / reactExample.js
Created September 22, 2020 00:25
AB testing
const variants = {
exp1: "base",
exp2: "variant2",
exp3: "variant3",
};
const experimentSettings = {
activate(exp_name, unique_identifier) {
// Api call to the server
return variants[exp_name] || 'base';
@max8hine
max8hine / helper.js
Last active June 25, 2020 03:15
Helper Methods (Pho, Js)
/**
* A errorHandler function
* Handle Axios request error
* Handle Javascript Error instance
* Handle Javascript TypeError
* Handle Any error and return default error message
* @param {Object || Any} error
* @returns {String}
*/
export const errorHandler = (
@max8hine
max8hine / SSH.md
Created September 14, 2019 04:21
SSH Key, Tips

Connect Github by Using SSH Key on macOS

Based on Github Doc, and clarified the confusing parts

  1. Open Terminal
  2. Checking for existing SSH keys
    $ ls -al ~/.ssh
    # Lists the files in your .shh directory, if they exist
@max8hine
max8hine / 1.js
Created August 14, 2019 23:42
Regex
var link = 'http://www.google.com/uploads/art/art_max_156575641388.0.png'
var parsingUrl = link.match(/\.([^.]*?)(?=\?|#|$)/)
/* [ '.png', 'png', index: 56, input: 'http://www.google.com/uploads/art/art_max_156575641388.0.png', groups: undefined ] */
var isPath = (/\.(jpg|jpeg|png)$/i).test(link)
/* true */
trimmedArtist = artist
@max8hine
max8hine / settings.json
Last active June 1, 2019 06:20
.vscode
{
"[javascript]": {
"editor.formatOnSave": true
},
"[json]": {
"editor.formatOnSave": true
},
"[css]": {
"editor.formatOnSave": true
},
@max8hine
max8hine / sessionSetting.js
Last active November 29, 2018 20:20
req.session and cookie
// https://github.com/jdesboeufs/connect-mongo#readme
// https://docs.mongodb.com/manual/tutorial/expire-data/
/* A session cookie is just a normal cookie without an expiration date.
Those are handled by the browser to be valid until the window is closed or program is quit.
But if the cookie is a httpOnly cookie (a cookie with the httpOnly parameter set),
you cannot read, change or delete it from outside of HTTP (meaning it must be changed on the server). */
cookie: { originalMaxAge: null, expires: null, httpOnly: true, path: '/' }
// Barchart
// approach 1 - full feature integration
// D3 handles props and state,
// React handles rednering logic
class Barchart extends Component {
xScale = d3.scaleBand().paddingInner(0.1)
yScale = d3.scaleLinear()
@max8hine
max8hine / proxies.js
Last active November 26, 2018 03:40
ES6 Proxies Object
const log = console.log
// https://www.youtube.com/watch?v=_5X2aB_mNp4
const Undefined = new Proxy(function() {}, {
// if call Undefined as function, you will get Undefined
get(target, key, receiver) {
return Undefined;
},
apply() {
return Undefined;