-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaths.js
278 lines (229 loc) · 7.08 KB
/
paths.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
var detectPath = require('./detectPath');
var pathSeparator = "/",
upALevel = "..",
bubbleCapture = "...",
currentKey = "#",
rootPath = "",
pathStart = "[",
pathEnd = "]",
pathWildcard = "*";
function pathToRaw(path) {
return path && path.slice(1, -1);
}
//***********************************************
//
// Raw To Path
//
//***********************************************
function rawToPath(rawPath) {
return pathStart + (rawPath == null ? '' : rawPath) + pathEnd;
}
var memoisePathCache = {};
function resolvePath() {
var memoiseKey,
pathParts = [];
for(var argumentIndex = arguments.length; argumentIndex--;){
var parts = pathToParts(arguments[argumentIndex]);
if(parts){
pathParts.unshift.apply(pathParts, parts);
}
if(isPathAbsolute(arguments[argumentIndex])){
break;
}
}
memoiseKey = pathParts.join(',');
if(memoisePathCache[memoiseKey]){
return memoisePathCache[memoiseKey];
}
var absoluteParts = [],
lastRemoved,
pathParts,
pathPart;
for(var pathPartIndex = 0; pathPartIndex < pathParts.length; pathPartIndex++){
pathPart = pathParts[pathPartIndex];
if (pathPart === currentKey) {
// Has a last removed? Add it back on.
if(lastRemoved != null){
absoluteParts.push(lastRemoved);
lastRemoved = null;
}
} else if (pathPart === rootPath) {
// Root path? Reset parts to be absolute.
absoluteParts = [''];
} else if (pathPart.slice(-bubbleCapture.length) === bubbleCapture) {
// deep bindings
if(pathPart !== bubbleCapture){
absoluteParts.push(pathPart.slice(0, -bubbleCapture.length));
}
} else if (pathPart === upALevel) {
// Up a level? Remove the last item in absoluteParts
lastRemoved = absoluteParts.pop();
} else if (pathPart.slice(0,2) === upALevel) {
var argument = pathPart.slice(2);
//named
while(absoluteParts[absoluteParts.length - 1] !== argument){
if(absoluteParts.length === 0){
throw "Named path part was not found: '" + pathPart + "', in path: '" + arguments[argumentIndex] + "'.";
}
lastRemoved = absoluteParts.pop();
}
} else {
// any following valid part? Add it to the absoluteParts.
absoluteParts.push(pathPart);
}
}
// Convert the absoluteParts to a Path and memoise the result.
return memoisePathCache[memoiseKey] = createPath(absoluteParts);
}
var memoisedPathTokens = {};
function createPath(path){
if(typeof path === 'number'){
path = path.toString();
}
if(path == null){
return rawToPath();
}
// passed in an Expression or an 'expression formatted' Path (eg: '[bla]')
if (typeof path === "string"){
if(memoisedPathTokens[path]){
return memoisedPathTokens[path];
}
if(path.charAt(0) === pathStart) {
var pathString = path.toString(),
detectedPath = detectPath(pathString);
if (detectedPath && detectedPath.length === pathString.length) {
return memoisedPathTokens[pathString] = detectedPath;
} else {
return false;
}
}else{
return createPath(rawToPath(path));
}
}
if(path instanceof Array) {
var parts = [];
for (var i = 0; i < path.length; i++) {
var pathPart = path[i];
pathPart = pathPart.replace(/([\[|\]|\\|\/])/g, '\\$1');
parts.push(pathPart);
}
if(parts.length === 1 && parts[0] === rootPath){
return createRootPath();
}
return rawToPath(parts.join(pathSeparator));
}
}
function createRootPath(){
return createPath([rootPath, rootPath]);
}
var pathCache = {};
function pathToParts(path){
var pathType = typeof path;
if(pathType !== 'string' && pathType !== 'number'){
if(Array.isArray(path)){
return path;
}
return;
}
// if we haven't been passed a path, then turn the input into a path
if (!isPath(path)) {
path = createPath(path);
if(path === false){
return;
}
}
path = path.slice(1,-1);
if(path === ""){
return [];
}
if(pathCache[path]){
return pathCache[path].slice();
}
var lastPartIndex = 0,
parts,
nextChar,
currentChar;
if(path.indexOf('\\') < 0){
pathCache[path] = path.split(pathSeparator);
return pathCache[path].slice();
}
parts = [];
for(var i = 0; i < path.length; i++){
currentChar = path.charAt(i);
if(currentChar === pathSeparator){
parts.push(path.slice(lastPartIndex,i));
lastPartIndex = i+1;
}else if(currentChar === '\\'){
nextChar = path.charAt(i+1);
if(nextChar === '\\'){
path = path.slice(0, i) + path.slice(i + 1);
}else if(nextChar === ']' || nextChar === '['){
path = path.slice(0, i) + path.slice(i + 1);
}else if(nextChar === pathSeparator){
parts.push(path.slice(lastPartIndex), i);
}
}
}
parts.push(path.slice(lastPartIndex));
pathCache[path] = parts.slice();
return parts;
}
function appendPath(){
var parts = pathToParts(arguments[0]);
if(!parts){
return;
}
if(isPathRoot(arguments[0])){
parts.pop();
}
for (var argumentIndex = 1; argumentIndex < arguments.length; argumentIndex++) {
var pathParts = pathToParts(arguments[argumentIndex]);
pathParts && parts.push.apply(parts, pathParts);
}
return createPath(parts);
}
function isPath(path) {
if(!(typeof path === 'string' || (path instanceof String))){
return;
}
return !!path.match(/^\[(?:[^\[\]]|\\.)*\]$/);
}
function isPathAbsolute(path){
var parts = pathToParts(path);
if(parts == null){
return false;
}
return parts[0] === rootPath;
}
function isPathRoot(path){
var parts = pathToParts(path);
if(parts == null){
return false;
}
return (isPathAbsolute(parts) && parts[0] === parts[1]) || parts.length === 0;
}
function isBubbleCapturePath(path){
var parts = pathToParts(path),
lastPart = parts[parts.length-1];
return lastPart && lastPart.slice(-bubbleCapture.length) === bubbleCapture;
}
module.exports = {
resolve: resolvePath,
create: createPath,
is: isPath,
isAbsolute: isPathAbsolute,
isRoot: isPathRoot,
isBubbleCapture: isBubbleCapturePath,
append: appendPath,
toParts: pathToParts,
createRoot: createRootPath,
constants:{
separator: pathSeparator,
upALevel: upALevel,
currentKey: currentKey,
root: rootPath,
start: pathStart,
end: pathEnd,
wildcard: pathWildcard
}
};