demo/bundle.js
/**
             * Expose a method for transforming tokens into the path function.
             */
Expose a method for transforming tokens into the path function.
Expose a method for transforming tokens into the path function .
function tokensToFunction (tokens) {
              var matches = new Array(tokens.length);
              for (var i = 0; i < tokens.length; i++) {
                if (typeof tokens[i] === 'object') {
                  matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$');
                }
              }
              return function (obj, opts) {
                var path = '';
                var data = obj || {};
                var options = opts || {};
                var encode = options.pretty ? encodeURIComponentPretty : encodeURIComponent;
                for (var i = 0; i < tokens.length; i++) {
                  var token = tokens[i];
                  if (typeof token === 'string') {
                    path += token;
                    continue
                  }
                  var value = data[token.name];
                  var segment;
                  if (value == null) {
                    if (token.optional) {
                      if (token.partial) {
                        path += token.prefix;
                      }
                      continue
                    } else {
                      throw new TypeError('Expected "' + token.name + '" to be defined')
                    }
                  }
                  if (isarray(value)) {
                    if (!token.repeat) {
                      throw new TypeError('Expected "' + token.name + '" to not repeat, but received `' + JSON.stringify(value) + '`')
                    }
                    if (value.length === 0) {
                      if (token.optional) {
                        continue
                      } else {
                        throw new TypeError('Expected "' + token.name + '" to not be empty')
                      }
                    }
                    for (var j = 0; j < value.length; j++) {
                      segment = encode(value[j]);
                      if (!matches[i].test(segment)) {
                        throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '", but received `' + JSON.stringify(segment) + '`')
                      }
                      path += (j === 0 ? token.prefix : token.delimiter) + segment;
                    }
                    continue
                  }
                  segment = token.asterisk ? encodeAsterisk(value) : encode(value);
                  if (!matches[i].test(segment)) {
                    throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but received "' + segment + '"')
                  }
                  path += token.prefix + segment;
                }
                return path
              }
            }
function tokensToFunction ( tokens ) { var matches = new Array ( tokens . length ) ; for ( var i = 0 ; i < tokens . length ; i ++ ) { if ( typeof tokens [ i ] === 'object' ) { matches [ i ] = new RegExp ( '^(?:' + tokens [ i ] . pattern + ')$' ) ; } } return function ( obj , opts ) { var path = '' ; var data = obj || { } ; var options = opts || { } ; var encode = options . pretty ? encodeURIComponentPretty : encodeURIComponent ; for ( var i = 0 ; i < tokens . length ; i ++ ) { var token = tokens [ i ] ; if ( typeof token === 'string' ) { path += token ; continue } var value = data [ token . name ] ; var segment ; if ( value == null ) { if ( token . optional ) { if ( token . partial ) { path += token . prefix ; } continue } else { throw new TypeError ( 'Expected "' + token . name + '" to be defined' ) } } if ( isarray ( value ) ) { if ( ! token . repeat ) { throw new TypeError ( 'Expected "' + token . name + '" to not repeat, but received `' + JSON . stringify ( value ) + '`' ) } if ( value . length === 0 ) { if ( token . optional ) { continue } else { throw new TypeError ( 'Expected "' + token . name + '" to not be empty' ) } } for ( var j = 0 ; j < value . length ; j ++ ) { segment = encode ( value [ j ] ) ; if ( ! matches [ i ] . test ( segment ) ) { throw new TypeError ( 'Expected all "' + token . name + '" to match "' + token . pattern + '", but received `' + JSON . stringify ( segment ) + '`' ) } path += ( j === 0 ? token . prefix : token . delimiter ) + segment ; } continue } segment = token . asterisk ? encodeAsterisk ( value ) : encode ( value ) ; if ( ! matches [ i ] . test ( segment ) ) { throw new TypeError ( 'Expected "' + token . name + '" to match "' + token . pattern + '", but received "' + segment + '"' ) } path += token . prefix + segment ; } return path } }
Expose a method for transforming tokens into the path function.
Expose a method for transforming tokens into the path function .
// Compile all the tokens into regexps. // Compile all the patterns before compilation. // Prepend partial segment prefixes.