util.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Some utility functions in js
  2. var u = (module.exports = {
  3. array: {
  4. // Returns a copy of the array with the value removed once
  5. //
  6. // [1, 2, 3, 1].del 1 #=> [2, 3, 1]
  7. // [1, 2, 3].del 4 #=> [1, 2, 3]
  8. del: function (arr, val) {
  9. var index = arr.indexOf(val);
  10. if (index != -1) {
  11. if (index == 0) {
  12. return arr.slice(1);
  13. } else {
  14. return arr.slice(0, index).concat(arr.slice(index + 1));
  15. }
  16. } else {
  17. return arr;
  18. }
  19. },
  20. // Returns the first element of the array
  21. //
  22. // [1, 2, 3].first() #=> 1
  23. first: function (arr) {
  24. return arr[0];
  25. },
  26. // Returns the last element of the array
  27. //
  28. // [1, 2, 3].last() #=> 3
  29. last: function (arr) {
  30. return arr[arr.length - 1];
  31. },
  32. },
  33. string: {
  34. // Returns a copy of str with all occurrences of pattern replaced with either replacement or the return value of a function.
  35. // The pattern will typically be a Regexp; if it is a String then no regular expression metacharacters will be interpreted
  36. // (that is /\d/ will match a digit, but ‘\d’ will match a backslash followed by a ‘d’).
  37. //
  38. // In the function form, the current match object is passed in as a parameter to the function, and variables such as
  39. // $[1], $[2], $[3] (where $ is the match object) will be set appropriately. The value returned by the function will be
  40. // substituted for the match on each call.
  41. //
  42. // The result inherits any tainting in the original string or any supplied replacement string.
  43. //
  44. // "hello".gsub /[aeiou]/, '*' #=> "h*ll*"
  45. // "hello".gsub /[aeiou]/, '<$1>' #=> "h<e>ll<o>"
  46. // "hello".gsub /[aeiou]/, ($) {
  47. // "<#{$[1]}>" #=> "h<e>ll<o>"
  48. //
  49. gsub: function (str, pattern, replacement) {
  50. var i, match, matchCmpr, matchCmprPrev, replacementStr, result, self;
  51. if (!(pattern != null && replacement != null)) return u.string.value(str);
  52. result = '';
  53. self = str;
  54. while (self.length > 0) {
  55. if ((match = self.match(pattern))) {
  56. result += self.slice(0, match.index);
  57. if (typeof replacement === 'function') {
  58. match[1] = match[1] || match[0];
  59. result += replacement(match);
  60. } else if (replacement.match(/\$[1-9]/)) {
  61. matchCmprPrev = match;
  62. matchCmpr = u.array.del(match, void 0);
  63. while (matchCmpr !== matchCmprPrev) {
  64. matchCmprPrev = matchCmpr;
  65. matchCmpr = u.array.del(matchCmpr, void 0);
  66. }
  67. match[1] = match[1] || match[0];
  68. replacementStr = replacement;
  69. for (i = 1; i <= 9; i++) {
  70. if (matchCmpr[i]) {
  71. replacementStr = u.string.gsub(replacementStr, new RegExp('\\$' + i), matchCmpr[i]);
  72. }
  73. }
  74. result += replacementStr;
  75. } else {
  76. result += replacement;
  77. }
  78. self = self.slice(match.index + match[0].length);
  79. } else {
  80. result += self;
  81. self = '';
  82. }
  83. }
  84. return result;
  85. },
  86. // Returns a copy of the String with the first letter being upper case
  87. //
  88. // "hello".upcase #=> "Hello"
  89. upcase: function (str) {
  90. var self = u.string.gsub(str, /_([a-z])/, function ($) {
  91. return '_' + $[1].toUpperCase();
  92. });
  93. self = u.string.gsub(self, /\/([a-z])/, function ($) {
  94. return '/' + $[1].toUpperCase();
  95. });
  96. return self[0].toUpperCase() + self.substr(1);
  97. },
  98. // Returns a copy of capitalized string
  99. //
  100. // "employee salary" #=> "Employee Salary"
  101. capitalize: function (str, spaces) {
  102. if (!str.length) {
  103. return str;
  104. }
  105. var self = str.toLowerCase();
  106. if (!spaces) {
  107. self = u.string.gsub(self, /\s([a-z])/, function ($) {
  108. return ' ' + $[1].toUpperCase();
  109. });
  110. }
  111. return self[0].toUpperCase() + self.substr(1);
  112. },
  113. // Returns a copy of the String with the first letter being lower case
  114. //
  115. // "HELLO".downcase #=> "hELLO"
  116. downcase: function (str) {
  117. var self = u.string.gsub(str, /_([A-Z])/, function ($) {
  118. return '_' + $[1].toLowerCase();
  119. });
  120. self = u.string.gsub(self, /\/([A-Z])/, function ($) {
  121. return '/' + $[1].toLowerCase();
  122. });
  123. return self[0].toLowerCase() + self.substr(1);
  124. },
  125. // Returns a string value for the String object
  126. //
  127. // "hello".value() #=> "hello"
  128. value: function (str) {
  129. return str.substr(0);
  130. },
  131. },
  132. });