Skip to content

Commit

Permalink
added Matter.before, Matter.after, Common.chainPathBefore, Common.cha…
Browse files Browse the repository at this point in the history
…inPathAfter, Common.get, Common.set
  • Loading branch information
liabru committed Nov 3, 2016
1 parent 05d0961 commit 50ad7ca
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/core/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,41 @@ module.exports = Common;
return values;
};

/**
* Gets a value from `base` relative to the `path` string.
* @method get
* @param {} obj The base object
* @param {string} path The path relative to `base`, e.g. 'Foo.Bar.baz'
* @param {number} [begin] Path slice begin
* @param {number} [end] Path slice end
* @return {} The object at the given path
*/
Common.get = function(obj, path, begin, end) {
path = path.split('.').slice(begin, end);

for (var i = 0; i < path.length; i += 1) {
obj = obj[path[i]];
}

return obj;
};

/**
* Sets a value on `base` relative to the given `path` string.
* @method set
* @param {} obj The base object
* @param {string} path The path relative to `base`, e.g. 'Foo.Bar.baz'
* @param {} val The value to set
* @param {number} [begin] Path slice begin
* @param {number} [end] Path slice end
* @return {} Pass through `val` for chaining
*/
Common.set = function(obj, path, val, begin, end) {
var parts = path.split('.').slice(begin, end);
Common.get(obj, path, 0, -1)[parts[parts.length - 1]] = val;
return val;
};

/**
* Returns a hex colour string made by lightening or darkening color by percent.
* @method shadeColor
Expand Down Expand Up @@ -491,4 +526,34 @@ module.exports = Common;
return chain;
};

/**
* Chains a function to excute before the original function on the given `path` relative to `base`.
* @method chainPathBefore
* @param {} base The base object
* @param {string} path The path relative to `base`
* @param {function} func The function to chain before the original
* @return {function} The chained function that replaced the original
*/
Common.chainPathBefore = function(base, path, func) {
return Common.set(base, path, Common.chain(
func,
Common.get(base, path)
));
};

/**
* Chains a function to excute after the original function on the given `path` relative to `base`.
* @method chainPathAfter
* @param {} base The base object
* @param {string} path The path relative to `base`
* @param {function} func The function to chain after the original
* @return {function} The chained function that replaced the original
*/
Common.chainPathAfter = function(base, path, func) {
return Common.set(base, path, Common.chain(
Common.get(base, path),
func
));
};

})();
25 changes: 25 additions & 0 deletions src/core/Matter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var Matter = {};
module.exports = Matter;

var Plugin = require('./Plugin');
var Common = require('./Common');

(function() {

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

/**
* Chains a function to excute before the original function on the given `path` relative to `Matter`.
* @method before
* @param {string} path The path relative to `Matter`
* @param {function} func The function to chain before the original
* @return {function} The chained function that replaced the original
*/
Matter.before = function(path, func) {
path = path.replace(/^Matter./, '');
return Common.chainPathBefore(Matter, path, func);
};

/**
* Chains a function to excute after the original function on the given `path` relative to `Matter`.
* @method after
* @param {string} path The path relative to `Matter`
* @param {function} func The function to chain after the original
* @return {function} The chained function that replaced the original
*/
Matter.after = function(path, func) {
path = path.replace(/^Matter./, '');
return Common.chainPathAfter(Matter, path, func);
};

})();

0 comments on commit 50ad7ca

Please sign in to comment.