lang.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. exports.stringReverse = function(string) {
  33. return string.split("").reverse().join("");
  34. };
  35. exports.stringRepeat = function (string, count) {
  36. var result = '';
  37. while (count > 0) {
  38. if (count & 1)
  39. result += string;
  40. if (count >>= 1)
  41. string += string;
  42. }
  43. return result;
  44. };
  45. var trimBeginRegexp = /^\s\s*/;
  46. var trimEndRegexp = /\s\s*$/;
  47. exports.stringTrimLeft = function (string) {
  48. return string.replace(trimBeginRegexp, '');
  49. };
  50. exports.stringTrimRight = function (string) {
  51. return string.replace(trimEndRegexp, '');
  52. };
  53. exports.copyObject = function(obj) {
  54. var copy = {};
  55. for (var key in obj) {
  56. copy[key] = obj[key];
  57. }
  58. return copy;
  59. };
  60. exports.copyArray = function(array){
  61. var copy = [];
  62. for (var i=0, l=array.length; i<l; i++) {
  63. if (array[i] && typeof array[i] == "object")
  64. copy[i] = this.copyObject( array[i] );
  65. else
  66. copy[i] = array[i];
  67. }
  68. return copy;
  69. };
  70. exports.deepCopy = function (obj) {
  71. if (typeof obj !== "object" || !obj)
  72. return obj;
  73. var cons = obj.constructor;
  74. if (cons === RegExp)
  75. return obj;
  76. var copy = cons();
  77. for (var key in obj) {
  78. if (typeof obj[key] === "object") {
  79. copy[key] = exports.deepCopy(obj[key]);
  80. } else {
  81. copy[key] = obj[key];
  82. }
  83. }
  84. return copy;
  85. };
  86. exports.arrayToMap = function(arr) {
  87. var map = {};
  88. for (var i=0; i<arr.length; i++) {
  89. map[arr[i]] = 1;
  90. }
  91. return map;
  92. };
  93. exports.createMap = function(props) {
  94. var map = Object.create(null);
  95. for (var i in props) {
  96. map[i] = props[i];
  97. }
  98. return map;
  99. };
  100. /*
  101. * splice out of 'array' anything that === 'value'
  102. */
  103. exports.arrayRemove = function(array, value) {
  104. for (var i = 0; i <= array.length; i++) {
  105. if (value === array[i]) {
  106. array.splice(i, 1);
  107. }
  108. }
  109. };
  110. exports.escapeRegExp = function(str) {
  111. return str.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
  112. };
  113. exports.escapeHTML = function(str) {
  114. return str.replace(/&/g, "&#38;").replace(/"/g, "&#34;").replace(/'/g, "&#39;").replace(/</g, "&#60;");
  115. };
  116. exports.getMatchOffsets = function(string, regExp) {
  117. var matches = [];
  118. string.replace(regExp, function(str) {
  119. matches.push({
  120. offset: arguments[arguments.length-2],
  121. length: str.length
  122. });
  123. });
  124. return matches;
  125. };
  126. /* deprecated */
  127. exports.deferredCall = function(fcn) {
  128. var timer = null;
  129. var callback = function() {
  130. timer = null;
  131. fcn();
  132. };
  133. var deferred = function(timeout) {
  134. deferred.cancel();
  135. timer = setTimeout(callback, timeout || 0);
  136. return deferred;
  137. };
  138. deferred.schedule = deferred;
  139. deferred.call = function() {
  140. this.cancel();
  141. fcn();
  142. return deferred;
  143. };
  144. deferred.cancel = function() {
  145. clearTimeout(timer);
  146. timer = null;
  147. return deferred;
  148. };
  149. deferred.isPending = function() {
  150. return timer;
  151. };
  152. return deferred;
  153. };
  154. exports.delayedCall = function(fcn, defaultTimeout) {
  155. var timer = null;
  156. var callback = function() {
  157. timer = null;
  158. fcn();
  159. };
  160. var _self = function(timeout) {
  161. if (timer == null)
  162. timer = setTimeout(callback, timeout || defaultTimeout);
  163. };
  164. _self.delay = function(timeout) {
  165. timer && clearTimeout(timer);
  166. timer = setTimeout(callback, timeout || defaultTimeout);
  167. };
  168. _self.schedule = _self;
  169. _self.call = function() {
  170. this.cancel();
  171. fcn();
  172. };
  173. _self.cancel = function() {
  174. timer && clearTimeout(timer);
  175. timer = null;
  176. };
  177. _self.isPending = function() {
  178. return timer;
  179. };
  180. return _self;
  181. };
  182. });