string.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Copyright (c) 2004-2012, 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
  5. */
  6. if(!dojo._hasResource["dojo.string"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojo.string"] = true;
  8. dojo.provide("dojo.string");
  9. dojo.getObject("string", true, dojo);
  10. /*=====
  11. dojo.string = {
  12. // summary: String utilities for Dojo
  13. };
  14. =====*/
  15. dojo.string.rep = function(/*String*/str, /*Integer*/num){
  16. // summary:
  17. // Efficiently replicate a string `n` times.
  18. // str:
  19. // the string to replicate
  20. // num:
  21. // number of times to replicate the string
  22. if(num <= 0 || !str){ return ""; }
  23. var buf = [];
  24. for(;;){
  25. if(num & 1){
  26. buf.push(str);
  27. }
  28. if(!(num >>= 1)){ break; }
  29. str += str;
  30. }
  31. return buf.join(""); // String
  32. };
  33. dojo.string.pad = function(/*String*/text, /*Integer*/size, /*String?*/ch, /*Boolean?*/end){
  34. // summary:
  35. // Pad a string to guarantee that it is at least `size` length by
  36. // filling with the character `ch` at either the start or end of the
  37. // string. Pads at the start, by default.
  38. // text:
  39. // the string to pad
  40. // size:
  41. // length to provide padding
  42. // ch:
  43. // character to pad, defaults to '0'
  44. // end:
  45. // adds padding at the end if true, otherwise pads at start
  46. // example:
  47. // | // Fill the string to length 10 with "+" characters on the right. Yields "Dojo++++++".
  48. // | dojo.string.pad("Dojo", 10, "+", true);
  49. if(!ch){
  50. ch = '0';
  51. }
  52. var out = String(text),
  53. pad = dojo.string.rep(ch, Math.ceil((size - out.length) / ch.length));
  54. return end ? out + pad : pad + out; // String
  55. };
  56. dojo.string.substitute = function( /*String*/ template,
  57. /*Object|Array*/map,
  58. /*Function?*/ transform,
  59. /*Object?*/ thisObject){
  60. // summary:
  61. // Performs parameterized substitutions on a string. Throws an
  62. // exception if any parameter is unmatched.
  63. // template:
  64. // a string with expressions in the form `${key}` to be replaced or
  65. // `${key:format}` which specifies a format function. keys are case-sensitive.
  66. // map:
  67. // hash to search for substitutions
  68. // transform:
  69. // a function to process all parameters before substitution takes
  70. // place, e.g. mylib.encodeXML
  71. // thisObject:
  72. // where to look for optional format function; default to the global
  73. // namespace
  74. // example:
  75. // Substitutes two expressions in a string from an Array or Object
  76. // | // returns "File 'foo.html' is not found in directory '/temp'."
  77. // | // by providing substitution data in an Array
  78. // | dojo.string.substitute(
  79. // | "File '${0}' is not found in directory '${1}'.",
  80. // | ["foo.html","/temp"]
  81. // | );
  82. // |
  83. // | // also returns "File 'foo.html' is not found in directory '/temp'."
  84. // | // but provides substitution data in an Object structure. Dotted
  85. // | // notation may be used to traverse the structure.
  86. // | dojo.string.substitute(
  87. // | "File '${name}' is not found in directory '${info.dir}'.",
  88. // | { name: "foo.html", info: { dir: "/temp" } }
  89. // | );
  90. // example:
  91. // Use a transform function to modify the values:
  92. // | // returns "file 'foo.html' is not found in directory '/temp'."
  93. // | dojo.string.substitute(
  94. // | "${0} is not found in ${1}.",
  95. // | ["foo.html","/temp"],
  96. // | function(str){
  97. // | // try to figure out the type
  98. // | var prefix = (str.charAt(0) == "/") ? "directory": "file";
  99. // | return prefix + " '" + str + "'";
  100. // | }
  101. // | );
  102. // example:
  103. // Use a formatter
  104. // | // returns "thinger -- howdy"
  105. // | dojo.string.substitute(
  106. // | "${0:postfix}", ["thinger"], null, {
  107. // | postfix: function(value, key){
  108. // | return value + " -- howdy";
  109. // | }
  110. // | }
  111. // | );
  112. thisObject = thisObject || dojo.global;
  113. transform = transform ?
  114. dojo.hitch(thisObject, transform) : function(v){ return v; };
  115. return template.replace(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g,
  116. function(match, key, format){
  117. var value = dojo.getObject(key, false, map);
  118. if(format){
  119. value = dojo.getObject(format, false, thisObject).call(thisObject, value, key);
  120. }
  121. return transform(value, key).toString();
  122. }); // String
  123. };
  124. /*=====
  125. dojo.string.trim = function(str){
  126. // summary:
  127. // Trims whitespace from both sides of the string
  128. // str: String
  129. // String to be trimmed
  130. // returns: String
  131. // Returns the trimmed string
  132. // description:
  133. // This version of trim() was taken from [Steven Levithan's blog](http://blog.stevenlevithan.com/archives/faster-trim-javascript).
  134. // The short yet performant version of this function is dojo.trim(),
  135. // which is part of Dojo base. Uses String.prototype.trim instead, if available.
  136. return ""; // String
  137. }
  138. =====*/
  139. dojo.string.trim = String.prototype.trim ?
  140. dojo.trim : // aliasing to the native function
  141. function(str){
  142. str = str.replace(/^\s+/, '');
  143. for(var i = str.length - 1; i >= 0; i--){
  144. if(/\S/.test(str.charAt(i))){
  145. str = str.substring(0, i + 1);
  146. break;
  147. }
  148. }
  149. return str;
  150. };
  151. }