utils.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*!
  2. * Module dependencies.
  3. */
  4. var ObjectId = require('./types/objectid');
  5. var cloneRegExp = require('regexp-clone');
  6. var sliced = require('sliced');
  7. var mpath = require('mpath');
  8. var ms = require('ms');
  9. var MongooseBuffer;
  10. var MongooseArray;
  11. var Document;
  12. /*!
  13. * Produces a collection name from model `name`.
  14. *
  15. * @param {String} name a model name
  16. * @return {String} a collection name
  17. * @api private
  18. */
  19. exports.toCollectionName = function(name, options) {
  20. options = options || {};
  21. if (name === 'system.profile') {
  22. return name;
  23. }
  24. if (name === 'system.indexes') {
  25. return name;
  26. }
  27. if (options.pluralization === false) {
  28. return name;
  29. }
  30. return pluralize(name.toLowerCase());
  31. };
  32. /**
  33. * Pluralization rules.
  34. *
  35. * These rules are applied while processing the argument to `toCollectionName`.
  36. *
  37. * @deprecated remove in 4.x gh-1350
  38. */
  39. exports.pluralization = [
  40. [/(m)an$/gi, '$1en'],
  41. [/(pe)rson$/gi, '$1ople'],
  42. [/(child)$/gi, '$1ren'],
  43. [/^(ox)$/gi, '$1en'],
  44. [/(ax|test)is$/gi, '$1es'],
  45. [/(octop|vir)us$/gi, '$1i'],
  46. [/(alias|status)$/gi, '$1es'],
  47. [/(bu)s$/gi, '$1ses'],
  48. [/(buffal|tomat|potat)o$/gi, '$1oes'],
  49. [/([ti])um$/gi, '$1a'],
  50. [/sis$/gi, 'ses'],
  51. [/(?:([^f])fe|([lr])f)$/gi, '$1$2ves'],
  52. [/(hive)$/gi, '$1s'],
  53. [/([^aeiouy]|qu)y$/gi, '$1ies'],
  54. [/(x|ch|ss|sh)$/gi, '$1es'],
  55. [/(matr|vert|ind)ix|ex$/gi, '$1ices'],
  56. [/([m|l])ouse$/gi, '$1ice'],
  57. [/(kn|w|l)ife$/gi, '$1ives'],
  58. [/(quiz)$/gi, '$1zes'],
  59. [/s$/gi, 's'],
  60. [/([^a-z])$/, '$1'],
  61. [/$/gi, 's']
  62. ];
  63. var rules = exports.pluralization;
  64. /**
  65. * Uncountable words.
  66. *
  67. * These words are applied while processing the argument to `toCollectionName`.
  68. * @api public
  69. */
  70. exports.uncountables = [
  71. 'advice',
  72. 'energy',
  73. 'excretion',
  74. 'digestion',
  75. 'cooperation',
  76. 'health',
  77. 'justice',
  78. 'labour',
  79. 'machinery',
  80. 'equipment',
  81. 'information',
  82. 'pollution',
  83. 'sewage',
  84. 'paper',
  85. 'money',
  86. 'species',
  87. 'series',
  88. 'rain',
  89. 'rice',
  90. 'fish',
  91. 'sheep',
  92. 'moose',
  93. 'deer',
  94. 'news',
  95. 'expertise',
  96. 'status',
  97. 'media'
  98. ];
  99. var uncountables = exports.uncountables;
  100. /*!
  101. * Pluralize function.
  102. *
  103. * @author TJ Holowaychuk (extracted from _ext.js_)
  104. * @param {String} string to pluralize
  105. * @api private
  106. */
  107. function pluralize(str) {
  108. var found;
  109. if (!~uncountables.indexOf(str.toLowerCase())) {
  110. found = rules.filter(function(rule) {
  111. return str.match(rule[0]);
  112. });
  113. if (found[0]) {
  114. return str.replace(found[0][0], found[0][1]);
  115. }
  116. }
  117. return str;
  118. }
  119. /*!
  120. * Determines if `a` and `b` are deep equal.
  121. *
  122. * Modified from node/lib/assert.js
  123. *
  124. * @param {any} a a value to compare to `b`
  125. * @param {any} b a value to compare to `a`
  126. * @return {Boolean}
  127. * @api private
  128. */
  129. exports.deepEqual = function deepEqual(a, b) {
  130. if (a === b) {
  131. return true;
  132. }
  133. if (a instanceof Date && b instanceof Date) {
  134. return a.getTime() === b.getTime();
  135. }
  136. if (a instanceof ObjectId && b instanceof ObjectId) {
  137. return a.toString() === b.toString();
  138. }
  139. if (a instanceof RegExp && b instanceof RegExp) {
  140. return a.source === b.source &&
  141. a.ignoreCase === b.ignoreCase &&
  142. a.multiline === b.multiline &&
  143. a.global === b.global;
  144. }
  145. if (typeof a !== 'object' && typeof b !== 'object') {
  146. return a == b;
  147. }
  148. if (a === null || b === null || a === undefined || b === undefined) {
  149. return false;
  150. }
  151. if (a.prototype !== b.prototype) {
  152. return false;
  153. }
  154. // Handle MongooseNumbers
  155. if (a instanceof Number && b instanceof Number) {
  156. return a.valueOf() === b.valueOf();
  157. }
  158. if (Buffer.isBuffer(a)) {
  159. return exports.buffer.areEqual(a, b);
  160. }
  161. if (isMongooseObject(a)) {
  162. a = a.toObject();
  163. }
  164. if (isMongooseObject(b)) {
  165. b = b.toObject();
  166. }
  167. try {
  168. var ka = Object.keys(a),
  169. kb = Object.keys(b),
  170. key, i;
  171. } catch (e) {
  172. // happens when one is a string literal and the other isn't
  173. return false;
  174. }
  175. // having the same number of owned properties (keys incorporates
  176. // hasOwnProperty)
  177. if (ka.length !== kb.length) {
  178. return false;
  179. }
  180. // the same set of keys (although not necessarily the same order),
  181. ka.sort();
  182. kb.sort();
  183. // ~~~cheap key test
  184. for (i = ka.length - 1; i >= 0; i--) {
  185. if (ka[i] !== kb[i]) {
  186. return false;
  187. }
  188. }
  189. // equivalent values for every corresponding key, and
  190. // ~~~possibly expensive deep test
  191. for (i = ka.length - 1; i >= 0; i--) {
  192. key = ka[i];
  193. if (!deepEqual(a[key], b[key])) {
  194. return false;
  195. }
  196. }
  197. return true;
  198. };
  199. /*!
  200. * Object clone with Mongoose natives support.
  201. *
  202. * If options.minimize is true, creates a minimal data object. Empty objects and undefined values will not be cloned. This makes the data payload sent to MongoDB as small as possible.
  203. *
  204. * Functions are never cloned.
  205. *
  206. * @param {Object} obj the object to clone
  207. * @param {Object} options
  208. * @return {Object} the cloned object
  209. * @api private
  210. */
  211. exports.clone = function clone(obj, options) {
  212. if (obj === undefined || obj === null) {
  213. return obj;
  214. }
  215. if (Array.isArray(obj)) {
  216. return cloneArray(obj, options);
  217. }
  218. if (isMongooseObject(obj)) {
  219. if (options && options.json && typeof obj.toJSON === 'function') {
  220. return obj.toJSON(options);
  221. }
  222. return obj.toObject(options);
  223. }
  224. if (obj.constructor) {
  225. switch (exports.getFunctionName(obj.constructor)) {
  226. case 'Object':
  227. return cloneObject(obj, options);
  228. case 'Date':
  229. return new obj.constructor(+obj);
  230. case 'RegExp':
  231. return cloneRegExp(obj);
  232. default:
  233. // ignore
  234. break;
  235. }
  236. }
  237. if (obj instanceof ObjectId) {
  238. return new ObjectId(obj.id);
  239. }
  240. if (!obj.constructor && exports.isObject(obj)) {
  241. // object created with Object.create(null)
  242. return cloneObject(obj, options);
  243. }
  244. if (obj.valueOf) {
  245. return obj.valueOf();
  246. }
  247. };
  248. var clone = exports.clone;
  249. /*!
  250. * ignore
  251. */
  252. function cloneObject(obj, options) {
  253. var retainKeyOrder = options && options.retainKeyOrder,
  254. minimize = options && options.minimize,
  255. ret = {},
  256. hasKeys,
  257. keys,
  258. val,
  259. k,
  260. i;
  261. if (retainKeyOrder) {
  262. for (k in obj) {
  263. val = clone(obj[k], options);
  264. if (!minimize || (typeof val !== 'undefined')) {
  265. hasKeys || (hasKeys = true);
  266. ret[k] = val;
  267. }
  268. }
  269. } else {
  270. // faster
  271. keys = Object.keys(obj);
  272. i = keys.length;
  273. while (i--) {
  274. k = keys[i];
  275. val = clone(obj[k], options);
  276. if (!minimize || (typeof val !== 'undefined')) {
  277. if (!hasKeys) {
  278. hasKeys = true;
  279. }
  280. ret[k] = val;
  281. }
  282. }
  283. }
  284. return minimize
  285. ? hasKeys && ret
  286. : ret;
  287. }
  288. function cloneArray(arr, options) {
  289. var ret = [];
  290. for (var i = 0, l = arr.length; i < l; i++) {
  291. ret.push(clone(arr[i], options));
  292. }
  293. return ret;
  294. }
  295. /*!
  296. * Shallow copies defaults into options.
  297. *
  298. * @param {Object} defaults
  299. * @param {Object} options
  300. * @return {Object} the merged object
  301. * @api private
  302. */
  303. exports.options = function(defaults, options) {
  304. var keys = Object.keys(defaults),
  305. i = keys.length,
  306. k;
  307. options = options || {};
  308. while (i--) {
  309. k = keys[i];
  310. if (!(k in options)) {
  311. options[k] = defaults[k];
  312. }
  313. }
  314. return options;
  315. };
  316. /*!
  317. * Generates a random string
  318. *
  319. * @api private
  320. */
  321. exports.random = function() {
  322. return Math.random().toString().substr(3);
  323. };
  324. /*!
  325. * Merges `from` into `to` without overwriting existing properties.
  326. *
  327. * @param {Object} to
  328. * @param {Object} from
  329. * @api private
  330. */
  331. exports.merge = function merge(to, from, options) {
  332. options = options || {};
  333. var keys = Object.keys(from);
  334. var i = 0;
  335. var len = keys.length;
  336. var key;
  337. if (options.retainKeyOrder) {
  338. while (i < len) {
  339. key = keys[i++];
  340. if (typeof to[key] === 'undefined') {
  341. to[key] = from[key];
  342. } else if (exports.isObject(from[key])) {
  343. merge(to[key], from[key]);
  344. } else if (options.overwrite) {
  345. to[key] = from[key];
  346. }
  347. }
  348. } else {
  349. while (len--) {
  350. key = keys[len];
  351. if (typeof to[key] === 'undefined') {
  352. to[key] = from[key];
  353. } else if (exports.isObject(from[key])) {
  354. merge(to[key], from[key]);
  355. } else if (options.overwrite) {
  356. to[key] = from[key];
  357. }
  358. }
  359. }
  360. };
  361. /*!
  362. * toString helper
  363. */
  364. var toString = Object.prototype.toString;
  365. /*!
  366. * Applies toObject recursively.
  367. *
  368. * @param {Document|Array|Object} obj
  369. * @return {Object}
  370. * @api private
  371. */
  372. exports.toObject = function toObject(obj) {
  373. Document || (Document = require('./document'));
  374. var ret;
  375. if (exports.isNullOrUndefined(obj)) {
  376. return obj;
  377. }
  378. if (obj instanceof Document) {
  379. return obj.toObject();
  380. }
  381. if (Array.isArray(obj)) {
  382. ret = [];
  383. for (var i = 0, len = obj.length; i < len; ++i) {
  384. ret.push(toObject(obj[i]));
  385. }
  386. return ret;
  387. }
  388. if ((obj.constructor && exports.getFunctionName(obj.constructor) === 'Object') ||
  389. (!obj.constructor && exports.isObject(obj))) {
  390. ret = {};
  391. for (var k in obj) {
  392. ret[k] = toObject(obj[k]);
  393. }
  394. return ret;
  395. }
  396. return obj;
  397. };
  398. /*!
  399. * Determines if `arg` is an object.
  400. *
  401. * @param {Object|Array|String|Function|RegExp|any} arg
  402. * @api private
  403. * @return {Boolean}
  404. */
  405. exports.isObject = function(arg) {
  406. if (Buffer.isBuffer(arg)) {
  407. return true;
  408. }
  409. return toString.call(arg) === '[object Object]';
  410. };
  411. /*!
  412. * A faster Array.prototype.slice.call(arguments) alternative
  413. * @api private
  414. */
  415. exports.args = sliced;
  416. /*!
  417. * process.nextTick helper.
  418. *
  419. * Wraps `callback` in a try/catch + nextTick.
  420. *
  421. * node-mongodb-native has a habit of state corruption when an error is immediately thrown from within a collection callback.
  422. *
  423. * @param {Function} callback
  424. * @api private
  425. */
  426. exports.tick = function tick(callback) {
  427. if (typeof callback !== 'function') {
  428. return;
  429. }
  430. return function() {
  431. try {
  432. callback.apply(this, arguments);
  433. } catch (err) {
  434. // only nextTick on err to get out of
  435. // the event loop and avoid state corruption.
  436. process.nextTick(function() {
  437. throw err;
  438. });
  439. }
  440. };
  441. };
  442. /*!
  443. * Returns if `v` is a mongoose object that has a `toObject()` method we can use.
  444. *
  445. * This is for compatibility with libs like Date.js which do foolish things to Natives.
  446. *
  447. * @param {any} v
  448. * @api private
  449. */
  450. exports.isMongooseObject = function(v) {
  451. Document || (Document = require('./document'));
  452. MongooseArray || (MongooseArray = require('./types').Array);
  453. MongooseBuffer || (MongooseBuffer = require('./types').Buffer);
  454. return v instanceof Document ||
  455. (v && v.isMongooseArray) ||
  456. (v && v.isMongooseBuffer);
  457. };
  458. var isMongooseObject = exports.isMongooseObject;
  459. /*!
  460. * Converts `expires` options of index objects to `expiresAfterSeconds` options for MongoDB.
  461. *
  462. * @param {Object} object
  463. * @api private
  464. */
  465. exports.expires = function expires(object) {
  466. if (!(object && object.constructor.name === 'Object')) {
  467. return;
  468. }
  469. if (!('expires' in object)) {
  470. return;
  471. }
  472. var when;
  473. if (typeof object.expires !== 'string') {
  474. when = object.expires;
  475. } else {
  476. when = Math.round(ms(object.expires) / 1000);
  477. }
  478. object.expireAfterSeconds = when;
  479. delete object.expires;
  480. };
  481. /*!
  482. * Populate options constructor
  483. */
  484. function PopulateOptions(path, select, match, options, model, subPopulate) {
  485. this.path = path;
  486. this.match = match;
  487. this.select = select;
  488. this.options = options;
  489. this.model = model;
  490. if (typeof subPopulate === 'object') {
  491. this.populate = subPopulate;
  492. }
  493. this._docs = {};
  494. }
  495. // make it compatible with utils.clone
  496. PopulateOptions.prototype.constructor = Object;
  497. // expose
  498. exports.PopulateOptions = PopulateOptions;
  499. /*!
  500. * populate helper
  501. */
  502. exports.populate = function populate(path, select, model, match, options, subPopulate) {
  503. // The order of select/conditions args is opposite Model.find but
  504. // necessary to keep backward compatibility (select could be
  505. // an array, string, or object literal).
  506. // might have passed an object specifying all arguments
  507. if (arguments.length === 1) {
  508. if (path instanceof PopulateOptions) {
  509. return [path];
  510. }
  511. if (Array.isArray(path)) {
  512. return path.map(function(o) {
  513. return exports.populate(o)[0];
  514. });
  515. }
  516. if (exports.isObject(path)) {
  517. match = path.match;
  518. options = path.options;
  519. select = path.select;
  520. model = path.model;
  521. subPopulate = path.populate;
  522. path = path.path;
  523. }
  524. } else if (typeof model !== 'string' && typeof model !== 'function') {
  525. options = match;
  526. match = model;
  527. model = undefined;
  528. }
  529. if (typeof path !== 'string') {
  530. throw new TypeError('utils.populate: invalid path. Expected string. Got typeof `' + typeof path + '`');
  531. }
  532. if (typeof subPopulate === 'object') {
  533. subPopulate = exports.populate(subPopulate);
  534. }
  535. var ret = [];
  536. var paths = path.split(' ');
  537. options = exports.clone(options, { retainKeyOrder: true });
  538. for (var i = 0; i < paths.length; ++i) {
  539. ret.push(new PopulateOptions(paths[i], select, match, options, model, subPopulate));
  540. }
  541. return ret;
  542. };
  543. /*!
  544. * Return the value of `obj` at the given `path`.
  545. *
  546. * @param {String} path
  547. * @param {Object} obj
  548. */
  549. exports.getValue = function(path, obj, map) {
  550. return mpath.get(path, obj, '_doc', map);
  551. };
  552. /*!
  553. * Sets the value of `obj` at the given `path`.
  554. *
  555. * @param {String} path
  556. * @param {Anything} val
  557. * @param {Object} obj
  558. */
  559. exports.setValue = function(path, val, obj, map) {
  560. mpath.set(path, val, obj, '_doc', map);
  561. };
  562. /*!
  563. * Returns an array of values from object `o`.
  564. *
  565. * @param {Object} o
  566. * @return {Array}
  567. * @private
  568. */
  569. exports.object = {};
  570. exports.object.vals = function vals(o) {
  571. var keys = Object.keys(o),
  572. i = keys.length,
  573. ret = [];
  574. while (i--) {
  575. ret.push(o[keys[i]]);
  576. }
  577. return ret;
  578. };
  579. /*!
  580. * @see exports.options
  581. */
  582. exports.object.shallowCopy = exports.options;
  583. /*!
  584. * Safer helper for hasOwnProperty checks
  585. *
  586. * @param {Object} obj
  587. * @param {String} prop
  588. */
  589. var hop = Object.prototype.hasOwnProperty;
  590. exports.object.hasOwnProperty = function(obj, prop) {
  591. return hop.call(obj, prop);
  592. };
  593. /*!
  594. * Determine if `val` is null or undefined
  595. *
  596. * @return {Boolean}
  597. */
  598. exports.isNullOrUndefined = function(val) {
  599. return val === null || val === undefined;
  600. };
  601. /*!
  602. * ignore
  603. */
  604. exports.array = {};
  605. /*!
  606. * Flattens an array.
  607. *
  608. * [ 1, [ 2, 3, [4] ]] -> [1,2,3,4]
  609. *
  610. * @param {Array} arr
  611. * @param {Function} [filter] If passed, will be invoked with each item in the array. If `filter` returns a falsey value, the item will not be included in the results.
  612. * @return {Array}
  613. * @private
  614. */
  615. exports.array.flatten = function flatten(arr, filter, ret) {
  616. ret || (ret = []);
  617. arr.forEach(function(item) {
  618. if (Array.isArray(item)) {
  619. flatten(item, filter, ret);
  620. } else {
  621. if (!filter || filter(item)) {
  622. ret.push(item);
  623. }
  624. }
  625. });
  626. return ret;
  627. };
  628. /*!
  629. * Removes duplicate values from an array
  630. *
  631. * [1, 2, 3, 3, 5] => [1, 2, 3, 5]
  632. * [ ObjectId("550988ba0c19d57f697dc45e"), ObjectId("550988ba0c19d57f697dc45e") ]
  633. * => [ObjectId("550988ba0c19d57f697dc45e")]
  634. *
  635. * @param {Array} arr
  636. * @return {Array}
  637. * @private
  638. */
  639. exports.array.unique = function(arr) {
  640. var primitives = {};
  641. var ids = {};
  642. var ret = [];
  643. var length = arr.length;
  644. for (var i = 0; i < length; ++i) {
  645. if (typeof arr[i] === 'number' || typeof arr[i] === 'string') {
  646. if (primitives[arr[i]]) {
  647. continue;
  648. }
  649. ret.push(arr[i]);
  650. primitives[arr[i]] = true;
  651. } else if (arr[i] instanceof ObjectId) {
  652. if (ids[arr[i].toString()]) {
  653. continue;
  654. }
  655. ret.push(arr[i]);
  656. ids[arr[i].toString()] = true;
  657. } else {
  658. ret.push(arr[i]);
  659. }
  660. }
  661. return ret;
  662. };
  663. /*!
  664. * Determines if two buffers are equal.
  665. *
  666. * @param {Buffer} a
  667. * @param {Object} b
  668. */
  669. exports.buffer = {};
  670. exports.buffer.areEqual = function(a, b) {
  671. if (!Buffer.isBuffer(a)) {
  672. return false;
  673. }
  674. if (!Buffer.isBuffer(b)) {
  675. return false;
  676. }
  677. if (a.length !== b.length) {
  678. return false;
  679. }
  680. for (var i = 0, len = a.length; i < len; ++i) {
  681. if (a[i] !== b[i]) {
  682. return false;
  683. }
  684. }
  685. return true;
  686. };
  687. exports.getFunctionName = function(fn) {
  688. if (fn.name) {
  689. return fn.name;
  690. }
  691. return (fn.toString().trim().match(/^function\s*([^\s(]+)/) || [])[1];
  692. };
  693. exports.decorate = function(destination, source) {
  694. for (var key in source) {
  695. destination[key] = source[key];
  696. }
  697. };
  698. /**
  699. * merges to with a copy of from
  700. *
  701. * @param {Object} to
  702. * @param {Object} fromObj
  703. * @api private
  704. */
  705. exports.mergeClone = function(to, fromObj) {
  706. var keys = Object.keys(fromObj);
  707. var len = keys.length;
  708. var i = 0;
  709. var key;
  710. while (i < len) {
  711. key = keys[i++];
  712. if (typeof to[key] === 'undefined') {
  713. // make sure to retain key order here because of a bug handling the $each
  714. // operator in mongodb 2.4.4
  715. to[key] = exports.clone(fromObj[key], {retainKeyOrder: 1});
  716. } else {
  717. if (exports.isObject(fromObj[key])) {
  718. var obj = fromObj[key];
  719. if (isMongooseObject(fromObj[key]) && !fromObj[key].isMongooseBuffer) {
  720. obj = obj.toObject({ transform: false, virtuals: false });
  721. }
  722. if (fromObj[key].isMongooseBuffer) {
  723. obj = new Buffer(obj);
  724. }
  725. exports.mergeClone(to[key], obj);
  726. } else {
  727. // make sure to retain key order here because of a bug handling the
  728. // $each operator in mongodb 2.4.4
  729. to[key] = exports.clone(fromObj[key], {retainKeyOrder: 1});
  730. }
  731. }
  732. }
  733. };
  734. /**
  735. * Executes a function on each element of an array (like _.each)
  736. *
  737. * @param {Array} arr
  738. * @param {Function} fn
  739. * @api private
  740. */
  741. exports.each = function(arr, fn) {
  742. for (var i = 0; i < arr.length; ++i) {
  743. fn(arr[i]);
  744. }
  745. };