Analysis of Prototype Source Code supplemented by String (4)

Source: Internet
Author: User

Replace Interpolate|Sub|Scan|Truncate | gsub
Interpolate: regards a string as a template and fills it with the object attributes.
Sub: replacement is used to replace the substring specified before the string that matches the pattern specified by pattern.
Scan: traverses all substrings in the string that match the pattern specified by the pattern parameter. Returns the original string.
Truncate: truncates a string to a specified length (including the suffix) and adds a suffix.
Gsub: replacement is used to replace all the values in the string that match the pattern.

The most important method in the above method is gsub. For details, refer to the Prototype Template class.
In addition to the limited number of times, sub is completely consistent with gsub.
Copy codeThe Code is as follows:
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 is the same, but scan returns the string itself.
Interpolate uses the string as a template. The core is gsub.
The only difference between truncate and truncate is that I have already divided the errors ).
Take the string 'Fuck the gfw' as an example. the steps for truncate to run 'Fuck the gfw'. truncate (10, '***') are:
1. Get the first 10-'*****'. length characters 'fuck t'
2. append the suffix '*****' to obtain 'fuck t ***** 'with a length of 10.
The process is simple and the source code is also simple:
Copy codeThe Code is as follows:
Function truncate (length, truncation ){
Length = length | 30; // default length: 30
Truncation = Object. isUndefined (truncation )? '...': Truncation; // default suffix...
Return this. length> length?
This. slice (0, length-truncation. length) + truncation: String (this );
}


Another convenience of Prototype is that useful code can be extracted at any time as a separate part or for your own use. The following is a separate template method.
Copy codeThe Code is as follows:
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.exe c (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.exe c (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
}
})();

Copy codeThe Code is as follows:
Var template = new Template ('My age is: # {name. age }');
Console. log (template. evaluate ({name: {age: 24}); // my age is: 24

String part (end)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.