]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/string.js
2 Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
8 if(!dojo
._hasResource
["dojo.string"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo
._hasResource
["dojo.string"] = true;
10 dojo
.provide("dojo.string");
12 dojo
.getObject("string", true, dojo
);
16 // summary: String utilities for Dojo
20 dojo
.string
.rep = function(/*String*/str
, /*Integer*/num
){
22 // Efficiently replicate a string `n` times.
24 // the string to replicate
26 // number of times to replicate the string
28 if(num
<= 0 || !str
){ return ""; }
35 if(!(num
>>= 1)){ break; }
38 return buf
.join(""); // String
41 dojo
.string
.pad = function(/*String*/text
, /*Integer*/size
, /*String?*/ch
, /*Boolean?*/end
){
43 // Pad a string to guarantee that it is at least `size` length by
44 // filling with the character `ch` at either the start or end of the
45 // string. Pads at the start, by default.
49 // length to provide padding
51 // character to pad, defaults to '0'
53 // adds padding at the end if true, otherwise pads at start
55 // | // Fill the string to length 10 with "+" characters on the right. Yields "Dojo++++++".
56 // | dojo.string.pad("Dojo", 10, "+", true);
61 var out
= String(text
),
62 pad
= dojo
.string
.rep(ch
, Math
.ceil((size
- out
.length
) / ch
.length
));
63 return end
? out
+ pad
: pad
+ out
; // String
66 dojo
.string
.substitute = function( /*String*/ template
,
68 /*Function?*/ transform
,
69 /*Object?*/ thisObject
){
71 // Performs parameterized substitutions on a string. Throws an
72 // exception if any parameter is unmatched.
74 // a string with expressions in the form `${key}` to be replaced or
75 // `${key:format}` which specifies a format function. keys are case-sensitive.
77 // hash to search for substitutions
79 // a function to process all parameters before substitution takes
80 // place, e.g. mylib.encodeXML
82 // where to look for optional format function; default to the global
85 // Substitutes two expressions in a string from an Array or Object
86 // | // returns "File 'foo.html' is not found in directory '/temp'."
87 // | // by providing substitution data in an Array
88 // | dojo.string.substitute(
89 // | "File '${0}' is not found in directory '${1}'.",
90 // | ["foo.html","/temp"]
93 // | // also returns "File 'foo.html' is not found in directory '/temp'."
94 // | // but provides substitution data in an Object structure. Dotted
95 // | // notation may be used to traverse the structure.
96 // | dojo.string.substitute(
97 // | "File '${name}' is not found in directory '${info.dir}'.",
98 // | { name: "foo.html", info: { dir: "/temp" } }
101 // Use a transform function to modify the values:
102 // | // returns "file 'foo.html' is not found in directory '/temp'."
103 // | dojo.string.substitute(
104 // | "${0} is not found in ${1}.",
105 // | ["foo.html","/temp"],
107 // | // try to figure out the type
108 // | var prefix = (str.charAt(0) == "/") ? "directory": "file";
109 // | return prefix + " '" + str + "'";
114 // | // returns "thinger -- howdy"
115 // | dojo.string.substitute(
116 // | "${0:postfix}", ["thinger"], null, {
117 // | postfix: function(value, key){
118 // | return value + " -- howdy";
123 thisObject
= thisObject
|| dojo
.global
;
124 transform
= transform
?
125 dojo
.hitch(thisObject
, transform
) : function(v
){ return v
; };
127 return template
.replace(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g,
128 function(match
, key
, format
){
129 var value
= dojo
.getObject(key
, false, map
);
131 value
= dojo
.getObject(format
, false, thisObject
).call(thisObject
, value
, key
);
133 return transform(value
, key
).toString();
138 dojo.string.trim = function(str){
140 // Trims whitespace from both sides of the string
142 // String to be trimmed
144 // Returns the trimmed string
146 // This version of trim() was taken from [Steven Levithan's blog](http://blog.stevenlevithan.com/archives/faster-trim-javascript).
147 // The short yet performant version of this function is dojo.trim(),
148 // which is part of Dojo base. Uses String.prototype.trim instead, if available.
153 dojo
.string
.trim
= String
.prototype.trim
?
154 dojo
.trim
: // aliasing to the native function
156 str
= str
.replace(/^\s+/, '');
157 for(var i
= str
.length
- 1; i
>= 0; i
--){
158 if(/\S/.test(str
.charAt(i
))){
159 str
= str
.substring(0, i
+ 1);