1 | (function() {
|
2 | var JsPath,
|
3 | __slice = Array.prototype.slice;
|
4 |
|
5 | this.JsPath = (function() {
|
6 | var primTypes,
|
7 | _this = this;
|
8 |
|
9 | primTypes = /^(string|number|boolean)$/;
|
10 |
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function JsPath(path, val) {
|
24 | return JsPath.setAt({}, path, val || {});
|
25 | }
|
26 |
|
27 | ['forEach', 'indexOf', 'join', 'pop', 'reverse', 'shift', 'sort', 'splice', 'unshift', 'push'].forEach(function(method) {
|
28 | return JsPath[method + 'At'] = function() {
|
29 | var obj, path, rest, target;
|
30 | obj = arguments[0], path = arguments[1], rest = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
|
31 | target = JsPath.getAt(obj, path);
|
32 | if ('function' === typeof (target != null ? target[method] : void 0)) {
|
33 | return target[method].apply(target, rest);
|
34 | } else {
|
35 | throw new Error("Does not implement method " + method + " at " + path);
|
36 | }
|
37 | };
|
38 | });
|
39 |
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | JsPath.getAt = function(ref, path) {
|
52 | var prop;
|
53 | if ('function' === typeof path.split) {
|
54 | path = path.split('.');
|
55 | } else {
|
56 | path = path.slice();
|
57 | }
|
58 | while ((ref != null) && (prop = path.shift())) {
|
59 | ref = ref[prop];
|
60 | }
|
61 | return ref;
|
62 | };
|
63 |
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | JsPath.setAt = function(obj, path, val) {
|
78 | var component, last, prev, ref;
|
79 | if ('function' === typeof path.split) {
|
80 | path = path.split('.');
|
81 | } else {
|
82 | path = path.slice();
|
83 | }
|
84 | last = path.pop();
|
85 | prev = [];
|
86 | ref = obj;
|
87 | while (component = path.shift()) {
|
88 | if (primTypes.test(typeof ref[component])) {
|
89 | throw new Error("" + (prev.concat(component).join('.')) + " is\nprimitive, and cannot be extended.");
|
90 | }
|
91 | ref = ref[component] || (ref[component] = {});
|
92 | prev.push(component);
|
93 | }
|
94 | ref[last] = val;
|
95 | return obj;
|
96 | };
|
97 |
|
98 | JsPath.assureAt = function(ref, path, initializer) {
|
99 | var obj;
|
100 | if (obj = this.getAt(ref, path)) {
|
101 | return obj;
|
102 | } else {
|
103 | this.setAt(ref, path, initializer);
|
104 | return initializer;
|
105 | }
|
106 | };
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | JsPath.deleteAt = function(ref, path) {
|
121 | var component, last, prev;
|
122 | if ('function' === typeof path.split) {
|
123 | path = path.split('.');
|
124 | } else {
|
125 | path = path.slice();
|
126 | }
|
127 | prev = [];
|
128 | last = path.pop();
|
129 | while (component = path.shift()) {
|
130 | if (primTypes.test(typeof ref[component])) {
|
131 | throw new Error("" + (prev.concat(component).join('.')) + " is\nprimitive; cannot drill any deeper.");
|
132 | }
|
133 | if (!(ref = ref[component])) return false;
|
134 | prev.push(component);
|
135 | }
|
136 | return delete ref[last];
|
137 | };
|
138 |
|
139 | return JsPath;
|
140 |
|
141 | }).call(this);
|
142 |
|
143 | |
144 |
|
145 |
|
146 |
|
147 |
|
148 | }).call(this);
|