string.js 4.7 KB

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