mirror of
https://git.hmsn.ink/kospo/helptalk/api.git
synced 2026-03-20 14:33:28 +09:00
first
This commit is contained in:
43
sample/dev/talk/bundle/mithril-2.2.2/pathname/build.js
Normal file
43
sample/dev/talk/bundle/mithril-2.2.2/pathname/build.js
Normal file
@@ -0,0 +1,43 @@
|
||||
"use strict"
|
||||
|
||||
var buildQueryString = require("../querystring/build")
|
||||
var assign = require("../util/assign")
|
||||
|
||||
// Returns `path` from `template` + `params`
|
||||
module.exports = function(template, params) {
|
||||
if ((/:([^\/\.-]+)(\.{3})?:/).test(template)) {
|
||||
throw new SyntaxError("Template parameter names must be separated by either a '/', '-', or '.'.")
|
||||
}
|
||||
if (params == null) return template
|
||||
var queryIndex = template.indexOf("?")
|
||||
var hashIndex = template.indexOf("#")
|
||||
var queryEnd = hashIndex < 0 ? template.length : hashIndex
|
||||
var pathEnd = queryIndex < 0 ? queryEnd : queryIndex
|
||||
var path = template.slice(0, pathEnd)
|
||||
var query = {}
|
||||
|
||||
assign(query, params)
|
||||
|
||||
var resolved = path.replace(/:([^\/\.-]+)(\.{3})?/g, function(m, key, variadic) {
|
||||
delete query[key]
|
||||
// If no such parameter exists, don't interpolate it.
|
||||
if (params[key] == null) return m
|
||||
// Escape normal parameters, but not variadic ones.
|
||||
return variadic ? params[key] : encodeURIComponent(String(params[key]))
|
||||
})
|
||||
|
||||
// In case the template substitution adds new query/hash parameters.
|
||||
var newQueryIndex = resolved.indexOf("?")
|
||||
var newHashIndex = resolved.indexOf("#")
|
||||
var newQueryEnd = newHashIndex < 0 ? resolved.length : newHashIndex
|
||||
var newPathEnd = newQueryIndex < 0 ? newQueryEnd : newQueryIndex
|
||||
var result = resolved.slice(0, newPathEnd)
|
||||
|
||||
if (queryIndex >= 0) result += template.slice(queryIndex, queryEnd)
|
||||
if (newQueryIndex >= 0) result += (queryIndex < 0 ? "?" : "&") + resolved.slice(newQueryIndex, newQueryEnd)
|
||||
var querystring = buildQueryString(query)
|
||||
if (querystring) result += (queryIndex < 0 && newQueryIndex < 0 ? "?" : "&") + querystring
|
||||
if (hashIndex >= 0) result += template.slice(hashIndex)
|
||||
if (newHashIndex >= 0) result += (hashIndex < 0 ? "" : "&") + resolved.slice(newHashIndex)
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
"use strict"
|
||||
|
||||
var parsePathname = require("./parse")
|
||||
|
||||
// Compiles a template into a function that takes a resolved path (without query
|
||||
// strings) and returns an object containing the template parameters with their
|
||||
// parsed values. This expects the input of the compiled template to be the
|
||||
// output of `parsePathname`. Note that it does *not* remove query parameters
|
||||
// specified in the template.
|
||||
module.exports = function(template) {
|
||||
var templateData = parsePathname(template)
|
||||
var templateKeys = Object.keys(templateData.params)
|
||||
var keys = []
|
||||
var regexp = new RegExp("^" + templateData.path.replace(
|
||||
// I escape literal text so people can use things like `:file.:ext` or
|
||||
// `:lang-:locale` in routes. This is all merged into one pass so I
|
||||
// don't also accidentally escape `-` and make it harder to detect it to
|
||||
// ban it from template parameters.
|
||||
/:([^\/.-]+)(\.{3}|\.(?!\.)|-)?|[\\^$*+.()|\[\]{}]/g,
|
||||
function(m, key, extra) {
|
||||
if (key == null) return "\\" + m
|
||||
keys.push({k: key, r: extra === "..."})
|
||||
if (extra === "...") return "(.*)"
|
||||
if (extra === ".") return "([^/]+)\\."
|
||||
return "([^/]+)" + (extra || "")
|
||||
}
|
||||
) + "$")
|
||||
return function(data) {
|
||||
// First, check the params. Usually, there isn't any, and it's just
|
||||
// checking a static set.
|
||||
for (var i = 0; i < templateKeys.length; i++) {
|
||||
if (templateData.params[templateKeys[i]] !== data.params[templateKeys[i]]) return false
|
||||
}
|
||||
// If no interpolations exist, let's skip all the ceremony
|
||||
if (!keys.length) return regexp.test(data.path)
|
||||
var values = regexp.exec(data.path)
|
||||
if (values == null) return false
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
data.params[keys[i].k] = keys[i].r ? values[i + 1] : decodeURIComponent(values[i + 1])
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
24
sample/dev/talk/bundle/mithril-2.2.2/pathname/parse.js
Normal file
24
sample/dev/talk/bundle/mithril-2.2.2/pathname/parse.js
Normal file
@@ -0,0 +1,24 @@
|
||||
"use strict"
|
||||
|
||||
var parseQueryString = require("../querystring/parse")
|
||||
|
||||
// Returns `{path, params}` from `url`
|
||||
module.exports = function(url) {
|
||||
var queryIndex = url.indexOf("?")
|
||||
var hashIndex = url.indexOf("#")
|
||||
var queryEnd = hashIndex < 0 ? url.length : hashIndex
|
||||
var pathEnd = queryIndex < 0 ? queryEnd : queryIndex
|
||||
var path = url.slice(0, pathEnd).replace(/\/{2,}/g, "/")
|
||||
|
||||
if (!path) path = "/"
|
||||
else {
|
||||
if (path[0] !== "/") path = "/" + path
|
||||
if (path.length > 1 && path[path.length - 1] === "/") path = path.slice(0, -1)
|
||||
}
|
||||
return {
|
||||
path: path,
|
||||
params: queryIndex < 0
|
||||
? {}
|
||||
: parseQueryString(url.slice(queryIndex + 1, queryEnd)),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user