-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
211 lines (176 loc) · 3.94 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict';
var fs = require('fs');
var path = require('path');
/**
* Lazily required module dependencies
*/
var utils = require('lazy-cache')(require);
var fn = require;
require = utils;
require('arr-flatten', 'flatten');
require('extend-shallow', 'extend');
require('get-value', 'get');
require('has-glob');
require('has-value', 'has');
require('is-registered');
require('is-valid-app');
require('kind-of', 'typeOf');
require('merge-value');
require('mixin-deep', 'merge');
require('read-file', 'read');
require('resolve-glob', 'resolve');
require('set-value', 'set');
require('union-value');
require = fn;
/**
* Utils
*/
utils.isValid = function(app, prop) {
return utils.isValidApp(app, 'base-data:' + prop, ['app', 'collection', 'list', 'views', 'group']);
};
/**
* Return the last item in `arr`
*/
utils.last = function(arr) {
if (!Array.isArray(arr)) {
throw new TypeError('expected value to be an array');
}
return arr[arr.length - 1];
};
/**
* Cast `val` to an array
*/
utils.arrayify = function(val) {
return val ? (Array.isArray(val) ? val : [val]) : [];
};
/**
* Iterate over registered data loaders and return any
* that match the given `filepath`
*
* @param {Array} `loaders`
* @param {String} `filepath`
* @return {Array} Returns an array of data-loader functions
*/
utils.matchLoaders = function(loaders, fp) {
var len = loaders.length, i = -1;
var ext = path.extname(fp);
var fns = [];
if (len === 0 && ext === '.json') {
return [function(str) {
return JSON.parse(str);
}];
}
while (++i < len) {
var loader = loaders[i];
var name = loader.name;
if (typeof name === 'string' && ext === utils.formatExt(name)) {
fns.push(loader.fn);
} else if (utils.typeOf(name) === 'regexp' && name.test(ext)) {
fns.push(loader.fn);
}
}
if (!fns.length) return null;
return fns;
};
/**
* format the given file extension to always start with a dot
*/
utils.formatExt = function(ext) {
if (ext.charAt(0) !== '.') {
return '.' + ext;
}
return ext;
};
/**
* Namespace a file
*/
utils.namespace = function(key, data, opts) {
var stem = utils.rename(key, data, opts);
if (opts.namespace === true && stem === 'data') {
return data;
}
var obj = {};
utils.set(obj, stem, data);
return obj;
};
/**
* Rename a file
*/
utils.rename = function(key, data, opts) {
var renameFn = utils.basename;
if (typeof opts.namespace === 'string') {
return opts.namespace;
}
if (typeof opts.namespace === 'function') {
renameFn = opts.namespace;
}
return renameFn(key, data, opts);
};
/**
* Get the basename of a filepath excluding extension.
* This is used as the default renaming function
* when `namespace` is true.
*/
utils.basename = function(fp) {
return path.basename(fp, path.extname(fp));
};
/**
* Return true if the key/value pair looks like a glob
* + options or undefined.
*/
utils.isGlob = function(key, val) {
if (typeof val === 'undefined' || utils.isObject(val)) {
return utils.hasGlob(key);
}
return false;
};
/**
* Return true if an object has any of the given keys.
* @return {Boolean}
*/
utils.hasAny = function(obj, keys) {
var len = keys.length;
while (len--) {
if (obj.hasOwnProperty(keys[len])) {
return true;
}
}
return false;
};
/**
* Return true if the given value is an object.
* @return {Boolean}
*/
utils.isObject = function(val) {
if (!val || Array.isArray(val)) {
return false;
}
return typeof val === 'object';
};
/**
* Return true if the given value looks like an options
* object.
*/
var optsKeys = ['namespace', 'renameKey'];
var globKeys = [
'base',
'cwd',
'destBase',
'expand',
'ext',
'extDot',
'extend',
'flatten',
'rename',
'process',
'srcBase'
];
utils.isOptions = function(val) {
if (!utils.isObject(val)) return false;
var keys = globKeys.concat(optsKeys);
return utils.hasAny(val, keys);
};
/**
* Expose `utils` modules
*/
module.exports = utils;