Initial commit
[yaffs-website] / node_modules / gaze / lib / helper.js
1 'use strict';
2
3 var path = require('path');
4 var helper = module.exports = {};
5
6 // Returns boolean whether filepath is dir terminated
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 // Create a `key:[]` if doesnt exist on `obj` then push or concat the `val`
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 // Ensures the dir is marked with path.sep
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 // Changes path.sep to unix ones for testing
31 helper.unixifyPathSep = function unixifyPathSep(filepath) {
32   return (process.platform === 'win32') ? String(filepath).replace(/\\/g, '/') : filepath;
33 };
34
35 /**
36  * Lo-Dash 1.0.1 <http://lodash.com/>
37  * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
38  * Based on Underscore.js 1.4.4 <http://underscorejs.org/>
39  * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud Inc.
40  * Available under MIT license <http://lodash.com/license>
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  * Copyright (c) 2010 Caolan McMahon
46  * Available under MIT license <https://raw.github.com/caolan/async/master/LICENSE>
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 };