Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80657 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
18
/**
19
* A data structure to efficiently find configuration for a given resource
20
* @class
21
* @param {Array.<ProjectConfiguration>} configurations
22
*/
23
24
var node_path = require('path');
25
function ConfigurationTrie(configurations) {
26
this.root = { paths: {} };
27
this.configurations = configurations;
28
configurations.forEach(this.indexConfiguration, this);
29
}
30
31
ConfigurationTrie.fromObject = function(object) {
32
var ProjectConfiguration = require('./resource/ProjectConfiguration');
33
return new ConfigurationTrie(object.map(function(r) {
34
return ProjectConfiguration.fromObject(r);
35
}));
36
};
37
38
ConfigurationTrie.prototype.toObject = function() {
39
return this.configurations.map(function(r) {
40
return r.toObject();
41
});
42
};
43
44
/**
45
* @protected
46
*/
47
ConfigurationTrie.prototype.indexConfiguration = function(configuration) {
48
configuration.getHasteRoots().forEach(function(path) {
49
var parts = path.split(node_path.sep);
50
var node = this.root;
51
for (var i = 0; i < parts.length; i++) {
52
var part = parts[i];
53
node.paths[part] = node.paths[part] || { paths: {} };
54
node = node.paths[part];
55
}
56
node.configuration = configuration;
57
}, this);
58
};
59
60
ConfigurationTrie.prototype.findConfiguration = function(resourcePath) {
61
var parts = resourcePath.split(node_path.sep);
62
var node = this.root;
63
var configuration;
64
for (var i = 0; i < parts.length - 1; i++) {
65
var part = parts[i];
66
if (node.paths[part]) {
67
node = node.paths[part];
68
configuration = node.configuration || configuration;
69
} else {
70
break;
71
}
72
}
73
return configuration;
74
};
75
76
module.exports = ConfigurationTrie;
77
78