-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUtility.js
More file actions
68 lines (65 loc) · 2.34 KB
/
Copy pathUtility.js
File metadata and controls
68 lines (65 loc) · 2.34 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
define('spec/Utility', [], function() {
var nameToPathCache = {};
var nameToPath = function(name) {
if (typeof name !== 'string' || name === '.' || name === '') return []; // '', for compatibility
if (nameToPathCache[name]) return nameToPathCache[name];
var parts = name.split('.');
var path = [];
for (var i = 0; i < parts.length; i++)
{
var match = parts[i].match(/([a-zA-Z0-9_$]+)\[([^\]]+)\]/);
if (match)
{
path.push(match[1]);
if (/^\d+$/.test(match[2]))
path.push(parseInt(match[2]));
else
path.push(match[2]);
}
else
{
path.push(parts[i]);
}
}
return (nameToPathCache[name] = path.reverse());
},
getValue =function(o, name, defaultValue) {
var path = nameToPath(name).slice(0);
var current = o;
while (current && path.length > 0)
{
var key = path.pop();
if (typeof current[key] !== 'undefined')
current = current[key];
else
return typeof defaultValue !== 'undefined' ? defaultValue : null;
}
return current;
};
beforeEach(function() {
jasmine.addMatchers({
toHaveProperty: function(util, customEqualityTesters) {
return {
compare: function(actual, name, value) {
var empty = {},
actual = getValue(actual, name, empty),
result = {};
result.pass = value === undefined
? actual !== empty
: actual == value;
return result;
}
};
},
toExist: function( util, customEqualityTesters) {
return {
compare: function(actual) {
var result = {};
result.pass = !!actual;
return result;
}
};
}
});
});
});