github.com/outbrain/consul@v1.4.5/ui-v2/app/utils/routing/walk.js (about)

     1  export const walk = function(routes) {
     2    const keys = Object.keys(routes);
     3    keys.forEach((item, i) => {
     4      if (item === '_options') {
     5        return;
     6      }
     7      const options = routes[item]._options;
     8      let cb;
     9      if (Object.keys(routes[item]).length > 1) {
    10        cb = function() {
    11          walk.apply(this, [routes[item]]);
    12        };
    13      }
    14      this.route(item, options, cb);
    15    });
    16    if (typeof routes.index === 'undefined') {
    17      routes.index = {
    18        _options: {
    19          path: '',
    20        },
    21      };
    22    }
    23  };
    24  
    25  /**
    26   * Drop in for the Router.map callback e.g. `Router.map(walk(routes))`
    27   * Uses { walk } to recursively walk through a JSON object of routes
    28   * and use `Router.route` to define your routes for your ember application
    29   *
    30   * @param {object} routes - JSON representation of routes
    31   */
    32  export default function(routes) {
    33    return function() {
    34      walk.apply(this, [routes]);
    35    };
    36  }
    37  
    38  // The following code is purposefully commented out to prevent it from ending up
    39  // in the production codebase. In future it would be good to figure out how to do this
    40  // without having to use comments.
    41  
    42  // const indent = function(num) {
    43  //   return Array(num).fill('  ', 0, num).join('')
    44  // }
    45  // /**
    46  //  * String dumper to produce Router.map code
    47  //  * Uses { walk } to recursively walk through a JSON object of routes
    48  //  * to produce the code necessary to define your routes for your ember application
    49  //  *
    50  //  * @param {object} routes - JSON representation of routes
    51  //  * @example `console.log(dump(routes));`
    52  //  */
    53  // export const dump = function(routes) {
    54  //   let level = 2;
    55  //   const obj = {
    56  //     out: '',
    57  //     route: function(name, options, cb) {
    58  //       this.out += `${indent(level)}this.route('${name}', ${JSON.stringify(options)}`;
    59  //       if(cb) {
    60  //         level ++;
    61  //         this.out += `, function() {
    62  // `;
    63  //         cb.apply(this, []);
    64  //         level --;
    65  //         this.out += `${indent(level)}});
    66  // `;
    67  //       } else {
    68  //         this.out += ');';
    69  //       }
    70  //       this.out += `
    71  // `;
    72  //     }
    73  //   };
    74  //   walk.apply(obj, [routes])
    75  //   return `Router.map(
    76  //   function() {
    77  // ${obj.out}
    78  //   }
    79  // );`;
    80  // }