Prototype源碼淺析 String部分(四)之補(bǔ)充
更新時間:2012年01月16日 00:18:54 作者:
Prototype源碼淺析 String部分(四)之補(bǔ)充,需要的朋友可以參考下。
替換 | interpolate | sub | scan | truncate | gsub |
sub : 將字符串中前指定個個與 pattern 指定的模式匹配的子串用 replacement 替換
scan : 遍歷字符串中與參數(shù) pattern 指定的模式匹配的所有子串。返回原始字符串本身。
truncate : 將字符串截短為指定的長度(包含后綴部分), 并添加一個后綴。
gsub :將字符串中所有與 pattern 指定的模式匹配的值全部用 replacement 替換掉
上面的方法中,最重要的一個方法是gsub,具體說明參見《淺析Prototype的模板類--Template》
sub除了可以限定次數(shù)外,其他與gsub完全一致。
復(fù)制代碼 代碼如下:
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 是將字符串當(dāng)做模板來用,核心還是gsub
truncate 是唯一有點(diǎn)區(qū)別的(我現(xiàn)在依稀感覺我分錯類了)。
以字符串'fuck the gfw'為例,truncate 的執(zhí)行'fuck the gfw'.truncate(10,'****')的步驟是:
1、獲得前面10 - '****'.length個字符 'fuck t'
2、拼上后綴'****',得到 'fuck t****',長度為10.
處理很簡單,源碼也簡單:
復(fù)制代碼 代碼如下:
function truncate(length, truncation) {
length = length || 30;//默認(rèn)長度30
truncation = Object.isUndefined(truncation) ? '...' : truncation;//默認(rèn)后綴...
return this.length > length ?
this.slice(0, length - truncation.length) + truncation : String(this);
}
另:Prototype的一個方便之處就是隨時可以抽取有用的代碼作為單獨(dú)的部分或者收為自己用。下面是單獨(dú)提出來的模板方法。
復(fù)制代碼 代碼如下:
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
}
})();
復(fù)制代碼 代碼如下:
var template = new Template('my age is : #{name.age}');
console.log(template.evaluate({name : {age : 24}}));//my age is : 24
String部分(完)
相關(guān)文章
[轉(zhuǎn)]prototype 源碼解讀 超強(qiáng)推薦
[轉(zhuǎn)]prototype 源碼解讀 超強(qiáng)推薦...2007-02-02prototype 源碼中文說明之 prototype.js
prototype 源碼中文說明之 prototype.js...2006-09-09Prototype ObjectRange對象學(xué)習(xí)
ObjectRange對象基本就是實(shí)現(xiàn)了連續(xù)的數(shù)字或者字符串,其中只包含一個方法,include,判斷某個數(shù)字或者字符串是否在ObjectRange里。并且ObjectRange對象還混入了Enumerable的方法,所以可以直接在ObjectRange對象上調(diào)用Enumerable對象里面的方法。2009-07-07