1 | 1 | var path = require('path'); |
|
2 | |||
3 | 1 | module.exports = function(name, func) { |
|
4 | // Load a built-in module if available |
||
5 | 741 | if (typeof func === 'undefined') { |
|
6 | 738 | try { |
|
7 | 738 | func = require(path.join('../adapters/', name)); |
|
8 | } |
||
9 | catch (e) { |
||
10 | 1 | throw new Error('"'+name+'" is not a built-in Patternlibrary adapter. '+e.message); |
|
11 | } |
||
12 | } |
||
13 | |||
14 | // Don't use one of the built-in meta terms |
||
15 | 740 | var reserved = ['docs', 'fileName', '_adapterData']; |
|
16 | 740 | for (var i in reserved) { |
|
0 ignored issues
–
show
|
|||
17 | 2218 | if (name === reserved[i]) { |
|
18 | 1 | throw new Error('"'+name+'" is a reserved keyword, and can\'t be used as the name of a Patternlibrary adapter.'); |
|
19 | } |
||
20 | } |
||
21 | |||
22 | // Make sure the adapter is an object with both of the required methods. |
||
23 | 739 | if (typeof func !== 'function') { |
|
24 | 1 | throw new Error('Patternlibrary adapters must be functions.') |
|
25 | } |
||
26 | |||
27 | // If no config object exists, add it |
||
28 | 738 | if (!func.config) func.config = {}; |
|
29 | |||
30 | 738 | this.adapters[name] = func; |
|
31 | |||
32 | 738 | return this; |
|
33 | } |
||
34 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: