]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/string.js.uncompressed.js
1 define("dojo/string", ["./_base/kernel", "./_base/lang"], function(dojo
, lang
) {
7 lang
.getObject("string", true, dojo
);
11 // summary: String utilities for Dojo
15 dojo
.string
.rep = function(/*String*/str
, /*Integer*/num
){
17 // Efficiently replicate a string `n` times.
19 // the string to replicate
21 // number of times to replicate the string
23 if(num
<= 0 || !str
){ return ""; }
30 if(!(num
>>= 1)){ break; }
33 return buf
.join(""); // String
36 dojo
.string
.pad = function(/*String*/text
, /*Integer*/size
, /*String?*/ch
, /*Boolean?*/end
){
38 // Pad a string to guarantee that it is at least `size` length by
39 // filling with the character `ch` at either the start or end of the
40 // string. Pads at the start, by default.
44 // length to provide padding
46 // character to pad, defaults to '0'
48 // adds padding at the end if true, otherwise pads at start
50 // | // Fill the string to length 10 with "+" characters on the right. Yields "Dojo++++++".
51 // | dojo.string.pad("Dojo", 10, "+", true);
56 var out
= String(text
),
57 pad
= dojo
.string
.rep(ch
, Math
.ceil((size
- out
.length
) / ch
.length
));
58 return end
? out
+ pad
: pad
+ out
; // String
61 dojo
.string
.substitute = function( /*String*/ template
,
63 /*Function?*/ transform
,
64 /*Object?*/ thisObject
){
66 // Performs parameterized substitutions on a string. Throws an
67 // exception if any parameter is unmatched.
69 // a string with expressions in the form `${key}` to be replaced or
70 // `${key:format}` which specifies a format function. keys are case-sensitive.
72 // hash to search for substitutions
74 // a function to process all parameters before substitution takes
75 // place, e.g. mylib.encodeXML
77 // where to look for optional format function; default to the global
80 // Substitutes two expressions in a string from an Array or Object
81 // | // returns "File 'foo.html' is not found in directory '/temp'."
82 // | // by providing substitution data in an Array
83 // | dojo.string.substitute(
84 // | "File '${0}' is not found in directory '${1}'.",
85 // | ["foo.html","/temp"]
88 // | // also returns "File 'foo.html' is not found in directory '/temp'."
89 // | // but provides substitution data in an Object structure. Dotted
90 // | // notation may be used to traverse the structure.
91 // | dojo.string.substitute(
92 // | "File '${name}' is not found in directory '${info.dir}'.",
93 // | { name: "foo.html", info: { dir: "/temp" } }
96 // Use a transform function to modify the values:
97 // | // returns "file 'foo.html' is not found in directory '/temp'."
98 // | dojo.string.substitute(
99 // | "${0} is not found in ${1}.",
100 // | ["foo.html","/temp"],
102 // | // try to figure out the type
103 // | var prefix = (str.charAt(0) == "/") ? "directory": "file";
104 // | return prefix + " '" + str + "'";
109 // | // returns "thinger -- howdy"
110 // | dojo.string.substitute(
111 // | "${0:postfix}", ["thinger"], null, {
112 // | postfix: function(value, key){
113 // | return value + " -- howdy";
118 thisObject
= thisObject
|| dojo
.global
;
119 transform
= transform
?
120 lang
.hitch(thisObject
, transform
) : function(v
){ return v
; };
122 return template
.replace(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g,
123 function(match
, key
, format
){
124 var value
= lang
.getObject(key
, false, map
);
126 value
= lang
.getObject(format
, false, thisObject
).call(thisObject
, value
, key
);
128 return transform(value
, key
).toString();
133 dojo.string.trim = function(str){
135 // Trims whitespace from both sides of the string
137 // String to be trimmed
139 // Returns the trimmed string
141 // This version of trim() was taken from [Steven Levithan's blog](http://blog.stevenlevithan.com/archives/faster-trim-javascript).
142 // The short yet performant version of this function is dojo.trim(),
143 // which is part of Dojo base. Uses String.prototype.trim instead, if available.
148 dojo
.string
.trim
= String
.prototype.trim
?
149 lang
.trim
: // aliasing to the native function
151 str
= str
.replace(/^\s+/, '');
152 for(var i
= str
.length
- 1; i
>= 0; i
--){
153 if(/\S/.test(str
.charAt(i
))){
154 str
= str
.substring(0, i
+ 1);