Skip to content

Commit f895763

Browse files
committed
add runtime version of has plugin
1 parent 46426fc commit f895763

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

has.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
define(["module"], function(module){
2+
var cache = (module.config && module.config()) || {};
3+
4+
function resolve(resource, has, isBuild) {
5+
var tokens = resource.match(/[\?:]|[^:\?]+/g);
6+
var i = 0;
7+
console.log(resource)
8+
get = function(skip){
9+
var term = tokens[i++];
10+
if(term === ":"){
11+
// empty string module name; therefore, no dependency
12+
return "";
13+
}else{
14+
// postfixed with a ? means it is a feature to branch on, the term is the name of the feature
15+
if(tokens[i++] === "?"){
16+
var hasResult = has(term);
17+
if(hasResult === undefined && isBuild){
18+
return undefined;
19+
}else if(!skip && hasResult){
20+
// matched the feature, get the first value from the options
21+
return get();
22+
}else{
23+
// did not match, get the second value, passing over the first
24+
get(true);
25+
return get(skip);
26+
}
27+
}
28+
// a module
29+
return term;
30+
}
31+
}
32+
return get();
33+
}
34+
35+
var has = function(name){
36+
var global = (function (){
37+
return this;
38+
})();
39+
40+
return typeof cache[name] === "function" ? (cache[name] = cache[name](global)) : cache[name]; // Boolean
41+
};
42+
43+
has.cache = cache;
44+
45+
has.add = function(name, test, now, force){
46+
(typeof cache[name] === "undefined" || force) && (cache[name] = test);
47+
return now && has(name);
48+
};
49+
50+
has.normalize = function(resource, normalize){
51+
var tokens = resource.match(/[\?:]|[^:\?]+/g);
52+
53+
for (var i = 0; i < tokens.length; i++) {
54+
if (tokens[i] !== ":" && tokens[i] !== "?" && tokens[i+1] !== "?") {
55+
tokens[i] = normalize(tokens[i]);
56+
}
57+
}
58+
59+
return tokens.join("");
60+
};
61+
62+
has.load = function(resource, req, onLoad, config){
63+
config = config || {};
64+
65+
if (!resource || config.isBuild) {
66+
onLoad();
67+
return;
68+
}
69+
70+
var mid = resolve(resource, has, config.isBuild);
71+
72+
if(mid){
73+
req([mid], onLoad);
74+
}else{
75+
onLoad();
76+
}
77+
};
78+
79+
return has;
80+
});

0 commit comments

Comments
 (0)