helper.js 2.2 KB

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