utils.js 5.3 KB

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