-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
85 lines (72 loc) · 2.25 KB
/
utils.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
var segmentsCache = {};
var bubbleCache = {};
exports.segmentsPattern = /[\.\[\]]/;
exports.clearCache = function clearCache() {
segmentsCache = {};
bubbleCache = {};
};
exports.resolve = function resolve(name, ns) {
if (!name) return {};
name = ns && !exports.endsWith(name, '.') ? name + '.' : name;
if (segmentsCache[name]) return segmentsCache[name];
var segments = name.split(exports.segmentsPattern);
segmentsCache[name] = {
namespace: segments.length > 1 ? segments.slice(0, segments.length - 1) : null,
actionType: segments[segments.length - 1]
};
return segmentsCache[name];
};
exports.nested = function nested(name, state, ns) {
var segments = exports.resolve(name, ns);
if (!segments.namespace || name === '*') return state;
var s = state;
var found = segments.namespace.every(function (seg, i) {
if (typeof s[seg] === 'undefined') return false;
s = s[seg];
return true;
});
return found ? s : null;
};
exports.nestedParent = function nestedParent(name, state, ns) {
var segments = exports.resolve(name, ns);
if (!segments.namespace) return {};
var s = state, k = null;
var found = segments.namespace.every(function (seg, i) {
if (typeof s[seg] === 'undefined') return false;
if (i < segments.namespace.length - 1) {
s = s[seg];
}
k = seg;
return true;
});
return found ? { state: s, key: k } : {};
};
exports.fill = function fill(name, state, ns) {
var segments = exports.resolve(name, ns);
if (!segments.namespace) return state;
var s = state;
segments.namespace.forEach(function (seg, i) {
if (typeof s[seg] === 'undefined') s[seg] = {};
s = s[seg];
});
return s;
};
exports.bubble = function bubble(name, state) {
if (bubbleCache[name]) return bubbleCache[name];
var names = [name];
var ns = name;
while (ns.search(exports.segmentsPattern) > 0) {
ns = ns.substring(0, searchLast(ns, exports.segmentsPattern));
names.push(ns);
}
if (name !== '*') names.push('*');
bubbleCache[name] = names;
return names;
};
function searchLast(str, regex) {
var match = str.match(regex);
return str.lastIndexOf(match[match.length - 1]);
}
exports.endsWith = function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
};