deno.land / x / deno@v1.28.2 / cli / bench / testdata / npm / hono / dist / utils / url.js

نووسراو ببینە
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.mergePath = exports.isAbsoluteURL = exports.getPathFromURL = exports.getPattern = exports.splitPath = void 0;const URL_REGEXP = /^https?:\/\/[a-zA-Z0-9\-\.:]+(\/?[^?#]*)/;const splitPath = (path) => { const paths = path.split(/\//); // faster than path.split('/') if (paths[0] === '') { paths.shift(); } return paths;};exports.splitPath = splitPath;const patternCache = {};const getPattern = (label) => { // * => wildcard // :id{[0-9]+} => ([0-9]+) // :id => (.+) //const name = '' if (label === '*') { return '*'; } const match = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/); if (match) { if (!patternCache[label]) { if (match[2]) { patternCache[label] = [label, match[1], new RegExp('^' + match[2] + '$')]; } else { patternCache[label] = [label, match[1], true]; } } return patternCache[label]; } return null;};exports.getPattern = getPattern;const getPathFromURL = (url, strict = true) => { const queryIndex = url.indexOf('?'); const result = url.substring(url.indexOf('/', 8), queryIndex === -1 ? url.length : queryIndex); // if strict routing is false => `/hello/hey/` and `/hello/hey` are treated the same // default is true if (strict === false && result.endsWith('/')) { return result.slice(0, -1); } return result;};exports.getPathFromURL = getPathFromURL;const isAbsoluteURL = (url) => { const match = url.match(URL_REGEXP); if (match) { return true; } return false;};exports.isAbsoluteURL = isAbsoluteURL;const mergePath = (...paths) => { let p = ''; let endsWithSlash = false; for (let path of paths) { /* ['/hey/','/say'] => ['/hey', '/say'] */ if (p.endsWith('/')) { p = p.slice(0, -1); endsWithSlash = true; } /* ['/hey','say'] => ['/hey', '/say'] */ if (!path.startsWith('/')) { path = `/${path}`; } /* ['/hey/', '/'] => `/hey/` */ if (path === '/' && endsWithSlash) { p = `${p}/`; } else if (path !== '/') { p = `${p}${path}`; } /* ['/', '/'] => `/` */ if (path === '/' && p === '') { p = '/'; } } return p;};exports.mergePath = mergePath;
deno

Version Info

Tagged at
2 years ago