Skip to content

Commit 50ad7ca

Browse files
committed
added Matter.before, Matter.after, Common.chainPathBefore, Common.chainPathAfter, Common.get, Common.set
1 parent 05d0961 commit 50ad7ca

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

src/core/Common.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,41 @@ module.exports = Common;
108108
return values;
109109
};
110110

111+
/**
112+
* Gets a value from `base` relative to the `path` string.
113+
* @method get
114+
* @param {} obj The base object
115+
* @param {string} path The path relative to `base`, e.g. 'Foo.Bar.baz'
116+
* @param {number} [begin] Path slice begin
117+
* @param {number} [end] Path slice end
118+
* @return {} The object at the given path
119+
*/
120+
Common.get = function(obj, path, begin, end) {
121+
path = path.split('.').slice(begin, end);
122+
123+
for (var i = 0; i < path.length; i += 1) {
124+
obj = obj[path[i]];
125+
}
126+
127+
return obj;
128+
};
129+
130+
/**
131+
* Sets a value on `base` relative to the given `path` string.
132+
* @method set
133+
* @param {} obj The base object
134+
* @param {string} path The path relative to `base`, e.g. 'Foo.Bar.baz'
135+
* @param {} val The value to set
136+
* @param {number} [begin] Path slice begin
137+
* @param {number} [end] Path slice end
138+
* @return {} Pass through `val` for chaining
139+
*/
140+
Common.set = function(obj, path, val, begin, end) {
141+
var parts = path.split('.').slice(begin, end);
142+
Common.get(obj, path, 0, -1)[parts[parts.length - 1]] = val;
143+
return val;
144+
};
145+
111146
/**
112147
* Returns a hex colour string made by lightening or darkening color by percent.
113148
* @method shadeColor
@@ -491,4 +526,34 @@ module.exports = Common;
491526
return chain;
492527
};
493528

529+
/**
530+
* Chains a function to excute before the original function on the given `path` relative to `base`.
531+
* @method chainPathBefore
532+
* @param {} base The base object
533+
* @param {string} path The path relative to `base`
534+
* @param {function} func The function to chain before the original
535+
* @return {function} The chained function that replaced the original
536+
*/
537+
Common.chainPathBefore = function(base, path, func) {
538+
return Common.set(base, path, Common.chain(
539+
func,
540+
Common.get(base, path)
541+
));
542+
};
543+
544+
/**
545+
* Chains a function to excute after the original function on the given `path` relative to `base`.
546+
* @method chainPathAfter
547+
* @param {} base The base object
548+
* @param {string} path The path relative to `base`
549+
* @param {function} func The function to chain after the original
550+
* @return {function} The chained function that replaced the original
551+
*/
552+
Common.chainPathAfter = function(base, path, func) {
553+
return Common.set(base, path, Common.chain(
554+
Common.get(base, path),
555+
func
556+
));
557+
};
558+
494559
})();

src/core/Matter.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var Matter = {};
99
module.exports = Matter;
1010

1111
var Plugin = require('./Plugin');
12+
var Common = require('./Common');
1213

1314
(function() {
1415

@@ -56,4 +57,28 @@ var Plugin = require('./Plugin');
5657
Plugin.use(Matter, Array.prototype.slice.call(arguments));
5758
};
5859

60+
/**
61+
* Chains a function to excute before the original function on the given `path` relative to `Matter`.
62+
* @method before
63+
* @param {string} path The path relative to `Matter`
64+
* @param {function} func The function to chain before the original
65+
* @return {function} The chained function that replaced the original
66+
*/
67+
Matter.before = function(path, func) {
68+
path = path.replace(/^Matter./, '');
69+
return Common.chainPathBefore(Matter, path, func);
70+
};
71+
72+
/**
73+
* Chains a function to excute after the original function on the given `path` relative to `Matter`.
74+
* @method after
75+
* @param {string} path The path relative to `Matter`
76+
* @param {function} func The function to chain after the original
77+
* @return {function} The chained function that replaced the original
78+
*/
79+
Matter.after = function(path, func) {
80+
path = path.replace(/^Matter./, '');
81+
return Common.chainPathAfter(Matter, path, func);
82+
};
83+
5984
})();

0 commit comments

Comments
 (0)