月份: 2010 年 2 月

  • 我的常用 js 函式庫

    ※ 2010-03-02 補充:

    Object prototype 改寫會導致 jQuery 出現異常,所以必須將 Object.prototype.isArray 註解或刪掉。
    請參考:Prototyping Object in Javascript breaks jQuery? – StackOverflow

    整理了一支我常用於網站開發的 js 函式庫,也包含了對字串、日期、陣列物件的 Prototype 增強函數,採 MIT 授權釋出,歡迎任意使用。

    源碼:lib.common.js(16 KB)
    壓縮:lib.common-min.js(9 KB)

    Prototype: String

    var url = 'http://essoduke.org/?s=測試';
    var x = 'Test 測試';
    var y = 'HTML';
    var z = '&;lt;strong&;gt;HTML</strong>';
    
    /* trim: 移除字串前後空白 */
    x.trim(); // result: Test
    
    /* bytes: 取得字串 bytes 長度 */
    x.bytes(); // result: 9
    
    /* empty: 是否為空 */
    x.empty(); // result: false
    
    /* left: 取字串左側 n 個字元 */
    x.left(4); // result: Test 
    
    /* right: 取字串右側 n 個字元 */
    x.right(4); //result: t 測試
    
    /* HTMLEncode: 編碼 HTML 標籤 */
    y.HTMLEncode(); // result: &lt;strong&gt;HTML&lt;/strong&gt;
    
    /* HTMLDecode: 還原被編碼的 HTML 字串 */
    z.HTMLDecode(); // result: HTML
    
    /* URLEncode: 進行 URL 編碼 */
    url.URLEncode(); // result: http%3A%2F%2Fessoduke.org%2F%3Fs%3D%E6%B8%AC%E8%A9%A6

    Prototype: Array

    var arr = [1, 2, 2, 3, 4, 4, 5];
    
    /* unique: 刪除重複的元素 */
    arr.unique(); // result: 1, 2, 3, 4, 5
    
    /* remove: 刪除第 n+1 個元素 */
    arr.remove(3); // result: 1, 2, 2, 4, 4, 5
    
    /* find: 尋找符合內容元素的鍵值 */
    arr.find(2); // result: 1, 2

    Prototype: Date

    var x = new Date();
    
    /* 
     * DateDiff: 計算傳入日期的差異 
     * @param string cDate : 要比較的日期
     * @param string mode: 計算類型 y=年, m=月, w=周, d=日
     */
    x.DateDiff('2010-01-01', 'd'); // result 56
    
    /* DateAdd: 加上或減去指定的日期時間間隔
     * @param string interval : 時間間隔單位 y, m, d, w, h, n, s, l
     * @param integer numer: 時間間隔單位次數,可為正數或負數
     * @param string pattern: 回傳的時間格式(非必要,預設 'yyyy-MM-dd hh:mm:ss')
     */
    x.DateAdd('d', 100); // result '2010-06-06 11:21:47'
    
    /*
     * format: 格式化日期
     * @param string interval : 引數可參考 http://php.net/manual/en/function.date.php
     */ 
    x.format('Y m j'); // result '2010-02-26'

    Prototype: Object

    /* isArray : 判斷是否為陣列 */
    var x = [1, 2, 3];
    x.isArray() // result 'true'
    var x = 'Test';
    x.isArray() // result 'false'

    Functions

    var web = new COMMON();
    
    /* regexp: 驗證常用欄位 account, password, email, url, ip, date, time, number, twid 台灣身份證 */
    web.regexp('[email protected]', 'email'); // 驗證電子郵件
    web.regexp('http://test.com', 'url'); // 驗證網址
    web.regexp('10.10.10.10', 'ip'); // 驗證 IP
    
    /* timestamp2time: 將 timestamp 轉為時間日期格式 */
    web.timestamp2time(1267155821); // result 'Fri Feb 26 2010 11:46:30 GMT+0800 (Taipei Standard Time)'
    
    /* timestamp: 取得 timestamp */
    web.timestamp();
    
    /* leftPad: 字串左邊補零 */
    web.leftPad('100', 6); // result '000100'
    
    /* ie: 檢查是否為 IE */
    web.ie();
    
    /* 
     * benchmark: 效能測試
     */
    web.benchmark.start(); // 開始計算
    [.....]
    web.benchmark.stop(); // 結束計算並返回執行秒數
    
    /*
     * cookie: cookie 操作
     */
    web.cookie.get('index'); // 取得 cookie[index] 內容
    web.cookie.set('index', '100'); //設置 cookie[index] 為 100
    web.cookie.remove('index'); // 移除 cookie[index]
    
    /*
     * debug: inline 方式顯示傳入變數內容或列舉子物件 
    * ※此方法需使用 jQuery Library,建議 1.3.2 以上版本
     */
    var x = 'String';
    web.debug(jQuery); // 列舉 jQuery 所有內容
    web.debug(x); //顯示 x 變數內容
  • 2010 ELITE Crono Mag

    荒廢很久的單車,多了這個玩具之後終於又可以…讓我氣喘吁吁了!

    噪音不大、腳感不輕,騎個幾分鐘就可以汗流浹背,不過 2010 年版的外盒就沒標示「MADE IN ITALY」,我猜是整組中國製的了。





    延伸閱讀:

  • 躲人隊,我來了

    上週四元氣售票開始預購 2010 MLB 台灣表演賽門票,中午開始就哀鴻遍野。
    果然一如往常,伺服器被塞爆,我的訂單也一直沒辦法完成訂購,客服電話又難打得要死。

    幸好皇天不負苦心人,禮拜六我撥了二三十通後終於接通,也順利完成付款,所以這些票是剛剛才拿到的…

    2010 MLB 台灣表演賽 澄清湖球場
    2010 MLB 台灣表演賽 澄清湖球場

    同場加映,皮克斯動畫二十年 – 高雄總動員,元旦買的到現在還沒去看 XD

    皮克斯動畫二十年 - 高雄總動員