Skip to content

Instantly share code, notes, and snippets.

View up209d's full-sized avatar

Duc Duong up209d

View GitHub Profile
@up209d
up209d / signed_es.sh
Created September 1, 2020 00:49
Signed request for AWS Elasticsearch
#!/bin/bash
sha256Hash() {
local output=$(printf "$1" | shasum -a 256)
echo "${output%% *}"
}
hex256() {
printf "$(printf "$1" | od -A n -t x1 | sed -E -e 's/[ \t\n]*//g' | tr -d '\n')\n"
}
@up209d
up209d / VPN.sh
Last active September 6, 2020 07:53
Auto connecting to VPN on MacOS
#!/bin/sh
# VPN.sh
###### SETUP #######
# You need to setup and be able to connect to you VPN at least once
# Change these variables for your setup
# You can export your VPN_PWD into .bash_profile (.zprofile if you are using zsh) so it won't be exposed in the command
# In .bash_profile or .zprofile insert lines
@up209d
up209d / yup.test.js
Created October 9, 2019 00:41
Yup test reading values from current form state
export default yup.object().test('bothFieldsCannotBeEmpty', 'Both fields cannot be empty', values => {
const { matchingInclusionRule, matchingExclusionRule } = values;
return (
!!matchingInclusionRule ||
!!matchingExclusionRule ||
new yup.ValidationError(`Inclusion Rule & Exclusion Rule cannot be left both empty.`, null, 'matchingInclusionRule')
);
});
@up209d
up209d / basic-noise.js
Last active October 3, 2019 06:22
Most basic 1D noise generating (running from 0 - 1)
export const generateNoise = (scale = 0.01, resolution = 1024) => {
let currentScale = scale;
let currentResolution = resolution >= 2 ? resolution : 2;
const randomArray = Array(currentResolution)
.fill(0)
.map(() => Math.random());
return {
noiseAt: pos => {
const currentSeed = scale * pos;
const frag = currentSeed % 1;
@up209d
up209d / asyncTest.js
Created September 23, 2019 23:54
Jest Async Test - Axios Mock - Rejection case
export const sendForgotPasswordEmail = email => {
return axios
.put(forgotPasswordResetUrl, { email, isForgotPasswordEmail: true, deactivate: true }, { customSuccessMessage: '' })
.then(response => {
return response;
})
.catch(error => {
throw error;
});
};
@up209d
up209d / README.md
Created September 7, 2019 13:22 — forked from pbojinov/README.md
Two way iframe communication- Check out working example here: http://pbojinov.github.io/iframe-communication/

Two way iframe communication

The main difference between the two pages is the method of sending messages. Recieving messages is the same in both.

Parent

Send messages to iframe using iframeEl.contentWindow.postMessage Recieve messages using window.addEventListener('message')

iframe

@up209d
up209d / utils.js
Created June 27, 2019 23:41
utils.js
// Convert Image to binary to arrayBuffer to clipboard (not work)
var canvasDataUrl = ((document.getElementsByClassName("canvasjs-chart-canvas"))[0]).toDataURL("image/png");
fetch(canvasDataUrl)
.then(res => res.arrayBuffer())
.then(arrayBuffer => {
const uint8Array = new Uint8Array(arrayBuffer);
document.addEventListener('copy', function(e) {
var str = '';
uint8Array.forEach(function(d) {
@up209d
up209d / react-tips.md
Last active October 3, 2019 07:43
React Performance Tips

React Components:

  • Use PureComponent instead of Component.

  • For stateless Components, wrap them with React.memo() or Recompose.pure().

  • Break big Components into pieces, wrap static pieces (part that doesn't use props or state) with React.memo or Recompose.pure, it shall avoid re-render as much as possible.

  • Only Layout Components and Page Components are aware of store from Redux.

@up209d
up209d / tip.css
Last active July 12, 2018 00:14
Child Div Expand Height base on dynamic height of Parent Div
.parent-div {
display: table;
}
.child-div {
display:block; /* Have to be block */
height: 100%;
}
@up209d
up209d / push.sh
Last active June 1, 2018 09:42
Force to push 'dist ' folder from local branch 'origin' to remote branch 'gh-pages'
git push origin `git subtree split --prefix dist master`:gh-pages --force