UNPKG

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