forked from jekyll/jekyll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-dir-ensure-trailing-slash
executable file
·54 lines (45 loc) · 1.25 KB
/
file-dir-ensure-trailing-slash
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
#!/usr/bin/env ruby
require 'benchmark/ips'
# For this pull request, which changes Page#dir
# https://github.com/jekyll/jekyll/pull/4403
FORWARD_SLASH = '/'.freeze
def pre_pr(url)
url[-1, 1] == FORWARD_SLASH ? url : File.dirname(url)
end
def pr(url)
if url.end_with?(FORWARD_SLASH)
url
else
url_dir = File.dirname(url)
url_dir.end_with?(FORWARD_SLASH) ? url_dir : "#{url_dir}/"
end
end
def envygeeks(url)
return url if url.end_with?(FORWARD_SLASH) || url == FORWARD_SLASH
url = File.dirname(url)
url == FORWARD_SLASH ? url : "#{url}/"
end
# Just a slash
Benchmark.ips do |x|
path = '/'
x.report("pre_pr:#{path}") { pre_pr(path) }
x.report("pr:#{path}") { pr(path) }
x.report("envygeeks:#{path}") { pr(path) }
x.compare!
end
# No trailing slash
Benchmark.ips do |x|
path = '/some/very/very/long/path/to/a/file/i/like'
x.report("pre_pr:#{path}") { pre_pr(path) }
x.report("pr:#{path}") { pr(path) }
x.report("envygeeks:#{path}") { pr(path) }
x.compare!
end
# No trailing slash
Benchmark.ips do |x|
path = '/some/very/very/long/path/to/a/file/i/like/'
x.report("pre_pr:#{path}") { pre_pr(path) }
x.report("pr:#{path}") { pr(path) }
x.report("envygeeks:#{path}") { pr(path) }
x.compare!
end