helper.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. var path = require('path');
  3. var helper = module.exports = {};
  4. // Returns boolean whether filepath is dir terminated
  5. helper.isDir = function isDir(dir) {
  6. if (typeof dir !== 'string') { return false; }
  7. return (dir.slice(-(path.sep.length)) === path.sep);
  8. };
  9. // Create a `key:[]` if doesnt exist on `obj` then push or concat the `val`
  10. helper.objectPush = function objectPush(obj, key, val) {
  11. if (obj[key] == null) { obj[key] = []; }
  12. if (Array.isArray(val)) { obj[key] = obj[key].concat(val); }
  13. else if (val) { obj[key].push(val); }
  14. return obj[key] = helper.unique(obj[key]);
  15. };
  16. // Ensures the dir is marked with path.sep
  17. helper.markDir = function markDir(dir) {
  18. if (typeof dir === 'string' &&
  19. dir.slice(-(path.sep.length)) !== path.sep &&
  20. dir !== '.') {
  21. dir += path.sep;
  22. }
  23. return dir;
  24. };
  25. // Changes path.sep to unix ones for testing
  26. helper.unixifyPathSep = function unixifyPathSep(filepath) {
  27. return (process.platform === 'win32') ? String(filepath).replace(/\\/g, '/') : filepath;
  28. };
  29. /**
  30. * Lo-Dash 1.0.1 <http://lodash.com/>
  31. * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
  32. * Based on Underscore.js 1.4.4 <http://underscorejs.org/>
  33. * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud Inc.
  34. * Available under MIT license <http://lodash.com/license>
  35. */
  36. helper.unique = function unique() { var array = Array.prototype.concat.apply(Array.prototype, arguments); var result = []; for (var i = 0; i < array.length; i++) { if (result.indexOf(array[i]) === -1) { result.push(array[i]); } } return result; };
  37. /**
  38. * Copyright (c) 2010 Caolan McMahon
  39. * Available under MIT license <https://raw.github.com/caolan/async/master/LICENSE>
  40. */
  41. helper.forEachSeries = function forEachSeries(arr, iterator, callback) {
  42. if (!arr.length) { return callback(); }
  43. var completed = 0;
  44. var iterate = function() {
  45. iterator(arr[completed], function (err) {
  46. if (err) {
  47. callback(err);
  48. callback = function() {};
  49. } else {
  50. completed += 1;
  51. if (completed === arr.length) {
  52. callback(null);
  53. } else {
  54. iterate();
  55. }
  56. }
  57. });
  58. };
  59. iterate();
  60. };