-
Notifications
You must be signed in to change notification settings - Fork 29
/
util.js
66 lines (58 loc) · 2.78 KB
/
util.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
/*───────────────────────────────────────────────────────────────────────────*\
│ Copyright (C) 2014 eBay Software Foundation │
│ │
│ Licensed under the Apache License, Version 2.0 (the "License"); │
│ you may not use this file except in compliance with the License. │
│ You may obtain a copy of the License at │
│ │
│ http://www.apache.org/licenses/LICENSE-2.0 │
│ │
│ Unless required by applicable law or agreed to in writing, software │
│ distributed under the License is distributed on an "AS IS" BASIS, │
│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
│ See the License for the specific language governing permissions and │
│ limitations under the License. │
\*───────────────────────────────────────────────────────────────────────────*/
'use strict';
/**
* Returns true if the obj is and express-like instance, otherwise false.
* @param obj
* @returns {boolean} whether or not the provided object is an express application
*/
exports.isExpress = function isExpress(obj) {
return obj && obj.handle && obj.set;
};
/**
* Similar to Array#map, but accepts objects or arrays.
* @param obj the object for which to map keys.
* @param fn the function implements the mapping. API is function(value, propertyName, srcObject) {}
* @returns {Array}
*/
exports.mapValues = function mapValues(obj, fn) {
return Object.keys(obj).map(function (prop) {
var name = prop.name || prop;
return fn(obj[prop], name, obj);
});
};
/**
* Adds the provided `name` property to the provided object.
* @param obj
* @param name
* @returns {*}
*/
exports.nameObject = function nameObject(obj, name) {
obj && (obj.name = name);
return obj;
};
/**
* Determines if node is able to resolve the provided module file.
* @param module The file path to the desired module.
* @returns {*} The absolute path to the module or `undefined` if not found.
*/
exports.tryResolve = function tryResolve(module) {
try {
return require.resolve(module);
} catch (e) {
return undefined;
}
};