Prototype源碼淺析 String部分(四)之補充

來源:互聯網
上載者:User

替換 interpolate  | sub |  scan |  truncate | gsub
interpolate : 將字串看作一個模板,並使用 object 的屬性填充它。
sub : 將字串中前指定個個與 pattern 指定的模式比對的子串用 replacement 替換
scan : 遍曆字串中與參數 pattern 指定的模式比對的所有子串。返回原始字串本身。
truncate : 將字串截短為指定的長度(包含尾碼部分), 並添加一個尾碼。
gsub :將字串中所有與 pattern 指定的模式比對的值全部用 replacement 替換掉

上面的方法中,最重要的一個方法是gsub,具體說明參見《淺析Prototype的模板類--Template》
sub除了可以限定次數外,其他與gsub完全一致。
複製代碼 代碼如下:
function sub(pattern, replacement, count) {
replacement = prepareReplacement(replacement);
count = Object.isUndefined(count) ? 1 : count;
return this.gsub(pattern, function(match) {
if (--count < 0) return match[0];
return replacement(match);
});
}

scan也是一樣的,不過scan最後返回的是字串本身而已。
interpolate 是將字串當做模板來用,核心還是gsub
truncate 是唯一有點區別的(我現在依稀感覺我分錯類了)。
以字串'fuck the gfw'為例,truncate 的執行'fuck the gfw'.truncate(10,'****')的步驟是:
1、獲得前面10 - '****'.length個字元 'fuck t'
2、拼上尾碼'****',得到 'fuck t****',長度為10.
處理很簡單,源碼也簡單:
複製代碼 代碼如下:
function truncate(length, truncation) {
length = length || 30;//預設長度30
truncation = Object.isUndefined(truncation) ? '...' : truncation;//預設尾碼...
return this.length > length ?
this.slice(0, length - truncation.length) + truncation : String(this);
}


另:Prototype的一個方便之處就是隨時可以抽取有用的代碼作為單獨的部分或者收為自己用。下面是單獨提出來的模板方法。
複製代碼 代碼如下:
function Template(template, pattern){
this.template = template;
this.pattern = pattern || /(^|.|\r|\n)(#\{(.*?)\})/;
}
Template.prototype = (function(){
function evaluate(obj){
return gsub.call(this,function(match){
if(obj == null){
return match[0] + '';
}
var before = match[1] || '';
if(before == '\\'){
return match[2];
}
var ctx = obj;
var expr = match[3];
var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/;
match = pattern.exec(expr);
if (match == null){
return before;
}
while (match != null) {
var comp = match[1].search(/^\[/) != -1 ? match[2].replace(/\\\\]/g, ']') : match[1];
ctx = ctx[comp];
if (null == ctx || '' == match[3]) break;
expr = expr.substring('[' == match[3] ? match[1].length : match[0].length);
match = pattern.exec(expr);
}
return before + (ctx === null ? '' : String(ctx));
});
}
function gsub(replacement){
var pattern = this.pattern;
var result = '';
var match = null;
var source = this.template;
if (!(pattern.length || pattern.source)) {
replacement = replacement('');
return replacement + source.split('').join(replacement) + replacement;
}
while (source.length > 0) {
if (match = source.match(pattern)) {
result += source.slice(0, match.index);
result += replacement(match) === null ? '' : String(replacement(match));
source = source.slice(match.index + match[0].length);
}else {
result += source;
source = '';
}
}
return result;
}
return {
constructor : Template,
evaluate : evaluate
}
})();

複製代碼 代碼如下:
var template = new Template('my age is : #{name.age}');
console.log(template.evaluate({name : {age : 24}}));//my age is : 24

String部分(完)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.