-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.js
128 lines (108 loc) · 3.13 KB
/
util.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
define([], function () {
var util;
return util = {
computeNamespace: function (expression) {
// summary:
// compute the full namespace
function computeName(expression) {
// summary:
// Reduces a MemberExpression into a flat list of identifiers.
// returns: Array
// List of Identifier or Literal nodes. If an Identifier has a computed property,
// this means that the MemberExpression was created using an array notation.
var name = [];
if (expression.type === "Identifier") {
name.push(expression.name);
}
if (expression.object) {
if (expression.object.type === "MemberExpression") {
name.push.apply(name, computeName(expression.object));
}
else {
name.push(expression.object);
}
}
if (expression.computed && expression.property.type === "Identifier") {
name.push(Object.create(expression.property, {
computed: {
value: true,
writable: true,
enumerable: true,
configurable: true
}
}));
}
else if (expression.property) {
name.push(expression.property);
}
return name;
}
return computeName(expression).map(function (identifier) {
if (!identifier) {
return "";
}
if (identifier.computed && identifier.type !== "Literal") {
return "#computed#";
}
return identifier.value || identifier.name;
}).join(".");
},
is: function (expectedName, contains) {
// summary:
// Creates a function to determine whether or not a given MemberExpression matches the expected name.
return function (callInfo) {
var computed = util.computeNamespace(callInfo);
if (!computed) {
return false;
}
return contains ?
computed.slice(0, expectedName.length) === expectedName :
expectedName === computed;
};
},
isCall: function (expectedAccessor, contains) {
// summary:
// Creates a function to determine whether or not the given CallExpression matches the expected name.
var isName = util.is(expectedAccessor, contains);
return function (callInfo) {
return callInfo.type === "CallExpression" && isName(callInfo.callee);
};
},
toMid: function (name) {
// summary:
// Converts a JavaScript name to an AMD module ID.
return name.replace(/\./g, "/");
},
traverse: function (object, visitor, master) {
// summary:
// Traverses a tree of objects.
var key, child, parent, path;
parent = typeof master === "undefined" ? [] : master;
if (visitor(object, parent) === false) {
return;
}
for (key in object) {
if (object.hasOwnProperty(key) && key !== "parent") {
child = object[key];
path = [ object ];
path.push(parent);
if (typeof child === "object" && child !== null) {
child.parent = object;
util.traverse(child, visitor, path);
}
}
}
},
traverseDom: function (node, visitor) {
// summary:
// Traverses a DOM generated by libxml.
if (visitor(node) === false) {
return;
}
var children = node.childNodes();
for (var i = 0, child; (child = children[i]); ++i) {
util.traverseDom(child, visitor);
}
}
};
});