Библиотека строковых JavaScript - попадание на мелкий контрольно-пропускной пункт - PullRequest
1 голос
/ 10 мая 2010

Хорошо - я пытаюсь создать библиотеку строк, которая содержит несколько полезных вещей, отсутствующих в JavaScript.Вот что у меня есть:

function $__STRING__$(in_string) { this.s = in_string; }
$__STRING__$.prototype = {
    uppercase:      function(){this.s = this.s.toUpperCase(); return this;},
    lowercase:      function(){this.s = this.s.toLowerCase(); return this;},
    trim:           function(){this.s = this.s.replace(/^\s+|\s+$/g,""); return this;},
    ltrim:          function(){this.s = this.s.replace(/^\s+/,""); return this;},
    rtrim:          function(){this.s = this.s.replace(/\s+$/,""); return this;},
    striptags:      function(){this.s = this.s.replace(/<\/?[^>]+(>|$)/g, ""); return this;},
    escapetags:     function(){this.s = this.s.replace(/</g,"<").replace(/>/g,">"); return this;},
    unescapetags:   function(){this.s = this.s.replace(/</g,"<").replace(/>/g,">"); return this;},
    underscorize:   function(){this.s = this.s.replace(/ /g,"_"); return this;},
    dasherize:      function(){this.s = this.s.replace(/ /g,"-"); return this;},
    spacify:        function(){this.s = this.s.replace(/_/g," "); return this;},
    left:           function(length){this.s = this.s.substring(length,0); return this;},
    right:          function(length){this.s = this.s.substring(this.s.length,this.s.length-length); return this;},
    shorten:        function(length){if(this.s.length<=length){return this.s;}else{this.left(this.s,length)+"..."; return this;}},
    mid:            function(start,length){return this.s.substring(start,(length+start));},
    _down:          function(){return this.s;},
    contains:       function(needle){if(this.s.indexOf(needle)!==-1){return true;}else{return false;}},
    startswith:     function(needle){if(this.left(this.s,needle.length)==needle){return true;}else{return false;}},
    endswith:       function(needle){if(this.right(this.s,needle.length)==needle){return true;}else{return false;};},
    toString:       function(){return this.s;}
}

function $E(in_string){return new $__STRING__$(in_string);}
String.prototype._enhance   = function(){return new $__STRING__$(this);};
String.prototype._up        = function(){return new $__STRING__$(this);};

Это работает довольно хорошо, и я могу объединять команды и т. Д.

Я настроил это так, чтобы я мог приводить строку как расширенную строку.2 способа:

$E('some string');
'some string'._enhance();

Однако каждый раз, когда я хочу использовать встроенный строковый метод, мне нужно сначала преобразовать его обратно в строку.Итак, на данный момент я вставил _down () и _up () методы, например, так:

alert( $E("hello man").uppercase()._down().replace("N", "Y")._up().dasherize() ); 
alert( "hello man"._enhance().uppercase()._down().replace("N", "Y")._up().dasherize() );

Это работает нормально, но я действительно хочу сделать сможет использовать все встроенные функции, которые может использовать строка .Я понимаю, что могу просто воспроизвести каждую функцию внутри своего объекта, но я надеялся, что есть более простой способ.

Итак, вопрос в том, есть ли простой способ сделать это?

Спасибо -

Ответы [ 2 ]

3 голосов
/ 10 мая 2010

Вы можете просмотреть методы в String.prototype:

for(var name in String.prototype) {
    if (typeof String.prototype[name] !== "function") continue;
    if (name in $__STRING__$.prototype) continue;

    addToProto(name);
}

function addToProto(name) {
    $__STRING__$.prototype[name] = function() {
        this.s = String.prototype[name].apply(this.s, arguments);
        return this;
    };
}

Обратите внимание, что это не будет правильно обрабатывать такие методы, как indexOf; Вы можете изменить это так:

$__STRING__$.prototype[name] = function() {
    var retVal = String.prototype[name].apply(this.s, arguments);
    if (typeof retVal === "string") {
        this.s = retVal;
        return this;
    } else 
        return retVal;
};

Кроме того, вы должны переместить ваши явные методы в прототип, например:

 $__STRING__$.prototype.uppercase = function(){this.s = this.s.toUpperCase(); return this;};

РЕДАКТИРОВАТЬ : цикл for .. in не будет работать правильно (методы в String.prototype не перечисляются).

Вместо этого вам нужно вручную вызывать addToProto для каждого имени метода, например:

addToProto('replace');
0 голосов
/ 10 мая 2010
$__STRING__$.prototype = String.prototype;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...