utils.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const specialProperties = ['__proto__', 'constructor', 'prototype'];
  6. /**
  7. * Clones objects
  8. *
  9. * @param {Object} obj the object to clone
  10. * @param {Object} options
  11. * @return {Object} the cloned object
  12. * @api private
  13. */
  14. const clone = exports.clone = function clone(obj, options) {
  15. if (obj === undefined || obj === null)
  16. return obj;
  17. if (Array.isArray(obj))
  18. return exports.cloneArray(obj, options);
  19. if (obj.constructor) {
  20. if (/ObjectI[dD]$/.test(obj.constructor.name)) {
  21. return 'function' == typeof obj.clone
  22. ? obj.clone()
  23. : new obj.constructor(obj.id);
  24. }
  25. if (obj.constructor.name === 'ReadPreference') {
  26. return new obj.constructor(obj.mode, clone(obj.tags, options));
  27. }
  28. if ('Binary' == obj._bsontype && obj.buffer && obj.value) {
  29. return 'function' == typeof obj.clone
  30. ? obj.clone()
  31. : new obj.constructor(obj.value(true), obj.sub_type);
  32. }
  33. if ('Date' === obj.constructor.name || 'Function' === obj.constructor.name)
  34. return new obj.constructor(+obj);
  35. if ('RegExp' === obj.constructor.name)
  36. return new RegExp(obj);
  37. if ('Buffer' === obj.constructor.name)
  38. return Buffer.from(obj);
  39. }
  40. if (isObject(obj))
  41. return exports.cloneObject(obj, options);
  42. if (obj.valueOf)
  43. return obj.valueOf();
  44. };
  45. /*!
  46. * ignore
  47. */
  48. exports.cloneObject = function cloneObject(obj, options) {
  49. const minimize = options && options.minimize,
  50. ret = {},
  51. keys = Object.keys(obj),
  52. len = keys.length;
  53. let hasKeys = false,
  54. val,
  55. k = '',
  56. i = 0;
  57. for (i = 0; i < len; ++i) {
  58. k = keys[i];
  59. // Not technically prototype pollution because this wouldn't merge properties
  60. // onto `Object.prototype`, but avoid properties like __proto__ as a precaution.
  61. if (specialProperties.indexOf(k) !== -1) {
  62. continue;
  63. }
  64. val = clone(obj[k], options);
  65. if (!minimize || ('undefined' !== typeof val)) {
  66. hasKeys || (hasKeys = true);
  67. ret[k] = val;
  68. }
  69. }
  70. return minimize
  71. ? hasKeys && ret
  72. : ret;
  73. };
  74. exports.cloneArray = function cloneArray(arr, options) {
  75. const ret = [],
  76. l = arr.length;
  77. let i = 0;
  78. for (; i < l; i++)
  79. ret.push(clone(arr[i], options));
  80. return ret;
  81. };
  82. /**
  83. * Merges `from` into `to` without overwriting existing properties.
  84. *
  85. * @param {Object} to
  86. * @param {Object} from
  87. * @api private
  88. */
  89. exports.merge = function merge(to, from) {
  90. const keys = Object.keys(from);
  91. for (const key of keys) {
  92. if (specialProperties.indexOf(key) !== -1) {
  93. continue;
  94. }
  95. if ('undefined' === typeof to[key]) {
  96. to[key] = from[key];
  97. } else {
  98. if (exports.isObject(from[key])) {
  99. merge(to[key], from[key]);
  100. } else {
  101. to[key] = from[key];
  102. }
  103. }
  104. }
  105. };
  106. /**
  107. * Same as merge but clones the assigned values.
  108. *
  109. * @param {Object} to
  110. * @param {Object} from
  111. * @api private
  112. */
  113. exports.mergeClone = function mergeClone(to, from) {
  114. const keys = Object.keys(from);
  115. for (const key of keys) {
  116. if (specialProperties.indexOf(key) !== -1) {
  117. continue;
  118. }
  119. if ('undefined' === typeof to[key]) {
  120. to[key] = clone(from[key]);
  121. } else {
  122. if (exports.isObject(from[key])) {
  123. mergeClone(to[key], from[key]);
  124. } else {
  125. to[key] = clone(from[key]);
  126. }
  127. }
  128. }
  129. };
  130. /**
  131. * Read pref helper (mongo 2.2 drivers support this)
  132. *
  133. * Allows using aliases instead of full preference names:
  134. *
  135. * p primary
  136. * pp primaryPreferred
  137. * s secondary
  138. * sp secondaryPreferred
  139. * n nearest
  140. *
  141. * @param {String} pref
  142. */
  143. exports.readPref = function readPref(pref) {
  144. switch (pref) {
  145. case 'p':
  146. pref = 'primary';
  147. break;
  148. case 'pp':
  149. pref = 'primaryPreferred';
  150. break;
  151. case 's':
  152. pref = 'secondary';
  153. break;
  154. case 'sp':
  155. pref = 'secondaryPreferred';
  156. break;
  157. case 'n':
  158. pref = 'nearest';
  159. break;
  160. }
  161. return pref;
  162. };
  163. /**
  164. * Read Concern helper (mongo 3.2 drivers support this)
  165. *
  166. * Allows using string to specify read concern level:
  167. *
  168. * local 3.2+
  169. * available 3.6+
  170. * majority 3.2+
  171. * linearizable 3.4+
  172. * snapshot 4.0+
  173. *
  174. * @param {String|Object} concern
  175. */
  176. exports.readConcern = function readConcern(concern) {
  177. if ('string' === typeof concern) {
  178. switch (concern) {
  179. case 'l':
  180. concern = 'local';
  181. break;
  182. case 'a':
  183. concern = 'available';
  184. break;
  185. case 'm':
  186. concern = 'majority';
  187. break;
  188. case 'lz':
  189. concern = 'linearizable';
  190. break;
  191. case 's':
  192. concern = 'snapshot';
  193. break;
  194. }
  195. concern = { level: concern };
  196. }
  197. return concern;
  198. };
  199. /**
  200. * Object.prototype.toString.call helper
  201. */
  202. const _toString = Object.prototype.toString;
  203. exports.toString = function(arg) {
  204. return _toString.call(arg);
  205. };
  206. /**
  207. * Determines if `arg` is an object.
  208. *
  209. * @param {Object|Array|String|Function|RegExp|any} arg
  210. * @return {Boolean}
  211. */
  212. const isObject = exports.isObject = function(arg) {
  213. return '[object Object]' == exports.toString(arg);
  214. };
  215. /**
  216. * Object.keys helper
  217. */
  218. exports.keys = Object.keys;
  219. /**
  220. * Basic Object.create polyfill.
  221. * Only one argument is supported.
  222. *
  223. * Based on https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create
  224. */
  225. exports.create = 'function' == typeof Object.create
  226. ? Object.create
  227. : create;
  228. function create(proto) {
  229. if (arguments.length > 1) {
  230. throw new Error('Adding properties is not supported');
  231. }
  232. function F() { }
  233. F.prototype = proto;
  234. return new F;
  235. }
  236. /**
  237. * inheritance
  238. */
  239. exports.inherits = function(ctor, superCtor) {
  240. ctor.prototype = exports.create(superCtor.prototype);
  241. ctor.prototype.constructor = ctor;
  242. };
  243. /**
  244. * Check if this object is an arguments object
  245. *
  246. * @param {Any} v
  247. * @return {Boolean}
  248. */
  249. exports.isArgumentsObject = function(v) {
  250. return Object.prototype.toString.call(v) === '[object Arguments]';
  251. };