Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdefaultable.js
More file actions
Latest commit
182 lines (147 loc) · 5.06 KB
/
Copy pathdefaultable.js
File metadata and controls
182 lines (147 loc) · 5.06 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
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
// Defaultable APIs
//
// Copyright 2011 Jason Smith, Jarrett Cruger and contributors
//
// 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.
module.exports=good_args(defaultable);
module.exports.def=good_args(fresh_defaultable);
module.exports.merge=merge_obj;
varpath_lib=require('path');
varreal_require=require;
functionfresh_defaultable(_mod,_defs,_definer){
varmod=defaultable.apply(this,arguments);
mod.defaults._defaultable.fresh=true;
returnmod;
}
functiondefaultable(real_module,initial_defs,definer,real_require){
if(!real_module||!real_module.exports)
thrownewError('Need to provide the module, with .exports object');
if(!is_obj(initial_defs))
thrownewError('Defaults must be an object');
// use real_module.id if filename not available (running on couchdb)
varmod_dir=path_lib.dirname(real_module.filename||real_module.id);
varmod_require=real_module.require||real_require||workaround_require;
workaround_require._defaultable=true;
functionworkaround_require(path){
if(/^\.\//.test(path)||/^\.\.\//.test(path))
path=path_lib.resolve(mod_dir,path);
try{
returnreal_require(path)
}catch(er){
// This CouchDB workaround really belongs in CouchDB. CouchDB errors look like this:
// ["error","invalid_require_path","Must require a JavaScript string, not: object"]
if(er[0]!=="error"||er[1]!=="invalid_require_path"
||er[2]!=="Must require a JavaScript string, not: object")
thrower;
try{returnreal_require(path+'/index')}
catch(er2){thrower}// Nope, throw the original error.
}
}
vardefaulter=make_defaulter({});
real_module.exports=defaulter(initial_defs);
returnreal_module.exports;
functionmake_defaulter(old_defs){
defaulter._defaultable={};
returndefaulter;
functiondefaulter(new_defs){
varfaux_exports={};
varfaux_module={"exports":faux_exports};
varfinal_defs=merge_obj(new_defs||{},old_defs);
require._defaultable=true;
functionrequire(path){
varmod=mod_require.call(real_module,path);
if(mod.defaults&&typeofmod.defaults==='function'&&mod.defaults._defaultable&&!mod.defaults._defaultable.fresh)
returnmod.defaults(final_defs);
returnmod;
}
definer(faux_module,faux_exports,final_defs,require);
varapi=faux_module.exports;
if(('defaults'inapi)&&!api.defaults._defaultable)
thrownewError('defaultable modules may not export a label called "defaults"');
else
api.defaults=make_defaulter(final_defs);
returnapi;
}
}
}
// Recursively merge higher-priority values into previously-set lower-priority ones.
functionmerge_obj(high,low){
if(!is_obj(high))
thrownewError('Bad merge high-priority');
if(!is_obj(low))
thrownewError('Bad merge low-priority');
varkeys=[];
functionadd_key(k){
if(!~keys.indexOf(k))
keys.push(k);
}
_each(_keys(high),add_key);
_each(_keys(low),add_key);
varresult={};
_each(keys,function(key){
varhigh_val=high[key];
varlow_val=low[key];
if(is_obj(high_val)&&is_obj(low_val))
result[key]=merge_obj(high_val,low_val);
elseif(keyinhigh)
result[key]=high[key];
elseif(keyinlow)
result[key]=low[key];
else
thrownewError('Unknown key type: '+key);
})
returnresult;
}
//
// Utilities
//
/**
* Added for browser compatibility
*/
function_keys(obj){
if(Object.keys)returnObject.keys(obj);
varkeys=[];
for(varkinobj){
if(obj.hasOwnProperty(k))keys.push(k);
}
returnkeys;
}
function_each(obj,fn){
for(vari=0,len=obj.length;i<len;i++){
fn(obj[i]);
}
}
functionisArray(obj){
if(Array.isArray)
returnArray.isArray(obj);
returntoString.call(obj)==='[object Array]';
}
functiongood_args(func){
// Make a function validate its parameters.
returngood;
functiongood(_Mod,_Defs,_Definer){
varargs=Array.prototype.slice.call(arguments);
varm0dule={'exports': {}};
if(args.length==1)
returnfunc(m0dule,{},args[0]);
elseif(args.length==2)
returnfunc(m0dule,args[0],args[1]);
elseif(args.length>2)
returnfunc.apply(this,args);
else
thrownewError('Unknown arguments: '+JSON.stringify(args));
}
}
functionis_obj(val){
returnval&&!isArray(val)&&(typeofval==='object')
}