All files / tao-router/src routeTag.js

100% Statements 7/7
100% Branches 0/0
100% Functions 3/3
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31                                      3x 3x 3x 3x   4x 4x   3x      
import get from 'get-value';
 
/**
 * Template-literal tag compiling a URL template into a route-building
 * function. Each interpolation is a `get-value` lookup path resolved
 * against the data object the returned function is called with; missing
 * values render as `''`.
 *
 * ```js
 * const userRoute = route`/users/${'user.id'}`;
 * userRoute({ user: { id: 7 } }); // => '/users/7'
 * ```
 *
 * @param {TemplateStringsArray} strings - the template's literal parts
 * @param {...string} keys - `get-value` lookup paths (the interpolations)
 * @returns {function(Object=): string} builds the route path from a data
 *          object
 */
export function routeTag(strings, ...keys) {
  return (...values) => {
    const vals = values[0];
    const route = [strings[0]];
    keys.forEach((key, i) => {
      // Stryker disable next-line ObjectLiteral: equivalent - route.join('') below converts a missing/undefined value to '' exactly like the { default: '' } option would, so both produce the same final string
      const data = get(vals, key, { default: '' });
      route.push(data, strings[i + 1]);
    });
    return route.join('');
  };
}