@@ -303,6 +303,7 @@ module.exports = Common;
303303 /**
304304 * The console logging level to use, where each level includes all levels above and excludes the levels below.
305305 * The default level is 'debug' which shows all console messages.
306+ *
306307 * Possible level values are:
307308 * - 0 = None
308309 * - 1 = Debug
@@ -317,7 +318,7 @@ module.exports = Common;
317318
318319 /**
319320 * Shows a `console.log` message only if the current `Common.logLevel` allows it.
320- * The message will be prefixed with 'Matter. js' to make it easily identifiable.
321+ * The message will be prefixed with 'matter- js' to make it easily identifiable.
321322 * @method log
322323 * @param ...objs {} The objects to log.
323324 */
@@ -329,7 +330,7 @@ module.exports = Common;
329330
330331 /**
331332 * Shows a `console.info` message only if the current `Common.logLevel` allows it.
332- * The message will be prefixed with 'Matter. js' to make it easily identifiable.
333+ * The message will be prefixed with 'matter- js' to make it easily identifiable.
333334 * @method info
334335 * @param ...objs {} The objects to log.
335336 */
@@ -341,7 +342,7 @@ module.exports = Common;
341342
342343 /**
343344 * Shows a `console.warn` message only if the current `Common.logLevel` allows it.
344- * The message will be prefixed with 'Matter. js' to make it easily identifiable.
345+ * The message will be prefixed with 'matter- js' to make it easily identifiable.
345346 * @method warn
346347 * @param ...objs {} The objects to log.
347348 */
@@ -451,14 +452,27 @@ module.exports = Common;
451452 * The value of `this` refers to the last value returned in the chain that was not `undefined`.
452453 * Therefore if a passed function does not return a value, the previously returned value is maintained.
453454 * After all passed functions have been called the new function returns the last returned value (if any).
455+ * If any of the passed functions are a chain, then the chain will be flattened.
454456 * @method chain
455457 * @param ...funcs {function} The functions to chain.
456458 * @return {function } A new function that calls the passed functions in order.
457459 */
458460 Common . chain = function ( ) {
459- var funcs = Array . prototype . slice . call ( arguments ) ;
461+ var args = Array . prototype . slice . call ( arguments ) ,
462+ funcs = [ ] ;
460463
461- return function ( ) {
464+ for ( var i = 0 ; i < args . length ; i += 1 ) {
465+ var func = args [ i ] ;
466+
467+ if ( func . _chained ) {
468+ // flatten already chained functions
469+ funcs . push . apply ( funcs , func . _chained ) ;
470+ } else {
471+ funcs . push ( func ) ;
472+ }
473+ }
474+
475+ var chain = function ( ) {
462476 var lastResult ;
463477
464478 for ( var i = 0 ; i < funcs . length ; i += 1 ) {
@@ -471,6 +485,10 @@ module.exports = Common;
471485
472486 return lastResult ;
473487 } ;
488+
489+ chain . _chained = funcs ;
490+
491+ return chain ;
474492 } ;
475493
476494} ) ( ) ;
0 commit comments