SemanticScuttle/includes/js/dojo/string.js

85 lines
2.7 KiB
JavaScript
Raw Normal View History

if(!dojo._hasResource["dojo.string"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojo.string"] = true;
dojo.provide("dojo.string");
/*=====
dojo.string = {
// summary: String utilities for Dojo
};
=====*/
dojo.string.pad = function(/*String*/text, /*int*/size, /*String?*/ch, /*boolean?*/end){
// summary:
// Pad a string to guarantee that it is at least `size` length by
// filling with the character `ch` at either the start or end of the
// string. Pads at the start, by default.
// text: the string to pad
// size: length to provide padding
// ch: character to pad, defaults to '0'
// end: adds padding at the end if true, otherwise pads at start
var out = String(text);
if(!ch){
ch = '0';
}
while(out.length < size){
if(end){
out += ch;
}else{
out = ch + out;
}
}
return out; // String
};
dojo.string.substitute = function( /*String*/template,
/*Object|Array*/map,
/*Function?*/transform,
/*Object?*/thisObject){
// summary:
// Performs parameterized substitutions on a string. Throws an
// exception if any parameter is unmatched.
// description:
// For example,
// | dojo.string.substitute("File '${0}' is not found in directory '${1}'.",["foo.html","/temp"]);
// | dojo.string.substitute("File '${name}' is not found in directory '${info.dir}'.",
// | {name: "foo.html", info: {dir: "/temp"}});
// both return
// | "File 'foo.html' is not found in directory '/temp'."
// template:
// a string with expressions in the form `${key}` to be replaced or
// `${key:format}` which specifies a format function.
// map: hash to search for substitutions
// transform:
// a function to process all parameters before substitution takes
// place, e.g. dojo.string.encodeXML
// thisObject:
// where to look for optional format function; default to the global
// namespace
return template.replace(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g, function(match, key, format){
var value = dojo.getObject(key,false,map);
if(format){ value = dojo.getObject(format,false,thisObject)(value);}
if(transform){ value = transform(value, key); }
return value.toString();
}); // string
};
dojo.string.trim = function(/*String*/ str){
// summary: trims whitespaces from both sides of the string
// description:
// This version of trim() was taken from [Steven Levithan's blog](http://blog.stevenlevithan.com/archives/faster-trim-javascript).
// The short yet performant version of this function is
// dojo.trim(), which is part of Dojo base.
str = str.replace(/^\s+/, '');
for(var i = str.length - 1; i > 0; i--){
if(/\S/.test(str.charAt(i))){
str = str.substring(0, i + 1);
break;
}
}
return str; // String
};
}