Skip to content

Commit ddbbc43

Browse files
committed
feat: allow :port shortcut, close #28
1 parent 5a4845b commit ddbbc43

File tree

5 files changed

+56
-132
lines changed

5 files changed

+56
-132
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ script:
1616
- npm run demo3
1717
- npm run demo4
1818
- npm run demo5
19+
- npm run demo6
1920
after_success:
2021
- npm run travis-deploy-once "npm run semantic-release"
2122
branches:

package-lock.json

Lines changed: 15 additions & 129 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"demo3": "node bin/start.js start-with-child http://127.0.0.1:9000 test",
8181
"demo4": "node bin/start.js 9000",
8282
"demo5": "node bin/start.js start-with-child 9000",
83+
"demo6": "node bin/start.js :9000",
8384
"travis-deploy-once": "travis-deploy-once"
8485
},
8586
"devDependencies": {

src/start-server-and-test-spec.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ describe('utils', () => {
2727
la(isUrlOrPort('6006'))
2828
la(isUrlOrPort(8080))
2929
})
30+
31+
it('allows :port string', () => {
32+
la(isUrlOrPort(':6006'))
33+
})
3034
})
3135

3236
context('normalizeUrl', () => {
@@ -42,9 +46,13 @@ describe('utils', () => {
4246
la(normalizeUrl(8080) === 'http://localhost:8080')
4347
})
4448

49+
it('changes :port to localhost', () => {
50+
la(normalizeUrl(':6006') === 'http://localhost:6006')
51+
})
52+
4553
it('returns original argument if does not know what to do', () => {
46-
la(normalizeUrl('foo') === 'foo')
47-
la(normalizeUrl(808000) === 808000)
54+
la(normalizeUrl('foo') === 'foo', normalizeUrl('foo'))
55+
la(normalizeUrl(808000) === 808000, normalizeUrl(808000))
4856
})
4957
})
5058
})

src/utils.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
11
const is = require('check-more-types')
22

3-
const isUrlOrPort = s => is.url(s) || is.port(parseInt(s))
3+
const isUrlOrPort = s => {
4+
if (is.url(s)) {
5+
return s
6+
}
7+
if (is.number(s)) {
8+
return is.port(s)
9+
}
10+
if (!is.string(s)) {
11+
return false
12+
}
13+
if (s[0] === ':') {
14+
const withoutColon = s.substr(1)
15+
return is.port(parseInt(withoutColon))
16+
}
17+
return is.port(parseInt(s))
18+
}
419

520
const normalizeUrl = s => {
621
if (is.url(s)) {
722
return s
823
}
24+
25+
if (is.number(s) && is.port(s)) {
26+
return `http://localhost:${s}`
27+
}
28+
29+
if (!is.string(s)) {
30+
return s
31+
}
32+
933
if (is.port(parseInt(s))) {
1034
return `http://localhost:${s}`
1135
}
36+
37+
if (s[0] === ':') {
38+
return `http://localhost${s}`
39+
}
1240
// for anything else, return original argument
1341
return s
1442
}

0 commit comments

Comments
 (0)