-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove some duplicated URL generation code (#2240)
I went down a rabbit hole while trying to untangle the bug in the dockbit and bitrise examples #2234 (review). The URL generation code is spaghetti-like, with functions, many of which I wrote, with opaque names, doing similar but not identical things, and making slightly incompatible assumptions about the way query strings are handled. I got a bit lost and need to take a step back. Meanwhile, this is a small piece of work I did that’s worth keeping. It doesn’t scratch the surface of the tangle, but it does remove a bit of duplication. It also makes a minor stylistic ES6 change in the handling of default arguments. Ref: #2027
- Loading branch information
1 parent
e983f7b
commit 3bb392d
Showing
6 changed files
with
92 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict' | ||
|
||
const queryString = require('query-string') | ||
|
||
function encodeField(s) { | ||
return encodeURIComponent(s.replace(/-/g, '--').replace(/_/g, '__')) | ||
} | ||
|
||
function staticBadgeUrl({ | ||
baseUrl, | ||
label, | ||
message, | ||
color = 'lightgray', | ||
style, | ||
format = 'svg', | ||
}) { | ||
if (!label || !message) { | ||
throw Error('label and message are required') | ||
} | ||
const path = [label, message, color].map(encodeField).join('-') | ||
const outQueryString = queryString.stringify({ | ||
style, | ||
}) | ||
const suffix = outQueryString ? `?${outQueryString}` : '' | ||
return `/badge/${path}.${format}${suffix}` | ||
} | ||
|
||
module.exports = { | ||
encodeField, | ||
staticBadgeUrl, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict' | ||
|
||
const { test, given } = require('sazerac') | ||
const { encodeField, staticBadgeUrl } = require('./make-badge-url') | ||
|
||
describe('Badge URL generation functions', function() { | ||
test(encodeField, () => { | ||
given('foo').expect('foo') | ||
given('').expect('') | ||
given('happy go lucky').expect('happy%20go%20lucky') | ||
given('do-right').expect('do--right') | ||
given('it_is_a_snake').expect('it__is__a__snake') | ||
}) | ||
|
||
test(staticBadgeUrl, () => { | ||
given({ | ||
label: 'foo', | ||
message: 'bar', | ||
color: 'blue', | ||
style: 'flat-square', | ||
}).expect('/badge/foo-bar-blue.svg?style=flat-square') | ||
given({ | ||
label: 'foo', | ||
message: 'bar', | ||
color: 'blue', | ||
style: 'flat-square', | ||
format: 'png', | ||
}).expect('/badge/foo-bar-blue.png?style=flat-square') | ||
given({ | ||
label: 'Hello World', | ||
message: 'Привет Мир', | ||
color: '#aabbcc', | ||
}).expect( | ||
'/badge/Hello%20World-%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%20%D0%9C%D0%B8%D1%80-%23aabbcc.svg' | ||
) | ||
given({ | ||
label: '123-123', | ||
message: 'abc-abc', | ||
color: 'blue', | ||
}).expect('/badge/123--123-abc--abc-blue.svg') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters