Skip to content

Instantly share code, notes, and snippets.

View huozhi's full-sized avatar
🎈

Jiachi Liu huozhi

🎈
View GitHub Profile
@huozhi
huozhi / readOnDrop.js
Created January 24, 2018 06:23
read content when drop file with drag events
const readOnDrop = (element, onRead) => {
element.addEventListener('dragover', ev => ev.preventDefault())
element.addEventListener('drop', ev => {
ev.preventDefault()
const dt = ev.dataTransfer
const file = dt.files[0]
const reader = new FileReader()
reader.onload = event => onRead(event.target.result)
reader.readAsText(file)
})
@huozhi
huozhi / ssl.sh
Created January 16, 2018 03:13
generate ssl keys for localhost
openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
# only enter the Common Name: your ip address or your domain name
@huozhi
huozhi / shuffleArray.js
Created July 14, 2017 02:54
shuffle an array
// https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array/6274381#6274381
/**
* Shuffles array in place. ES6 version
* @param {Array} a items The array containing the items.
*/
function shuffle(a) {
for (let i = a.length; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
@huozhi
huozhi / formatTime.js
Created July 11, 2017 09:43
format seconds with given formatter
function formatTime(seconds, format) {
var values = {
h: Math.floor(seconds / 3600),
m: Math.floor(seconds % 3600 / 60),
s: Math.round(seconds % 60),
}
var type
var value
return format.split(':')
@huozhi
huozhi / git-log.sh
Created June 23, 2017 02:40
git pretty log
git log --all --decorate --oneline --graph
@huozhi
huozhi / chainFunctions.js
Created February 26, 2017 06:02
chained multiple functions
/**
* https://github.com/react-bootstrap/react-bootstrap/blob/master/src/utils/createChainedFunction.js
* Safe chained function
*
* Will only create a new function if needed,
* otherwise will pass back existing functions or null.
*/
export function chainFunctions(...funcs) {
return funcs
.filter(f => f != null)
@huozhi
huozhi / calculator.js
Created February 25, 2017 04:44
calculate expression
var isNumber = (val) => /[0-9]/.test(val)
var isAlpha = (val) => /[a-z]/.test(val)
var isEmpty = (arr) => arr.length === 0
var top = (stack) => stack[stack.length - 1]
var calculate = function(str) {
var i = 0
var snum = []
var sop = []
@huozhi
huozhi / base64ToBlob.js
Created September 12, 2016 13:19
convert base64 to blob object (from stackoverflow)
export const dataURItoBlob = (dataURI) => {
// convert base64/URLEncoded data component to raw binary data held in a string
let byteString
if (dataURI.split(',')[0].indexOf('base64') >= 0) {
byteString = atob(dataURI.split(',')[1])
} else {
byteString = unescape(dataURI.split(',')[1])
}
// separate out the mime component
const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
@huozhi
huozhi / onPasetImage.js
Created September 12, 2016 13:18
paste image from clipboard
export const onPasetImage = (event, onLoadImageDataURI) => {
const items = (event.clipboardData || event.originalEvent.clipboardData).items
for (let index in items) {
const item = items[index]
if (item.kind === 'file') {
const blob = item.getAsFile()
const reader = new FileReader()
reader.onload = (event) => {
onLoadImageDataURI(event.target.result)
}
@huozhi
huozhi / shmod.sh
Created August 20, 2016 18:06
show file mode
#/bin/bash
stat -f "%OLp %N" $@