strExtensions – JavaScript için string uzantıları

JavaScript kullanırken eksikliğini hissettiğim bir kaç string metot var. padLeft, padRight, trimLeft, trimRight ve trim metotları varsayılan string metotları arasında yer almıyor. Zaman içinde çeşitli projelerde bu metotlara ihtiyaç duydukça yazıp yazıp bir kenara koymuştum. Şimdi bu metotları toplayıp ufak bir paket oluşturdum ve sizlerle de paylaşayım dedim. valiDate v2.0‘ı da bu pakete dahil ettim. Metotları aşağıda bulabilirsiniz;

/*
 * strExtensions - String extensions
 * https://www.karalamalar.net/
 *
 * Copyright (c) 2009 İzzet Emre Erkan
 * Licensed under Creative Commons Attribution-Share Alike 3.0 Unported License
 * http://creativecommons.org/licenses/by-sa/3.0/
 *
 * Date: 2009-10-08 17:43:34 +0300 (Thu, 08 Oct 2009)
 * Revision: 2
 */
String.prototype.trimLeft =
  function(c) {
    c = c || ' ';
    return this.replace(new RegExp('^'+c+'+'),"");
  };
String.prototype.trimRight =
  function(c) {
    c = c || ' ';
    return this.replace(new RegExp(''+c+'+$'),"");
  };
String.prototype.trim =
  function(c) {
    return this.trimLeft(c).trimRight(c);
  };
String.prototype.padLeft =
  function(l, c) {
    c = c || ' ';
    var s = this.toString();
    while (s.length < l) s = c + s;
    return s;
  }
String.prototype.padRight =
  function(l, c) {
    c = c || ' ';
    var s = this.toString();
    while (s.length < l) s = s + c;
    return s;
  }
String.prototype.valiDate =
  function() {
    if(/^(0[1-9]|[12][0-9]PO|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)[0-9]{2}$/.test(this)) {
      var v = this.replace(/[- /]/g,'.').split('.');
      var d=parseInt(v[0],10), m=parseInt(v[1],10), y=parseInt(v[2],10);
      var o = new Date(y, m - 1, d);
      return o.getDate() == d && o.getMonth() + 1 == m && o.getFullYear() == y;
    }
    else
      return false;
  }

Paketin küçültülmüş halini (1130 byte) buradan edinebilirsiniz.

“strExtensions – JavaScript için string uzantıları” için bir yorum

Yorumlar kapalı.