utils.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const ms = require('ms');
  6. const mpath = require('mpath');
  7. const sliced = require('sliced');
  8. const Buffer = require('safe-buffer').Buffer;
  9. const Decimal = require('./types/decimal128');
  10. const ObjectId = require('./types/objectid');
  11. const PopulateOptions = require('./options/PopulateOptions');
  12. const clone = require('./helpers/clone');
  13. const isObject = require('./helpers/isObject');
  14. const isBsonType = require('./helpers/isBsonType');
  15. const getFunctionName = require('./helpers/getFunctionName');
  16. const isMongooseObject = require('./helpers/isMongooseObject');
  17. const promiseOrCallback = require('./helpers/promiseOrCallback');
  18. const specialProperties = require('./helpers/specialProperties');
  19. let Document;
  20. exports.specialProperties = specialProperties;
  21. /*!
  22. * Produces a collection name from model `name`. By default, just returns
  23. * the model name
  24. *
  25. * @param {String} name a model name
  26. * @param {Function} pluralize function that pluralizes the collection name
  27. * @return {String} a collection name
  28. * @api private
  29. */
  30. exports.toCollectionName = function(name, pluralize) {
  31. if (name === 'system.profile') {
  32. return name;
  33. }
  34. if (name === 'system.indexes') {
  35. return name;
  36. }
  37. if (typeof pluralize === 'function') {
  38. return pluralize(name);
  39. }
  40. return name;
  41. };
  42. /*!
  43. * Determines if `a` and `b` are deep equal.
  44. *
  45. * Modified from node/lib/assert.js
  46. *
  47. * @param {any} a a value to compare to `b`
  48. * @param {any} b a value to compare to `a`
  49. * @return {Boolean}
  50. * @api private
  51. */
  52. exports.deepEqual = function deepEqual(a, b) {
  53. if (a === b) {
  54. return true;
  55. }
  56. if (a instanceof Date && b instanceof Date) {
  57. return a.getTime() === b.getTime();
  58. }
  59. if ((isBsonType(a, 'ObjectID') && isBsonType(b, 'ObjectID')) ||
  60. (isBsonType(a, 'Decimal128') && isBsonType(b, 'Decimal128'))) {
  61. return a.toString() === b.toString();
  62. }
  63. if (a instanceof RegExp && b instanceof RegExp) {
  64. return a.source === b.source &&
  65. a.ignoreCase === b.ignoreCase &&
  66. a.multiline === b.multiline &&
  67. a.global === b.global;
  68. }
  69. if (typeof a !== 'object' && typeof b !== 'object') {
  70. return a == b;
  71. }
  72. if (a === null || b === null || a === undefined || b === undefined) {
  73. return false;
  74. }
  75. if (a.prototype !== b.prototype) {
  76. return false;
  77. }
  78. // Handle MongooseNumbers
  79. if (a instanceof Number && b instanceof Number) {
  80. return a.valueOf() === b.valueOf();
  81. }
  82. if (Buffer.isBuffer(a)) {
  83. return exports.buffer.areEqual(a, b);
  84. }
  85. if (isMongooseObject(a)) {
  86. a = a.toObject();
  87. }
  88. if (isMongooseObject(b)) {
  89. b = b.toObject();
  90. }
  91. let ka;
  92. let kb;
  93. let key;
  94. let i;
  95. try {
  96. ka = Object.keys(a);
  97. kb = Object.keys(b);
  98. } catch (e) {
  99. // happens when one is a string literal and the other isn't
  100. return false;
  101. }
  102. // having the same number of owned properties (keys incorporates
  103. // hasOwnProperty)
  104. if (ka.length !== kb.length) {
  105. return false;
  106. }
  107. // the same set of keys (although not necessarily the same order),
  108. ka.sort();
  109. kb.sort();
  110. // ~~~cheap key test
  111. for (i = ka.length - 1; i >= 0; i--) {
  112. if (ka[i] !== kb[i]) {
  113. return false;
  114. }
  115. }
  116. // equivalent values for every corresponding key, and
  117. // ~~~possibly expensive deep test
  118. for (i = ka.length - 1; i >= 0; i--) {
  119. key = ka[i];
  120. if (!deepEqual(a[key], b[key])) {
  121. return false;
  122. }
  123. }
  124. return true;
  125. };
  126. /*!
  127. * Get the last element of an array
  128. */
  129. exports.last = function(arr) {
  130. if (arr.length > 0) {
  131. return arr[arr.length - 1];
  132. }
  133. return void 0;
  134. };
  135. exports.clone = clone;
  136. /*!
  137. * ignore
  138. */
  139. exports.promiseOrCallback = promiseOrCallback;
  140. /*!
  141. * ignore
  142. */
  143. exports.omit = function omit(obj, keys) {
  144. if (keys == null) {
  145. return Object.assign({}, obj);
  146. }
  147. if (!Array.isArray(keys)) {
  148. keys = [keys];
  149. }
  150. const ret = Object.assign({}, obj);
  151. for (const key of keys) {
  152. delete ret[key];
  153. }
  154. return ret;
  155. };
  156. /*!
  157. * Shallow copies defaults into options.
  158. *
  159. * @param {Object} defaults
  160. * @param {Object} options
  161. * @return {Object} the merged object
  162. * @api private
  163. */
  164. exports.options = function(defaults, options) {
  165. const keys = Object.keys(defaults);
  166. let i = keys.length;
  167. let k;
  168. options = options || {};
  169. while (i--) {
  170. k = keys[i];
  171. if (!(k in options)) {
  172. options[k] = defaults[k];
  173. }
  174. }
  175. return options;
  176. };
  177. /*!
  178. * Generates a random string
  179. *
  180. * @api private
  181. */
  182. exports.random = function() {
  183. return Math.random().toString().substr(3);
  184. };
  185. /*!
  186. * Merges `from` into `to` without overwriting existing properties.
  187. *
  188. * @param {Object} to
  189. * @param {Object} from
  190. * @api private
  191. */
  192. exports.merge = function merge(to, from, options, path) {
  193. options = options || {};
  194. const keys = Object.keys(from);
  195. let i = 0;
  196. const len = keys.length;
  197. let key;
  198. path = path || '';
  199. const omitNested = options.omitNested || {};
  200. while (i < len) {
  201. key = keys[i++];
  202. if (options.omit && options.omit[key]) {
  203. continue;
  204. }
  205. if (omitNested[path]) {
  206. continue;
  207. }
  208. if (specialProperties.has(key)) {
  209. continue;
  210. }
  211. if (to[key] == null) {
  212. to[key] = from[key];
  213. } else if (exports.isObject(from[key])) {
  214. if (!exports.isObject(to[key])) {
  215. to[key] = {};
  216. }
  217. if (from[key] != null) {
  218. if (from[key].instanceOfSchema) {
  219. if (to[key].instanceOfSchema) {
  220. to[key].add(from[key].clone());
  221. } else {
  222. to[key] = from[key].clone();
  223. }
  224. continue;
  225. } else if (from[key] instanceof ObjectId) {
  226. to[key] = new ObjectId(from[key]);
  227. continue;
  228. }
  229. }
  230. merge(to[key], from[key], options, path ? path + '.' + key : key);
  231. } else if (options.overwrite) {
  232. to[key] = from[key];
  233. }
  234. }
  235. };
  236. /*!
  237. * Applies toObject recursively.
  238. *
  239. * @param {Document|Array|Object} obj
  240. * @return {Object}
  241. * @api private
  242. */
  243. exports.toObject = function toObject(obj) {
  244. Document || (Document = require('./document'));
  245. let ret;
  246. if (obj == null) {
  247. return obj;
  248. }
  249. if (obj instanceof Document) {
  250. return obj.toObject();
  251. }
  252. if (Array.isArray(obj)) {
  253. ret = [];
  254. for (const doc of obj) {
  255. ret.push(toObject(doc));
  256. }
  257. return ret;
  258. }
  259. if (exports.isPOJO(obj)) {
  260. ret = {};
  261. for (const k in obj) {
  262. if (specialProperties.has(k)) {
  263. continue;
  264. }
  265. ret[k] = toObject(obj[k]);
  266. }
  267. return ret;
  268. }
  269. return obj;
  270. };
  271. exports.isObject = isObject;
  272. /*!
  273. * Determines if `arg` is a plain old JavaScript object (POJO). Specifically,
  274. * `arg` must be an object but not an instance of any special class, like String,
  275. * ObjectId, etc.
  276. *
  277. * `Object.getPrototypeOf()` is part of ES5: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf
  278. *
  279. * @param {Object|Array|String|Function|RegExp|any} arg
  280. * @api private
  281. * @return {Boolean}
  282. */
  283. exports.isPOJO = function isPOJO(arg) {
  284. if (arg == null || typeof arg !== 'object') {
  285. return false;
  286. }
  287. const proto = Object.getPrototypeOf(arg);
  288. // Prototype may be null if you used `Object.create(null)`
  289. // Checking `proto`'s constructor is safe because `getPrototypeOf()`
  290. // explicitly crosses the boundary from object data to object metadata
  291. return !proto || proto.constructor.name === 'Object';
  292. };
  293. /*!
  294. * Determines if `obj` is a built-in object like an array, date, boolean,
  295. * etc.
  296. */
  297. exports.isNativeObject = function(arg) {
  298. return Array.isArray(arg) ||
  299. arg instanceof Date ||
  300. arg instanceof Boolean ||
  301. arg instanceof Number ||
  302. arg instanceof String;
  303. };
  304. /*!
  305. * Determines if `val` is an object that has no own keys
  306. */
  307. exports.isEmptyObject = function(val) {
  308. return val != null &&
  309. typeof val === 'object' &&
  310. Object.keys(val).length === 0;
  311. };
  312. /*!
  313. * Search if `obj` or any POJOs nested underneath `obj` has a property named
  314. * `key`
  315. */
  316. exports.hasKey = function hasKey(obj, key) {
  317. const props = Object.keys(obj);
  318. for (const prop of props) {
  319. if (prop === key) {
  320. return true;
  321. }
  322. if (exports.isPOJO(obj[prop]) && exports.hasKey(obj[prop], key)) {
  323. return true;
  324. }
  325. }
  326. return false;
  327. };
  328. /*!
  329. * A faster Array.prototype.slice.call(arguments) alternative
  330. * @api private
  331. */
  332. exports.args = sliced;
  333. /*!
  334. * process.nextTick helper.
  335. *
  336. * Wraps `callback` in a try/catch + nextTick.
  337. *
  338. * node-mongodb-native has a habit of state corruption when an error is immediately thrown from within a collection callback.
  339. *
  340. * @param {Function} callback
  341. * @api private
  342. */
  343. exports.tick = function tick(callback) {
  344. if (typeof callback !== 'function') {
  345. return;
  346. }
  347. return function() {
  348. try {
  349. callback.apply(this, arguments);
  350. } catch (err) {
  351. // only nextTick on err to get out of
  352. // the event loop and avoid state corruption.
  353. process.nextTick(function() {
  354. throw err;
  355. });
  356. }
  357. };
  358. };
  359. /*!
  360. * Returns true if `v` is an object that can be serialized as a primitive in
  361. * MongoDB
  362. */
  363. exports.isMongooseType = function(v) {
  364. return v instanceof ObjectId || v instanceof Decimal || v instanceof Buffer;
  365. };
  366. exports.isMongooseObject = isMongooseObject;
  367. /*!
  368. * Converts `expires` options of index objects to `expiresAfterSeconds` options for MongoDB.
  369. *
  370. * @param {Object} object
  371. * @api private
  372. */
  373. exports.expires = function expires(object) {
  374. if (!(object && object.constructor.name === 'Object')) {
  375. return;
  376. }
  377. if (!('expires' in object)) {
  378. return;
  379. }
  380. let when;
  381. if (typeof object.expires !== 'string') {
  382. when = object.expires;
  383. } else {
  384. when = Math.round(ms(object.expires) / 1000);
  385. }
  386. object.expireAfterSeconds = when;
  387. delete object.expires;
  388. };
  389. /*!
  390. * populate helper
  391. */
  392. exports.populate = function populate(path, select, model, match, options, subPopulate, justOne, count) {
  393. // might have passed an object specifying all arguments
  394. let obj = null;
  395. if (arguments.length === 1) {
  396. if (path instanceof PopulateOptions) {
  397. return [path];
  398. }
  399. if (Array.isArray(path)) {
  400. const singles = makeSingles(path);
  401. return singles.map(o => exports.populate(o)[0]);
  402. }
  403. if (exports.isObject(path)) {
  404. obj = Object.assign({}, path);
  405. } else {
  406. obj = { path: path };
  407. }
  408. } else if (typeof model === 'object') {
  409. obj = {
  410. path: path,
  411. select: select,
  412. match: model,
  413. options: match
  414. };
  415. } else {
  416. obj = {
  417. path: path,
  418. select: select,
  419. model: model,
  420. match: match,
  421. options: options,
  422. populate: subPopulate,
  423. justOne: justOne,
  424. count: count
  425. };
  426. }
  427. if (typeof obj.path !== 'string') {
  428. throw new TypeError('utils.populate: invalid path. Expected string. Got typeof `' + typeof path + '`');
  429. }
  430. return _populateObj(obj);
  431. // The order of select/conditions args is opposite Model.find but
  432. // necessary to keep backward compatibility (select could be
  433. // an array, string, or object literal).
  434. function makeSingles(arr) {
  435. const ret = [];
  436. arr.forEach(function(obj) {
  437. if (/[\s]/.test(obj.path)) {
  438. const paths = obj.path.split(' ');
  439. paths.forEach(function(p) {
  440. const copy = Object.assign({}, obj);
  441. copy.path = p;
  442. ret.push(copy);
  443. });
  444. } else {
  445. ret.push(obj);
  446. }
  447. });
  448. return ret;
  449. }
  450. };
  451. function _populateObj(obj) {
  452. if (Array.isArray(obj.populate)) {
  453. const ret = [];
  454. obj.populate.forEach(function(obj) {
  455. if (/[\s]/.test(obj.path)) {
  456. const copy = Object.assign({}, obj);
  457. const paths = copy.path.split(' ');
  458. paths.forEach(function(p) {
  459. copy.path = p;
  460. ret.push(exports.populate(copy)[0]);
  461. });
  462. } else {
  463. ret.push(exports.populate(obj)[0]);
  464. }
  465. });
  466. obj.populate = exports.populate(ret);
  467. } else if (obj.populate != null && typeof obj.populate === 'object') {
  468. obj.populate = exports.populate(obj.populate);
  469. }
  470. const ret = [];
  471. const paths = obj.path.split(' ');
  472. if (obj.options != null) {
  473. obj.options = exports.clone(obj.options);
  474. }
  475. for (const path of paths) {
  476. ret.push(new PopulateOptions(Object.assign({}, obj, { path: path })));
  477. }
  478. return ret;
  479. }
  480. /*!
  481. * Return the value of `obj` at the given `path`.
  482. *
  483. * @param {String} path
  484. * @param {Object} obj
  485. */
  486. exports.getValue = function(path, obj, map) {
  487. return mpath.get(path, obj, '_doc', map);
  488. };
  489. /*!
  490. * Sets the value of `obj` at the given `path`.
  491. *
  492. * @param {String} path
  493. * @param {Anything} val
  494. * @param {Object} obj
  495. */
  496. exports.setValue = function(path, val, obj, map, _copying) {
  497. mpath.set(path, val, obj, '_doc', map, _copying);
  498. };
  499. /*!
  500. * Returns an array of values from object `o`.
  501. *
  502. * @param {Object} o
  503. * @return {Array}
  504. * @private
  505. */
  506. exports.object = {};
  507. exports.object.vals = function vals(o) {
  508. const keys = Object.keys(o);
  509. let i = keys.length;
  510. const ret = [];
  511. while (i--) {
  512. ret.push(o[keys[i]]);
  513. }
  514. return ret;
  515. };
  516. /*!
  517. * @see exports.options
  518. */
  519. exports.object.shallowCopy = exports.options;
  520. /*!
  521. * Safer helper for hasOwnProperty checks
  522. *
  523. * @param {Object} obj
  524. * @param {String} prop
  525. */
  526. const hop = Object.prototype.hasOwnProperty;
  527. exports.object.hasOwnProperty = function(obj, prop) {
  528. return hop.call(obj, prop);
  529. };
  530. /*!
  531. * Determine if `val` is null or undefined
  532. *
  533. * @return {Boolean}
  534. */
  535. exports.isNullOrUndefined = function(val) {
  536. return val === null || val === undefined;
  537. };
  538. /*!
  539. * ignore
  540. */
  541. exports.array = {};
  542. /*!
  543. * Flattens an array.
  544. *
  545. * [ 1, [ 2, 3, [4] ]] -> [1,2,3,4]
  546. *
  547. * @param {Array} arr
  548. * @param {Function} [filter] If passed, will be invoked with each item in the array. If `filter` returns a falsy value, the item will not be included in the results.
  549. * @return {Array}
  550. * @private
  551. */
  552. exports.array.flatten = function flatten(arr, filter, ret) {
  553. ret || (ret = []);
  554. arr.forEach(function(item) {
  555. if (Array.isArray(item)) {
  556. flatten(item, filter, ret);
  557. } else {
  558. if (!filter || filter(item)) {
  559. ret.push(item);
  560. }
  561. }
  562. });
  563. return ret;
  564. };
  565. /*!
  566. * ignore
  567. */
  568. const _hasOwnProperty = Object.prototype.hasOwnProperty;
  569. exports.hasUserDefinedProperty = function(obj, key) {
  570. if (obj == null) {
  571. return false;
  572. }
  573. if (Array.isArray(key)) {
  574. for (const k of key) {
  575. if (exports.hasUserDefinedProperty(obj, k)) {
  576. return true;
  577. }
  578. }
  579. return false;
  580. }
  581. if (_hasOwnProperty.call(obj, key)) {
  582. return true;
  583. }
  584. if (typeof obj === 'object' && key in obj) {
  585. const v = obj[key];
  586. return v !== Object.prototype[key] && v !== Array.prototype[key];
  587. }
  588. return false;
  589. };
  590. /*!
  591. * ignore
  592. */
  593. const MAX_ARRAY_INDEX = Math.pow(2, 32) - 1;
  594. exports.isArrayIndex = function(val) {
  595. if (typeof val === 'number') {
  596. return val >= 0 && val <= MAX_ARRAY_INDEX;
  597. }
  598. if (typeof val === 'string') {
  599. if (!/^\d+$/.test(val)) {
  600. return false;
  601. }
  602. val = +val;
  603. return val >= 0 && val <= MAX_ARRAY_INDEX;
  604. }
  605. return false;
  606. };
  607. /*!
  608. * Removes duplicate values from an array
  609. *
  610. * [1, 2, 3, 3, 5] => [1, 2, 3, 5]
  611. * [ ObjectId("550988ba0c19d57f697dc45e"), ObjectId("550988ba0c19d57f697dc45e") ]
  612. * => [ObjectId("550988ba0c19d57f697dc45e")]
  613. *
  614. * @param {Array} arr
  615. * @return {Array}
  616. * @private
  617. */
  618. exports.array.unique = function(arr) {
  619. const primitives = {};
  620. const ids = {};
  621. const ret = [];
  622. for (const item of arr) {
  623. if (typeof item === 'number' || typeof item === 'string' || item == null) {
  624. if (primitives[item]) {
  625. continue;
  626. }
  627. ret.push(item);
  628. primitives[item] = true;
  629. } else if (item instanceof ObjectId) {
  630. if (ids[item.toString()]) {
  631. continue;
  632. }
  633. ret.push(item);
  634. ids[item.toString()] = true;
  635. } else {
  636. ret.push(item);
  637. }
  638. }
  639. return ret;
  640. };
  641. /*!
  642. * Determines if two buffers are equal.
  643. *
  644. * @param {Buffer} a
  645. * @param {Object} b
  646. */
  647. exports.buffer = {};
  648. exports.buffer.areEqual = function(a, b) {
  649. if (!Buffer.isBuffer(a)) {
  650. return false;
  651. }
  652. if (!Buffer.isBuffer(b)) {
  653. return false;
  654. }
  655. if (a.length !== b.length) {
  656. return false;
  657. }
  658. for (let i = 0, len = a.length; i < len; ++i) {
  659. if (a[i] !== b[i]) {
  660. return false;
  661. }
  662. }
  663. return true;
  664. };
  665. exports.getFunctionName = getFunctionName;
  666. /*!
  667. * Decorate buffers
  668. */
  669. exports.decorate = function(destination, source) {
  670. for (const key in source) {
  671. if (specialProperties.has(key)) {
  672. continue;
  673. }
  674. destination[key] = source[key];
  675. }
  676. };
  677. /**
  678. * merges to with a copy of from
  679. *
  680. * @param {Object} to
  681. * @param {Object} fromObj
  682. * @api private
  683. */
  684. exports.mergeClone = function(to, fromObj) {
  685. if (isMongooseObject(fromObj)) {
  686. fromObj = fromObj.toObject({
  687. transform: false,
  688. virtuals: false,
  689. depopulate: true,
  690. getters: false,
  691. flattenDecimals: false
  692. });
  693. }
  694. const keys = Object.keys(fromObj);
  695. const len = keys.length;
  696. let i = 0;
  697. let key;
  698. while (i < len) {
  699. key = keys[i++];
  700. if (specialProperties.has(key)) {
  701. continue;
  702. }
  703. if (typeof to[key] === 'undefined') {
  704. to[key] = exports.clone(fromObj[key], {
  705. transform: false,
  706. virtuals: false,
  707. depopulate: true,
  708. getters: false,
  709. flattenDecimals: false
  710. });
  711. } else {
  712. let val = fromObj[key];
  713. if (val != null && val.valueOf && !(val instanceof Date)) {
  714. val = val.valueOf();
  715. }
  716. if (exports.isObject(val)) {
  717. let obj = val;
  718. if (isMongooseObject(val) && !val.isMongooseBuffer) {
  719. obj = obj.toObject({
  720. transform: false,
  721. virtuals: false,
  722. depopulate: true,
  723. getters: false,
  724. flattenDecimals: false
  725. });
  726. }
  727. if (val.isMongooseBuffer) {
  728. obj = Buffer.from(obj);
  729. }
  730. exports.mergeClone(to[key], obj);
  731. } else {
  732. to[key] = exports.clone(val, {
  733. flattenDecimals: false
  734. });
  735. }
  736. }
  737. }
  738. };
  739. /**
  740. * Executes a function on each element of an array (like _.each)
  741. *
  742. * @param {Array} arr
  743. * @param {Function} fn
  744. * @api private
  745. */
  746. exports.each = function(arr, fn) {
  747. for (const item of arr) {
  748. fn(item);
  749. }
  750. };
  751. /*!
  752. * ignore
  753. */
  754. exports.getOption = function(name) {
  755. const sources = Array.prototype.slice.call(arguments, 1);
  756. for (const source of sources) {
  757. if (source[name] != null) {
  758. return source[name];
  759. }
  760. }
  761. return null;
  762. };
  763. /*!
  764. * ignore
  765. */
  766. exports.noop = function() {};