string.js 5.1 KB

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