PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/fabricator-assemble/node_modules/i/lib/util.js

https://gitlab.com/jeaster12/fabby
JavaScript | 136 lines | 91 code | 7 blank | 38 comment | 23 complexity | c8bde1e81286fd96b5cfc9be67e45f67 MD5 | raw file
  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. var self = str.toLowerCase();
  103. if(!spaces) {
  104. self = u.string.gsub(self, /\s([a-z])/, function ($) {
  105. return " " + $[1].toUpperCase();
  106. });
  107. }
  108. return self[0].toUpperCase() + self.substr(1);
  109. },
  110. // Returns a copy of the String with the first letter being lower case
  111. //
  112. // "HELLO".downcase #=> "hELLO"
  113. downcase: function(str) {
  114. var self = u.string.gsub(str, /_([A-Z])/, function ($) {
  115. return "_" + $[1].toLowerCase();
  116. });
  117. self = u.string.gsub(self, /\/([A-Z])/, function ($) {
  118. return "/" + $[1].toLowerCase();
  119. });
  120. return self[0].toLowerCase() + self.substr(1);
  121. },
  122. // Returns a string value for the String object
  123. //
  124. // "hello".value() #=> "hello"
  125. value: function (str) {
  126. return str.substr(0);
  127. }
  128. }
  129. }