租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.3 KiB

3 months ago
  1. 'use strict';
  2. exports.randomString = function randomString(length, charSet) {
  3. var result = [];
  4. length = length || 16;
  5. charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  6. while (length--) {
  7. result.push(charSet[Math.floor(Math.random() * charSet.length)]);
  8. }
  9. return result.join('');
  10. };
  11. /**
  12. * split string to array
  13. * @param {String} str
  14. * @param {String} [sep] default is ','
  15. * @return {Array}
  16. */
  17. exports.split = function split(str, sep) {
  18. str = str || '';
  19. sep = sep || ',';
  20. var items = str.split(sep);
  21. var needs = [];
  22. for (var i = 0; i < items.length; i++) {
  23. var s = items[i].trim();
  24. if (s.length > 0) {
  25. needs.push(s);
  26. }
  27. }
  28. return needs;
  29. };
  30. // always optimized
  31. exports.splitAlwaysOptimized = function splitAlwaysOptimized() {
  32. var str = '';
  33. var sep = ',';
  34. if (arguments.length === 1) {
  35. str = arguments[0] || '';
  36. } else if (arguments.length === 2) {
  37. str = arguments[0] || '';
  38. sep = arguments[1] || ',';
  39. }
  40. var items = str.split(sep);
  41. var needs = [];
  42. for (var i = 0; i < items.length; i++) {
  43. var s = items[i].trim();
  44. if (s.length > 0) {
  45. needs.push(s);
  46. }
  47. }
  48. return needs;
  49. };
  50. /**
  51. * Replace string
  52. *
  53. * @param {String} str
  54. * @param {String|RegExp} substr
  55. * @param {String|Function} newSubstr
  56. * @return {String}
  57. */
  58. exports.replace = function replace(str, substr, newSubstr) {
  59. var replaceFunction = newSubstr;
  60. if (typeof replaceFunction !== 'function') {
  61. replaceFunction = function () {
  62. return newSubstr;
  63. };
  64. }
  65. return str.replace(substr, replaceFunction);
  66. };
  67. // original source https://github.com/nodejs/node/blob/v7.5.0/lib/_http_common.js#L300
  68. /**
  69. * True if val contains an invalid field-vchar
  70. * field-value = *( field-content / obs-fold )
  71. * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
  72. * field-vchar = VCHAR / obs-text
  73. *
  74. * checkInvalidHeaderChar() is currently designed to be inlinable by v8,
  75. * so take care when making changes to the implementation so that the source
  76. * code size does not exceed v8's default max_inlined_source_size setting.
  77. **/
  78. var validHdrChars = [
  79. 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, // 0 - 15
  80. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
  81. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 32 - 47
  82. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 48 - 63
  83. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
  84. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 80 - 95
  85. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
  86. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 112 - 127
  87. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 128 ...
  88. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  89. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  90. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  91. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  92. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  93. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  94. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // ... 255
  95. ];
  96. /**
  97. * Replace invalid http header characters with replacement
  98. *
  99. * @param {String} val
  100. * @param {String|Function} replacement - can be `function(char)`
  101. * @return {Object}
  102. */
  103. exports.replaceInvalidHttpHeaderChar = function replaceInvalidHttpHeaderChar(val, replacement) {
  104. replacement = replacement || ' ';
  105. var invalid = false;
  106. if (!val || typeof val !== 'string') {
  107. return {
  108. val: val,
  109. invalid: invalid,
  110. };
  111. }
  112. var replacementType = typeof replacement;
  113. var chars;
  114. for (var i = 0; i < val.length; ++i) {
  115. if (!validHdrChars[val.charCodeAt(i)]) {
  116. // delay create chars
  117. chars = chars || val.split('');
  118. if (replacementType === 'function') {
  119. chars[i] = replacement(chars[i]);
  120. } else {
  121. chars[i] = replacement;
  122. }
  123. }
  124. }
  125. if (chars) {
  126. val = chars.join('');
  127. invalid = true;
  128. }
  129. return {
  130. val: val,
  131. invalid: invalid,
  132. };
  133. };
  134. /**
  135. * Detect invalid http header characters in a string
  136. *
  137. * @param {String} val
  138. * @return {Boolean}
  139. */
  140. exports.includesInvalidHttpHeaderChar = function includesInvalidHttpHeaderChar(val) {
  141. if (!val || typeof val !== 'string') {
  142. return false;
  143. }
  144. for (var i = 0; i < val.length; ++i) {
  145. if (!validHdrChars[val.charCodeAt(i)]) {
  146. return true;
  147. }
  148. }
  149. return false;
  150. };