@@ -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} ) ( ) ;
0 commit comments