Skip to content

Instantly share code, notes, and snippets.

@liuzhuan
liuzhuan / run.sh
Last active March 23, 2021 15:51
Compress png inside svg, in python v3. Based on https://gist.github.com/flesser/3f9c80ff42879cad41bf
#!/bin/sh
# This is used to process multiple svg files
cp -r svg-original svg-compressed
cd svg-compressed
files=$(ls *.svg)
for file in $files
do
@liuzhuan
liuzhuan / filterParams.js
Created June 12, 2017 02:04
Select some keys from a given object
function filterParams(source, keys) {
let result = {};
keys.forEach(item => {
result[item] = source[item];
});
return result;
}
const source = {
@liuzhuan
liuzhuan / passport.js
Created May 16, 2017 06:11
Pseudo code for passport sdk.
const SESSION_STATUS = {
NOT_LOGIN: 1,
LOGIN: 2,
ACCESS_TOKEN_EXPIRED: 3,
REFRESH_TOKEN_EXPIRED: 4
};
let refresh_timer = null;
function checkAuth(callback) {
@liuzhuan
liuzhuan / intInput.js
Created January 17, 2017 10:38
How to force a input field only accept integer?
/**
* 强制输入框只能输入正整数
* @param {string} id 输入框的id
*/
function intInput(id) {
var item = document.querySelector(id);
item.onkeyup = forceInt;
item.onafterpaste = forceInt;
function forceInt() {
import os, shutil
src = "img"
dest = "newimg"
if os.path.isdir(dest) == False:
os.mkdir(dest)
imgs = os.listdir(src)
@liuzhuan
liuzhuan / Ball.as
Created December 14, 2014 12:46
Node Garden in ActionScript 3
package {
import flash.display.Sprite;
public class Ball extends Sprite {
public var vx:Number;
public var vy:Number;
public function Ball() {
init();
}
function trim(str:String):String {
return str.replace(/^\s+/, "").replace(/\s+$/, "");
}
var demo:String = " foo bar ";
trace("["+demo+"]"); // [ foo bar ]
trace("["+trim(demo)+"]"); // [foo bar]
# version 3.X
# print out all the files' name, and stores
# them in a text file, in list format
import os
cwd = os.getcwd()
lst = os.listdir(cwd)
str = repr(lst)
output = open("output.txt", "w", encoding="utf-8")
output.write(str)
@liuzhuan
liuzhuan / read-UTF8.py
Last active August 29, 2015 14:10
Python read UTF-8 text file
# -*- coding:utf-8 -*-
import io
lines = io.open("data.txt", "r", encoding="utf-8-sig").readlines() # windows
lines = io.open("data.txt", "r", encoding="utf-8").readlines() # linux
var can = document.getElementById("can");
var ctx = can.getContext("2d");
var touching = 0;
can.addEventListener("touchstart", touchDown, false);
can.addEventListener("touchmove", touchXY, true); // why is true
can.addEventListener("touchend", touchUp, false);
function touchUp() {
touching = 0;