MCPcopy Index your code
hub / github.com/deployd/deployd / tokensToFunction

Function tokensToFunction

test-app/public/sinon.js:31098–31177  ·  view source on GitHub ↗

* Expose a method for transforming tokens into the path function.

(tokens)

Source from the content-addressed store, hash-verified

31096 * Expose a method for transforming tokens into the path function.
31097 */
31098 function tokensToFunction (tokens) {
31099 // Compile all the tokens into regexps.
31100 var matches = new Array(tokens.length)
31101
31102 // Compile all the patterns before compilation.
31103 for (var i = 0; i < tokens.length; i++) {
31104 if (typeof tokens[i] === 'object') {
31105 matches[i] = new RegExp('^(?:' + tokens[i].pattern + ')$')
31106 }
31107 }
31108
31109 return function (obj, opts) {
31110 var path = ''
31111 var data = obj || {}
31112 var options = opts || {}
31113 var encode = options.pretty ? encodeURIComponentPretty : encodeURIComponent
31114
31115 for (var i = 0; i < tokens.length; i++) {
31116 var token = tokens[i]
31117
31118 if (typeof token === 'string') {
31119 path += token
31120
31121 continue
31122 }
31123
31124 var value = data[token.name]
31125 var segment
31126
31127 if (value == null) {
31128 if (token.optional) {
31129 // Prepend partial segment prefixes.
31130 if (token.partial) {
31131 path += token.prefix
31132 }
31133
31134 continue
31135 } else {
31136 throw new TypeError('Expected "' + token.name + '" to be defined')
31137 }
31138 }
31139
31140 if (isarray(value)) {
31141 if (!token.repeat) {
31142 throw new TypeError('Expected "' + token.name + '" to not repeat, but received `' + JSON.stringify(value) + '`')
31143 }
31144
31145 if (value.length === 0) {
31146 if (token.optional) {
31147 continue
31148 } else {
31149 throw new TypeError('Expected "' + token.name + '" to not be empty')
31150 }
31151 }
31152
31153 for (var j = 0; j < value.length; j++) {
31154 segment = encode(value[j])
31155

Callers 1

compileFunction · 0.85

Calls 2

encodeFunction · 0.85
encodeAsteriskFunction · 0.85

Tested by

no test coverage detected