forked from nodegit/nodegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlookup_wrapper.js
More file actions
40 lines (32 loc) · 1.11 KB
/
lookup_wrapper.js
File metadata and controls
40 lines (32 loc) · 1.11 KB
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
var Promise = require("nodegit-promise");
var NodeGit = require("../../");
/**
* Wraps a method so that you can pass in either a string, OID or the object
* itself and you will always get back a promise that resolves to the object.
* @param {Object} objectType The object type that you're expecting to receive.
* @param {Function} lookupFunction The function to do the lookup for the
* object. Defaults to `objectType.lookup`.
* @return {Function}
*/
function lookupWrapper(objectType, lookupFunction) {
lookupFunction = lookupFunction || objectType.lookup;
return function(repo, id, callback) {
if (id instanceof objectType) {
return Promise.resolve(id).then(function(obj) {
obj.repo = repo;
if (typeof callback === "function") {
callback(null, obj);
}
return obj;
}, callback);
}
return lookupFunction(repo, id).then(function(obj) {
obj.repo = repo;
if (typeof callback === "function") {
callback(null, obj);
}
return obj;
}, callback);
};
}
NodeGit.Utils.lookupWrapper = lookupWrapper;