lang.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.last = function(a) {
  33. return a[a.length - 1];
  34. };
  35. exports.stringReverse = function(string) {
  36. return string.split("").reverse().join("");
  37. };
  38. exports.stringRepeat = function (string, count) {
  39. var result = '';
  40. while (count > 0) {
  41. if (count & 1)
  42. result += string;
  43. if (count >>= 1)
  44. string += string;
  45. }
  46. return result;
  47. };
  48. var trimBeginRegexp = /^\s\s*/;
  49. var trimEndRegexp = /\s\s*$/;
  50. exports.stringTrimLeft = function (string) {
  51. return string.replace(trimBeginRegexp, '');
  52. };
  53. exports.stringTrimRight = function (string) {
  54. return string.replace(trimEndRegexp, '');
  55. };
  56. exports.copyObject = function(obj) {
  57. var copy = {};
  58. for (var key in obj) {
  59. copy[key] = obj[key];
  60. }
  61. return copy;
  62. };
  63. exports.copyArray = function(array){
  64. var copy = [];
  65. for (var i=0, l=array.length; i<l; i++) {
  66. if (array[i] && typeof array[i] == "object")
  67. copy[i] = this.copyObject( array[i] );
  68. else
  69. copy[i] = array[i];
  70. }
  71. return copy;
  72. };
  73. exports.deepCopy = function deepCopy(obj) {
  74. if (typeof obj !== "object" || !obj)
  75. return obj;
  76. var copy;
  77. if (Array.isArray(obj)) {
  78. copy = [];
  79. for (var key = 0; key < obj.length; key++) {
  80. copy[key] = deepCopy(obj[key]);
  81. }
  82. return copy;
  83. }
  84. var cons = obj.constructor;
  85. if (cons === RegExp)
  86. return obj;
  87. copy = cons();
  88. for (var key in obj) {
  89. copy[key] = deepCopy(obj[key]);
  90. }
  91. return copy;
  92. };
  93. exports.arrayToMap = function(arr) {
  94. var map = {};
  95. for (var i=0; i<arr.length; i++) {
  96. map[arr[i]] = 1;
  97. }
  98. return map;
  99. };
  100. exports.createMap = function(props) {
  101. var map = Object.create(null);
  102. for (var i in props) {
  103. map[i] = props[i];
  104. }
  105. return map;
  106. };
  107. /*
  108. * splice out of 'array' anything that === 'value'
  109. */
  110. exports.arrayRemove = function(array, value) {
  111. for (var i = 0; i <= array.length; i++) {
  112. if (value === array[i]) {
  113. array.splice(i, 1);
  114. }
  115. }
  116. };
  117. exports.escapeRegExp = function(str) {
  118. return str.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
  119. };
  120. exports.escapeHTML = function(str) {
  121. return str.replace(/&/g, "&#38;").replace(/"/g, "&#34;").replace(/'/g, "&#39;").replace(/</g, "&#60;");
  122. };
  123. exports.getMatchOffsets = function(string, regExp) {
  124. var matches = [];
  125. string.replace(regExp, function(str) {
  126. matches.push({
  127. offset: arguments[arguments.length-2],
  128. length: str.length
  129. });
  130. });
  131. return matches;
  132. };
  133. /* deprecated */
  134. exports.deferredCall = function(fcn) {
  135. var timer = null;
  136. var callback = function() {
  137. timer = null;
  138. fcn();
  139. };
  140. var deferred = function(timeout) {
  141. deferred.cancel();
  142. timer = setTimeout(callback, timeout || 0);
  143. return deferred;
  144. };
  145. deferred.schedule = deferred;
  146. deferred.call = function() {
  147. this.cancel();
  148. fcn();
  149. return deferred;
  150. };
  151. deferred.cancel = function() {
  152. clearTimeout(timer);
  153. timer = null;
  154. return deferred;
  155. };
  156. deferred.isPending = function() {
  157. return timer;
  158. };
  159. return deferred;
  160. };
  161. exports.delayedCall = function(fcn, defaultTimeout) {
  162. var timer = null;
  163. var callback = function() {
  164. timer = null;
  165. fcn();
  166. };
  167. var _self = function(timeout) {
  168. if (timer == null)
  169. timer = setTimeout(callback, timeout || defaultTimeout);
  170. };
  171. _self.delay = function(timeout) {
  172. timer && clearTimeout(timer);
  173. timer = setTimeout(callback, timeout || defaultTimeout);
  174. };
  175. _self.schedule = _self;
  176. _self.call = function() {
  177. this.cancel();
  178. fcn();
  179. };
  180. _self.cancel = function() {
  181. timer && clearTimeout(timer);
  182. timer = null;
  183. };
  184. _self.isPending = function() {
  185. return timer;
  186. };
  187. return _self;
  188. };
  189. });