]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/string.js
2 Copyright (c) 2004-2010, 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");
14 // summary: String utilities for Dojo
18 dojo
.string
.rep = function(/*String*/str
, /*Integer*/num
){
20 // Efficiently replicate a string `n` times.
22 // the string to replicate
24 // number of times to replicate the string
26 if(num
<= 0 || !str
){ return ""; }
33 if(!(num
>>= 1)){ break; }
36 return buf
.join(""); // String
39 dojo
.string
.pad = function(/*String*/text
, /*Integer*/size
, /*String?*/ch
, /*Boolean?*/end
){
41 // Pad a string to guarantee that it is at least `size` length by
42 // filling with the character `ch` at either the start or end of the
43 // string. Pads at the start, by default.
47 // length to provide padding
49 // character to pad, defaults to '0'
51 // adds padding at the end if true, otherwise pads at start
53 // | // Fill the string to length 10 with "+" characters on the right. Yields "Dojo++++++".
54 // | dojo.string.pad("Dojo", 10, "+", true);
59 var out
= String(text
),
60 pad
= dojo
.string
.rep(ch
, Math
.ceil((size
- out
.length
) / ch
.length
));
61 return end
? out
+ pad
: pad
+ out
; // String
64 dojo
.string
.substitute = function( /*String*/ template
,
66 /*Function?*/ transform
,
67 /*Object?*/ thisObject
){
69 // Performs parameterized substitutions on a string. Throws an
70 // exception if any parameter is unmatched.
72 // a string with expressions in the form `${key}` to be replaced or
73 // `${key:format}` which specifies a format function. keys are case-sensitive.
75 // hash to search for substitutions
77 // a function to process all parameters before substitution takes
78 // place, e.g. mylib.encodeXML
80 // where to look for optional format function; default to the global
83 // Substitutes two expressions in a string from an Array or Object
84 // | // returns "File 'foo.html' is not found in directory '/temp'."
85 // | // by providing substitution data in an Array
86 // | dojo.string.substitute(
87 // | "File '${0}' is not found in directory '${1}'.",
88 // | ["foo.html","/temp"]
91 // | // also returns "File 'foo.html' is not found in directory '/temp'."
92 // | // but provides substitution data in an Object structure. Dotted
93 // | // notation may be used to traverse the structure.
94 // | dojo.string.substitute(
95 // | "File '${name}' is not found in directory '${info.dir}'.",
96 // | { name: "foo.html", info: { dir: "/temp" } }
99 // Use a transform function to modify the values:
100 // | // returns "file 'foo.html' is not found in directory '/temp'."
101 // | dojo.string.substitute(
102 // | "${0} is not found in ${1}.",
103 // | ["foo.html","/temp"],
105 // | // try to figure out the type
106 // | var prefix = (str.charAt(0) == "/") ? "directory": "file";
107 // | return prefix + " '" + str + "'";
112 // | // returns "thinger -- howdy"
113 // | dojo.string.substitute(
114 // | "${0:postfix}", ["thinger"], null, {
115 // | postfix: function(value, key){
116 // | return value + " -- howdy";
121 thisObject
= thisObject
|| dojo
.global
;
122 transform
= transform
?
123 dojo
.hitch(thisObject
, transform
) : function(v
){ return v
; };
125 return template
.replace(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g,
126 function(match
, key
, format
){
127 var value
= dojo
.getObject(key
, false, map
);
129 value
= dojo
.getObject(format
, false, thisObject
).call(thisObject
, value
, key
);
131 return transform(value
, key
).toString();
136 dojo.string.trim = function(str){
138 // Trims whitespace from both sides of the string
140 // String to be trimmed
142 // Returns the trimmed string
144 // This version of trim() was taken from [Steven Levithan's blog](http://blog.stevenlevithan.com/archives/faster-trim-javascript).
145 // The short yet performant version of this function is dojo.trim(),
146 // which is part of Dojo base. Uses String.prototype.trim instead, if available.
151 dojo
.string
.trim
= String
.prototype.trim
?
152 dojo
.trim
: // aliasing to the native function
154 str
= str
.replace(/^\s+/, '');
155 for(var i
= str
.length
- 1; i
>= 0; i
--){
156 if(/\S/.test(str
.charAt(i
))){
157 str
= str
.substring(0, i
+ 1);