1 | 'use strict';
|
2 |
|
3 | var path = require('path');
|
4 | var helper = module.exports = {};
|
5 |
|
6 |
|
7 | helper.isDir = function isDir(dir) {
|
8 | if (typeof dir !== 'string') { return false; }
|
9 | return (dir.slice(-(path.sep.length)) === path.sep);
|
10 | };
|
11 |
|
12 |
|
13 | helper.objectPush = function objectPush(obj, key, val) {
|
14 | if (obj[key] == null) { obj[key] = []; }
|
15 | if (Array.isArray(val)) { obj[key] = obj[key].concat(val); }
|
16 | else if (val) { obj[key].push(val); }
|
17 | return obj[key] = helper.unique(obj[key]);
|
18 | };
|
19 |
|
20 |
|
21 | helper.markDir = function markDir(dir) {
|
22 | if (typeof dir === 'string' &&
|
23 | dir.slice(-(path.sep.length)) !== path.sep &&
|
24 | dir !== '.') {
|
25 | dir += path.sep;
|
26 | }
|
27 | return dir;
|
28 | };
|
29 |
|
30 |
|
31 | helper.unixifyPathSep = function unixifyPathSep(filepath) {
|
32 | return (process.platform === 'win32') ? String(filepath).replace(/\\/g, '/') : filepath;
|
33 | };
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | 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; };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | helper.forEachSeries = function forEachSeries(arr, iterator, callback) {
|
49 | if (!arr.length) { return callback(); }
|
50 | var completed = 0;
|
51 | var iterate = function() {
|
52 | iterator(arr[completed], function (err) {
|
53 | if (err) {
|
54 | callback(err);
|
55 | callback = function() {};
|
56 | } else {
|
57 | completed += 1;
|
58 | if (completed === arr.length) {
|
59 | callback(null);
|
60 | } else {
|
61 | iterate();
|
62 | }
|
63 | }
|
64 | });
|
65 | };
|
66 | iterate();
|
67 | };
|