我的常用 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

  1. var url = 'http://essoduke.org/?s=測試';
  2. var x = 'Test 測試';
  3. var y = '<strong>HTML</strong>';
  4. var z = '&;lt;strong&;gt;HTML&lt;/strong&gt;';
  5.  
  6. /* trim: 移除字串前後空白 */
  7. x.trim(); // result: Test
  8.  
  9. /* bytes: 取得字串 bytes 長度 */
  10. x.bytes(); // result: 9
  11.  
  12. /* empty: 是否為空 */
  13. x.empty(); // result: false
  14.  
  15. /* left: 取字串左側 n 個字元 */
  16. x.left(4); // result: Test
  17.  
  18. /* right: 取字串右側 n 個字元 */
  19. x.right(4); //result: t 測試
  20.  
  21. /* HTMLEncode: 編碼 HTML 標籤 */
  22. y.HTMLEncode(); // result: &amp;lt;strong&amp;gt;HTML&amp;lt;/strong&amp;gt;
  23.  
  24. /* HTMLDecode: 還原被編碼的 HTML 字串 */
  25. z.HTMLDecode(); // result: <strong>HTML</strong>
  26.  
  27. /* URLEncode: 進行 URL 編碼 */
  28. url.URLEncode(); // result: http%3A%2F%2Fessoduke.org%2F%3Fs%3D%E6%B8%AC%E8%A9%A6

Prototype: Array

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

Prototype: Date

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

Prototype: Object

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

Functions

  1. var web = new COMMON();
  2.  
  3. /* regexp: 驗證常用欄位 account, password, email, url, ip, date, time, number, twid 台灣身份證 */
  4. web.regexp('i.am@god.com', 'email'); // 驗證電子郵件
  5. web.regexp('http://test.com', 'url'); // 驗證網址
  6. web.regexp('10.10.10.10', 'ip'); // 驗證 IP
  7.  
  8. /* timestamp2time: 將 timestamp 轉為時間日期格式 */
  9. web.timestamp2time(1267155821); // result 'Fri Feb 26 2010 11:46:30 GMT+0800 (Taipei Standard Time)'
  10.  
  11. /* timestamp: 取得 timestamp */
  12. web.timestamp();
  13.  
  14. /* leftPad: 字串左邊補零 */
  15. web.leftPad('100', 6); // result '000100'
  16.  
  17. /* ie: 檢查是否為 IE */
  18. web.ie();
  19.  
  20. /*
  21. * benchmark: 效能測試
  22. */
  23. web.benchmark.start(); // 開始計算
  24. [.....]
  25. web.benchmark.stop(); // 結束計算並返回執行秒數
  26.  
  27. /*
  28. * cookie: cookie 操作
  29. */
  30. web.cookie.get('index'); // 取得 cookie[index] 內容
  31. web.cookie.set('index', '100'); //設置 cookie[index] 為 100
  32. web.cookie.remove('index'); // 移除 cookie[index]
  33.  
  34. /*
  35. * debug: inline 方式顯示傳入變數內容或列舉子物件
  36. * ※此方法需使用 <a href="http://jquery.com/">jQuery Library</a>,建議 1.3.2 以上版本
  37. */
  38. var x = 'String';
  39. web.debug(jQuery); // 列舉 jQuery 所有內容
  40. web.debug(x); //顯示 x 變數內容
標籤: , , ,

相關文章

No Comments yet »

本篇文章迴響的訂閱源料 TrackBack URI

發表迴響

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds. Valid XHTML and CSS.