Skip to content

Commit

Permalink
feat: allow :port shortcut, close #28
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Apr 5, 2018
1 parent 5a4845b commit ddbbc43
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 132 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ script:
- npm run demo3
- npm run demo4
- npm run demo5
- npm run demo6
after_success:
- npm run travis-deploy-once "npm run semantic-release"
branches:
Expand Down
144 changes: 15 additions & 129 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"demo3": "node bin/start.js start-with-child http://127.0.0.1:9000 test",
"demo4": "node bin/start.js 9000",
"demo5": "node bin/start.js start-with-child 9000",
"demo6": "node bin/start.js :9000",
"travis-deploy-once": "travis-deploy-once"
},
"devDependencies": {
Expand Down
12 changes: 10 additions & 2 deletions src/start-server-and-test-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ describe('utils', () => {
la(isUrlOrPort('6006'))
la(isUrlOrPort(8080))
})

it('allows :port string', () => {
la(isUrlOrPort(':6006'))
})
})

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

it('changes :port to localhost', () => {
la(normalizeUrl(':6006') === 'http://localhost:6006')
})

it('returns original argument if does not know what to do', () => {
la(normalizeUrl('foo') === 'foo')
la(normalizeUrl(808000) === 808000)
la(normalizeUrl('foo') === 'foo', normalizeUrl('foo'))
la(normalizeUrl(808000) === 808000, normalizeUrl(808000))
})
})
})
30 changes: 29 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
const is = require('check-more-types')

const isUrlOrPort = s => is.url(s) || is.port(parseInt(s))
const isUrlOrPort = s => {
if (is.url(s)) {
return s
}
if (is.number(s)) {
return is.port(s)
}
if (!is.string(s)) {
return false
}
if (s[0] === ':') {
const withoutColon = s.substr(1)
return is.port(parseInt(withoutColon))
}
return is.port(parseInt(s))
}

const normalizeUrl = s => {
if (is.url(s)) {
return s
}

if (is.number(s) && is.port(s)) {
return `http://localhost:${s}`
}

if (!is.string(s)) {
return s
}

if (is.port(parseInt(s))) {
return `http://localhost:${s}`
}

if (s[0] === ':') {
return `http://localhost${s}`
}
// for anything else, return original argument
return s
}
Expand Down

0 comments on commit ddbbc43

Please sign in to comment.