Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ports, hosts, and targets subforms actually work
- Loading branch information
1 parent
36dbae3
commit cfb5eb2
Showing
5 changed files
with
209 additions
and
62 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
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,33 @@ | ||
import { parsePortRange } from './parse' | ||
|
||
describe('parsePortRange', () => { | ||
describe('parses', () => { | ||
it('parses single ports up to 5 digits', () => { | ||
expect(parsePortRange('0')).toEqual([0, 0]) | ||
expect(parsePortRange('1')).toEqual([1, 1]) | ||
expect(parsePortRange('123')).toEqual([123, 123]) | ||
expect(parsePortRange('12356')).toEqual([12356, 12356]) | ||
}) | ||
|
||
it('parses ranges', () => { | ||
expect(parsePortRange('123-456')).toEqual([123, 456]) | ||
expect(parsePortRange('1-45690')).toEqual([1, 45690]) | ||
expect(parsePortRange('5-5')).toEqual([5, 5]) | ||
}) | ||
}) | ||
|
||
describe('rejects', () => { | ||
it('nonsense', () => { | ||
expect(parsePortRange('12a5')).toEqual(null) | ||
expect(parsePortRange('lkajsdfha')).toEqual(null) | ||
}) | ||
|
||
it('p2 < p1', () => { | ||
expect(parsePortRange('123-45')).toEqual(null) | ||
}) | ||
|
||
it('too many digits', () => { | ||
expect(parsePortRange('239032')).toEqual(null) | ||
}) | ||
}) | ||
}) |
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,18 @@ | ||
type PortRange = [number, number] | ||
|
||
/** Parse '1234' into [1234, 1234] and '80-100' into [80, 100] */ | ||
// TODO: parsing should probably throw errors rather than returning | ||
// null so we can annotate the failure with a reason | ||
export function parsePortRange(portRange: string): PortRange | null { | ||
// TODO: pull pattern from openapi spec (requires generator work) | ||
const match = /^([0-9]{1,5})((?:-)[0-9]{1,5})?$/.exec(portRange) | ||
if (!match) return null | ||
|
||
const p1 = parseInt(match[1], 10) | ||
// API treats a single port as a range with the same start and end | ||
const p2 = match[2] ? parseInt(match[2].slice(1), 10) : p1 | ||
|
||
if (p2 < p1) return null | ||
|
||
return [p1, p2] | ||
} |