Skip to content

Instantly share code, notes, and snippets.

View CatTail's full-sized avatar

Chiyu Zhong CatTail

View GitHub Profile
@CatTail
CatTail / askmark.py
Created October 10, 2012 16:39
面对知乎网站发表问题时需要问号做的调研
# coding=utf-8
#! /usr/bin/env
import httplib
import re
import sys
pn = 0
MAX = 10000
certainty = 0
// html email code transformation
// See http://www.awflasher.com/jsmail
function HtmlEncode( s )
{
var result = "";
for (var j = 0; j < s.length; j++ ) {
// Encode 25% of characters
if (Math.random() < 0.25
|| s.charAt(j) == ':'
|| s.charAt(j) == '@'
@CatTail
CatTail / gist:4167036
Created November 29, 2012 05:43
Javascript array concatenation
// Following concat function concatenate javascript arrays
// It consider three more factor then normal Array.prototype.concat:
// 1. eliminate common element
// 2. escape null and undefined
// 3. deal with element being an array
var arr1 = ['a'];
var arr2 = ['b', 'c'];
var arr3 = ['c', ['d'], 'e', undefined, null];
// return object classname
@CatTail
CatTail / htmlentity.js
Created November 30, 2012 08:27
Javascript: encode(decode) html text into html entity
// encode(decode) html text into html entity
var decodeHtmlEntity = function(str) {
return str.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
});
};
var encodeHtmlEntity = function(str) {
var buf = [];
for (var i=str.length-1;i>=0;i--) {
@CatTail
CatTail / dec2hex.js
Created November 30, 2012 10:49
Javascript: convert decimal into hexadecimal
/**
* @param {number} dec Decimal number need to be converted
* @return {string} String representation of hexadecimal
*/
var dec2hex = function(dec) {
var buf = [],
map = '0123456789ABCDEF';
while (parseInt(dec / 16, 10) !== 0) {
buf.unshift(map[dec % 16]);
dec = parseInt(dec / 16, 10);
@CatTail
CatTail / bitshift.js
Created December 1, 2012 00:28
Javascript: bitshift
var dec1 = 5234542352345;
var bin1 = dec1.toString(2);
// chop into 32 bit
var bin2 = bin1.slice(bin1.length-32);
var dec2 = parseInt(bin2, 2);
var dec3 = dec2 >> 0;
var bin3 = dec3.toString(2);
console.log(dec1);
@CatTail
CatTail / conversion.js
Created December 1, 2012 05:37
Javascript: encoding conversion
// escape don't: * @ – _ + . /
// encodeURI don't: ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
// encodeURIComponent don't: - _ . ! ~ * ' ( )
/*
// 部分转义
// text -> unicode
unicode = escape(text);
// unicode -> text
text = unescape(unicode);
// text -> utf8
@CatTail
CatTail / dec2bin.js
Created December 1, 2012 10:16
Javascript: convert negative number into binary
// Handler negtive number into binary.
// The code following is really meanless for number itself is depend on it's
// length. For instance, in bit operation, number will be treated as 32 bit.
// @see http://stackoverflow.com/questions/4338315/javascript-inverting-a-binary-value-of-a-number
function dec2Bin(dec)
{
if(dec > 0) {
return dec.toString(2);
}
else {
@CatTail
CatTail / validate_email.js
Last active July 10, 2020 07:17
Javascript: validate email address
/**
* Using regular expression to validate email address.
* Exactly email address format refer to http://en.wikipedia.org/wiki/Email_address.
* @author zhongchiyu@gmail.com
*/
var validateEmail = (function() {
// ATTENSION: escape is really mess because you have to escape in string and
// in regular expression.
var normal = "0-9a-zA-Z\\!#\\$%&'\\*\\+\\-\\/\\=\\?\\^_`\\{\\|\\}~";
// mix contain normal character and special character
@CatTail
CatTail / clone.js
Created December 4, 2012 13:38
Javascript: object clone
// clone only simple object
// handle Array, Date and RegExp
// resolve circular reference
var clone = (function(){
// classify object
var classof = function(o){
if (o === null) { return "null"; }
if (o === undefined) { return "undefined"; }
// I suppose Object.prototype.toString use obj.constructor.name
// to generate string