utils.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use strict';
  2. var has = Object.prototype.hasOwnProperty;
  3. var hexTable = (function () {
  4. var array = [];
  5. for (var i = 0; i < 256; ++i) {
  6. array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
  7. }
  8. return array;
  9. }());
  10. exports.arrayToObject = function (source, options) {
  11. var obj = options && options.plainObjects ? Object.create(null) : {};
  12. for (var i = 0; i < source.length; ++i) {
  13. if (typeof source[i] !== 'undefined') {
  14. obj[i] = source[i];
  15. }
  16. }
  17. return obj;
  18. };
  19. exports.merge = function (target, source, options) {
  20. if (!source) {
  21. return target;
  22. }
  23. if (typeof source !== 'object') {
  24. if (Array.isArray(target)) {
  25. target.push(source);
  26. } else if (typeof target === 'object') {
  27. target[source] = true;
  28. } else {
  29. return [target, source];
  30. }
  31. return target;
  32. }
  33. if (typeof target !== 'object') {
  34. return [target].concat(source);
  35. }
  36. var mergeTarget = target;
  37. if (Array.isArray(target) && !Array.isArray(source)) {
  38. mergeTarget = exports.arrayToObject(target, options);
  39. }
  40. if (Array.isArray(target) && Array.isArray(source)) {
  41. source.forEach(function (item, i) {
  42. if (has.call(target, i)) {
  43. if (target[i] && typeof target[i] === 'object') {
  44. target[i] = exports.merge(target[i], item, options);
  45. } else {
  46. target.push(item);
  47. }
  48. } else {
  49. target[i] = item;
  50. }
  51. });
  52. return target;
  53. }
  54. return Object.keys(source).reduce(function (acc, key) {
  55. var value = source[key];
  56. if (Object.prototype.hasOwnProperty.call(acc, key)) {
  57. acc[key] = exports.merge(acc[key], value, options);
  58. } else {
  59. acc[key] = value;
  60. }
  61. return acc;
  62. }, mergeTarget);
  63. };
  64. exports.decode = function (str) {
  65. try {
  66. return decodeURIComponent(str.replace(/\+/g, ' '));
  67. } catch (e) {
  68. return str;
  69. }
  70. };
  71. exports.encode = function (str) {
  72. // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
  73. // It has been adapted here for stricter adherence to RFC 3986
  74. if (str.length === 0) {
  75. return str;
  76. }
  77. var string = typeof str === 'string' ? str : String(str);
  78. var out = '';
  79. for (var i = 0; i < string.length; ++i) {
  80. var c = string.charCodeAt(i);
  81. if (
  82. c === 0x2D || // -
  83. c === 0x2E || // .
  84. c === 0x5F || // _
  85. c === 0x7E || // ~
  86. (c >= 0x30 && c <= 0x39) || // 0-9
  87. (c >= 0x41 && c <= 0x5A) || // a-z
  88. (c >= 0x61 && c <= 0x7A) // A-Z
  89. ) {
  90. out += string.charAt(i);
  91. continue;
  92. }
  93. if (c < 0x80) {
  94. out = out + hexTable[c];
  95. continue;
  96. }
  97. if (c < 0x800) {
  98. out = out + (hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]);
  99. continue;
  100. }
  101. if (c < 0xD800 || c >= 0xE000) {
  102. out = out + (hexTable[0xE0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
  103. continue;
  104. }
  105. i += 1;
  106. c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
  107. out += hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)];
  108. }
  109. return out;
  110. };
  111. exports.compact = function (obj, references) {
  112. if (typeof obj !== 'object' || obj === null) {
  113. return obj;
  114. }
  115. var refs = references || [];
  116. var lookup = refs.indexOf(obj);
  117. if (lookup !== -1) {
  118. return refs[lookup];
  119. }
  120. refs.push(obj);
  121. if (Array.isArray(obj)) {
  122. var compacted = [];
  123. for (var i = 0; i < obj.length; ++i) {
  124. if (obj[i] && typeof obj[i] === 'object') {
  125. compacted.push(exports.compact(obj[i], refs));
  126. } else if (typeof obj[i] !== 'undefined') {
  127. compacted.push(obj[i]);
  128. }
  129. }
  130. return compacted;
  131. }
  132. var keys = Object.keys(obj);
  133. keys.forEach(function (key) {
  134. obj[key] = exports.compact(obj[key], refs);
  135. });
  136. return obj;
  137. };
  138. exports.isRegExp = function (obj) {
  139. return Object.prototype.toString.call(obj) === '[object RegExp]';
  140. };
  141. exports.isBuffer = function (obj) {
  142. if (obj === null || typeof obj === 'undefined') {
  143. return false;
  144. }
  145. return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
  146. };