Ohm-Management - Projektarbeit B-ME
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.

utils.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. 'use strict';
  2. var has = Object.prototype.hasOwnProperty;
  3. var isArray = Array.isArray;
  4. var hexTable = (function () {
  5. var array = [];
  6. for (var i = 0; i < 256; ++i) {
  7. array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
  8. }
  9. return array;
  10. }());
  11. var compactQueue = function compactQueue(queue) {
  12. while (queue.length > 1) {
  13. var item = queue.pop();
  14. var obj = item.obj[item.prop];
  15. if (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. };
  26. var arrayToObject = function arrayToObject(source, options) {
  27. var obj = options && options.plainObjects ? Object.create(null) : {};
  28. for (var i = 0; i < source.length; ++i) {
  29. if (typeof source[i] !== 'undefined') {
  30. obj[i] = source[i];
  31. }
  32. }
  33. return obj;
  34. };
  35. var merge = function merge(target, source, options) {
  36. if (!source) {
  37. return target;
  38. }
  39. if (typeof source !== 'object') {
  40. if (isArray(target)) {
  41. target.push(source);
  42. } else if (target && typeof target === 'object') {
  43. if ((options && (options.plainObjects || options.allowPrototypes)) || !has.call(Object.prototype, source)) {
  44. target[source] = true;
  45. }
  46. } else {
  47. return [target, source];
  48. }
  49. return target;
  50. }
  51. if (!target || typeof target !== 'object') {
  52. return [target].concat(source);
  53. }
  54. var mergeTarget = target;
  55. if (isArray(target) && !isArray(source)) {
  56. mergeTarget = arrayToObject(target, options);
  57. }
  58. if (isArray(target) && isArray(source)) {
  59. source.forEach(function (item, i) {
  60. if (has.call(target, i)) {
  61. var targetItem = target[i];
  62. if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {
  63. target[i] = merge(targetItem, 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] = merge(acc[key], value, options);
  77. } else {
  78. acc[key] = value;
  79. }
  80. return acc;
  81. }, mergeTarget);
  82. };
  83. var 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. var decode = function (str, decoder, charset) {
  90. var strWithoutPlus = str.replace(/\+/g, ' ');
  91. if (charset === 'iso-8859-1') {
  92. // unescape never throws, no try...catch needed:
  93. return strWithoutPlus.replace(/%[0-9a-f]{2}/gi, unescape);
  94. }
  95. // utf-8
  96. try {
  97. return decodeURIComponent(strWithoutPlus);
  98. } catch (e) {
  99. return strWithoutPlus;
  100. }
  101. };
  102. var encode = function encode(str, defaultEncoder, charset) {
  103. // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
  104. // It has been adapted here for stricter adherence to RFC 3986
  105. if (str.length === 0) {
  106. return str;
  107. }
  108. var string = typeof str === 'string' ? str : String(str);
  109. if (charset === 'iso-8859-1') {
  110. return escape(string).replace(/%u[0-9a-f]{4}/gi, function ($0) {
  111. return '%26%23' + parseInt($0.slice(2), 16) + '%3B';
  112. });
  113. }
  114. var out = '';
  115. for (var i = 0; i < string.length; ++i) {
  116. var c = string.charCodeAt(i);
  117. if (
  118. c === 0x2D // -
  119. || c === 0x2E // .
  120. || c === 0x5F // _
  121. || c === 0x7E // ~
  122. || (c >= 0x30 && c <= 0x39) // 0-9
  123. || (c >= 0x41 && c <= 0x5A) // a-z
  124. || (c >= 0x61 && c <= 0x7A) // A-Z
  125. ) {
  126. out += string.charAt(i);
  127. continue;
  128. }
  129. if (c < 0x80) {
  130. out = out + hexTable[c];
  131. continue;
  132. }
  133. if (c < 0x800) {
  134. out = out + (hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]);
  135. continue;
  136. }
  137. if (c < 0xD800 || c >= 0xE000) {
  138. out = out + (hexTable[0xE0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
  139. continue;
  140. }
  141. i += 1;
  142. c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
  143. out += hexTable[0xF0 | (c >> 18)]
  144. + hexTable[0x80 | ((c >> 12) & 0x3F)]
  145. + hexTable[0x80 | ((c >> 6) & 0x3F)]
  146. + hexTable[0x80 | (c & 0x3F)];
  147. }
  148. return out;
  149. };
  150. var compact = function compact(value) {
  151. var queue = [{ obj: { o: value }, prop: 'o' }];
  152. var refs = [];
  153. for (var i = 0; i < queue.length; ++i) {
  154. var item = queue[i];
  155. var obj = item.obj[item.prop];
  156. var keys = Object.keys(obj);
  157. for (var j = 0; j < keys.length; ++j) {
  158. var key = keys[j];
  159. var val = obj[key];
  160. if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {
  161. queue.push({ obj: obj, prop: key });
  162. refs.push(val);
  163. }
  164. }
  165. }
  166. compactQueue(queue);
  167. return value;
  168. };
  169. var isRegExp = function isRegExp(obj) {
  170. return Object.prototype.toString.call(obj) === '[object RegExp]';
  171. };
  172. var isBuffer = function isBuffer(obj) {
  173. if (!obj || typeof obj !== 'object') {
  174. return false;
  175. }
  176. return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
  177. };
  178. var combine = function combine(a, b) {
  179. return [].concat(a, b);
  180. };
  181. module.exports = {
  182. arrayToObject: arrayToObject,
  183. assign: assign,
  184. combine: combine,
  185. compact: compact,
  186. decode: decode,
  187. encode: encode,
  188. isBuffer: isBuffer,
  189. isRegExp: isRegExp,
  190. merge: merge
  191. };