Skip to content

Instantly share code, notes, and snippets.

View leohxj's full-sized avatar
💯
Focusing

Leo Hui leohxj

💯
Focusing
View GitHub Profile
@leohxj
leohxj / parseURL.js
Created April 14, 2014 07:47
Parsing URLs with the DOM!
// This function creates a new anchor element and uses location
// properties (inherent) to get the desired URL data. Some String
// operations are used (to normalize results across browsers).
function parseURL(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
@leohxj
leohxj / ie8Event.js
Created May 12, 2014 09:05
IE8 Event
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
evnet.target = evnet.target || event.srcElement;
@leohxj
leohxj / .gitconfig
Created May 14, 2014 06:21
gitconfig
[user]
name = leohxj
email = leohxj@gmail.com
[alias]
st = status
l = log --pretty=oneline -n 20 --graph --abbrev-commit
ll = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --
b = branch
ci = commit
ca = commit -a
@leohxj
leohxj / nginx-forwards.conf
Created May 19, 2014 07:57
nginx实现反向代理
upstream nodejs__upstream {
server 127.0.0.1:3000;
keepalive 64;
}
server {
listen 80;
server_name 192.168.1.60;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@leohxj
leohxj / typeof.js
Created May 28, 2014 02:42
typeof运算符和Object.prototype.toString
typeof {foo: 'bar'};
// "object"
typeof ['foo', 'bar'];
// "object"
typeof "foobar";
// "string"
typeof /foo|bar/;
@leohxj
leohxj / arguments2Array.js
Created May 28, 2014 02:56
arguments对象转换成一个数组最好的办法就是 Array.prototype.slice.call
function y(){
var args = Array.prototype.slice.call(arguments);
}
@leohxj
leohxj / formatNumber.js
Created May 29, 2014 06:00
将阿拉伯数字每三位分隔,如15000->15,000
// 直白一点的方法, 循环拼接字符串
function formatNumber(number, split) {
var string = String(number);
var split = split || 3;
var newStr= new Array(string.length+ parseInt(string.length/split));
newStr[newStr.length-1]=string[string.length-1];
var currentIndex=string.length-1;
for(var i = newStr.length-1;i >= 0;i--)
{
if((newStr.length-i)%(split+1)==0)
@leohxj
leohxj / reverseStr.js
Created May 29, 2014 07:04
反转字符串, 效率的话iterative ,hack ,m5较高。测试地址为:http://jsperf.com/string-reverse-compare/5
// iterative
function string_reverse_1(str) {
var new_str = "";
for (var i = str.length - 1; i >= 0; i--) {
new_str += str.charAt(i);
}
return new_str;
}
@leohxj
leohxj / isInteger.js
Created June 2, 2014 09:20
在JavaScript中判断整型的N种方法
// 你可以使用余数运算(%),将一个数字按1求余,看看余数是不是0。
function isInteger(x) {
return x % 1 === 0;
}
// 通过Math.round()
function isInteger(x) {
return Math.round(x) === x;
}
@leohxj
leohxj / findMinArrayIndex.js
Created June 11, 2014 05:45
给定一个非空的JavaScript数字数组,找到最小值的索引。(如果最小值出现不止一次,那么任何此类索引是可以接受的。) 方式一,手工方式最快
// 1
function indexOfSmallest(a) {
var lowest = 0;
for (var i = 1; i < a.length; i++) {
if (a[i] < a[lowest]) lowest = i;
}
return lowest;
}
// 2