git show commit user and count

github 上有個很酷的圖表是看到所有人的 commit 數量以及增減多少行 code,相同功能在 git 上也有,如同下方的顯示方式可以很清楚的看到所有 commit 數量以及是誰 commit

1266 Isken ******
139 Kyle ******
3 Randy **

只要使用以下指令就可以叫出上方這列表
git shortlog -sn

其中 shortlog 有以下幾個指令可以使用
usage: git shortlog [] [] [[--] [...]]

-n, --numbered sort output according to the number of commits per author(依照 commit 數量降覓排序)
-s, --summary Suppress commit descriptions, only provides commit count(不顯示 commit 內容,只顯示 commit 數量)
-e, --email Show the email address of each author (顯示 email 來分別不同 user)
-w[] Linewrap output

如果指令改為 -sne 會再加上 email 來判斷為不同人,所以同一個名字可能會出現兩個
git shortlog -sne

1266 Isken ******
216 Isken ******
139 Kyle ******
3 Randy **

如果後面再加上檔案路徑就只會顯示針對這檔案的 commit 數量

資料來源
https://www.kernel.org/pub/software/scm/git/docs/git-shortlog.html

javascript 強轉 boolean,數學運算

一些小小的 javascript 數學運算,第一個是快速的無條件設去的方式,但是用 "~~" 開頭會在 jslint 被擋下,雖然是較為快速,但使用時還是必須考量一下整體的 coding style,下面的是把變數強轉為 boolean,這也是個更快速的方式,但一樣會被 jslint 擋下,斟酌使用。

var a = (99.3 / 18.6) | 0; // 5
var b = ~~(99.3 / 18.6); // 5
window === true // false
!!window === true // true
view raw test.js hosted with ❤ by GitHub

web 小觀念, id 自動會變為 global 變數

如同標題,只要用加上 id,基本上都可以直接 console.log 出來 dom element,除非你用其他變數覆蓋過去,小觀念。

<div id="sample"></div>
<script type="text/javascript">
console.log(sample);
</script>
view raw test.html hosted with ❤ by GitHub


Javascript URL parsing

這也忘記是從哪來的,沒記錯應該是 stackoverflow.com 上找到(有發現記得告訴我),這是個快速 parsing url 的方式,使用了 html 本身的功能來做 parser,效能上當然不用多說。

function parseURL(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
host: a.hostname,
port: a.port,
query: a.search,
params: (function(){
var ret = {},
seg = a.search.replace(/^\?/,'').split('&'),
len = seg.length, i = 0, s;
for (;i<len;i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
hash: a.hash.replace('#',''),
path: a.pathname.replace(/^([^\/])/,'/$1'),
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
segments: a.pathname.replace(/^\//,'').split('/')
};
}
view raw urlParsing.js hosted with ❤ by GitHub


Log 變為模糊

此方式可將 inspect 打開後所看到的 log 變模糊,但不是每個瀏覽器都適用,目前測試 Chrome, Safair, Opera 可以正常運作。

var _log = console.log;
console.log = function() {
_log.call(console, '%c' + [].slice.call(arguments).join(' '), 'color:transparent;text-shadow:0 0 2px rgba(0,0,0,.5);');
};
view raw blurLog.js hosted with ❤ by GitHub


Javascript random string

這段真的忘記從哪來的,但相當實用,幫你產出亂碼字串,用的方式相當棒,先直接產生亂數再轉為 36 進位,其中帶入參數為想要產生幾位數的亂數。

function generateRandomAlphaNum(len) {
var rdmString = "";
for (; rdmString.length < len; rdmString += Math.random().toString(36).substr(2));
return rdmString.substr(0, len);
}
view raw randomString.js hosted with ❤ by GitHub


禁止使用 iframe 讀取你的頁面

算是個小技巧,幫自己筆記一下,加上這行後別的網站就無法使用 iframe 的方式把你的網站 load 進來。

if (window.location != window.parent.location) window.parent.location = window.location;